Question

In: Computer Science

C language Modify the getAvg() function to support passing an array by reference. The parameters of...

C language

  1. Modify the getAvg() function to support passing an array by reference.
    • The parameters of this function should be pointer to a float array, and the size of the array.
    • Returns the average of the array.
  2. Build a function called getScoreFromUser() with the following properties:
    • Asks the user to enter the score
    • scans into a local float variable
    • returns the float value that the user entered.
  3. Build and present a menu to a user like the following and enclose it into a loop that ends when the Quit option is chosen. Insert a line with your favorite character before and after printing the menu.

    ***************************
    1. Enter user name.
    2. Enter scores.
    3. Display average score.
    4. Display summary.
    5. Quit
      ***************************
  4. For Option #1, scanf a string, and store it to a username char array.
  5. For Option #2, do the following :
    • Ask the user how many exams they have and store that value into an int variable.
    • Use a for loop in combination with getScoreFromUser() to get the user's scores.

      Hint: Declare a float array of size 20 to store the user's scores. If the user wants to enter more than the array size we declared, display an error, and have the user re-enter the test count. Some array members will be left un-used which is okay.

      Hint: You can use this inside your for loop: scores_array[i] = getScoreFromUser()
    • Use the getAvg() function to calculate the average of all the tests the user entered.
  6. For Option #3, do the following :
    • If the user has already entered test scores, display the average of all the test scores.
    • If the user has not yet entered test scores, display an error message similar to: "Please use the menu to enter test scores first."
  7. For Option #4, if the user has not yet entered their name and test scores, display an error message. Otherwise display the average, the letter grade of the average, and the user's name.

    Example: "Hello Charley, your test scores were 70, 75, 80, 85, 90, 95. Your average is 82.5 with letter grade: B."

    Hint: You can print "Hello Charley, your test scores were " first, and then use for loop to print thetest scores, followed by "Your average is 90.0 with letter grade: A."

Solutions

Expert Solution

//header files
#include<stdio.h>
#include<stdlib.h>
//function to calculate and return average
float getAvg(float *a,int s)
{
//loop variable
int j;
//variable to hold sum of elements of array
float sum_elements=0;
//loop to go from 0 to array size s
for(j=0;j<s;j++)
{
//sum up the elements of array a
sum_elements=sum_elements+a[j];

}
//return the average which is sum of the elements/number of elements in array a
return sum_elements/s;

}
//function to get input from user
float getScoreFromUser()
{
//score variable
float score;
//taking score as an input from user and storing in score variable
printf("Enter score: ");
scanf("%f",&score);
//returning the score
return score;
}
//main function
int main()
{
//character array to hold username
char username[30];
//variable to store choice from menu
int c;
//variable to store number of exams
int n;
//flag for test scores used to check if the test scores are entered or not
int flag_scores=0;
//flag for name to check if the name is entered or not
int flag_name=0;
//array to store test scores
float scores_array[20] ;
//variable to store average of test scores
float average;
//character to store grade as per the average
char ch;
//infinite loop
while(1)
{
//printing the menu as required
printf("***************************\n");
printf("Enter user name.\n");
printf("Enter scores.\n");
printf("Display average score.\n");
printf("Display summary.\n");
printf("Quit\n");
printf("***************************\n");
//taking choice from the user as an integer input
scanf("%d",&c);
//if c is 5 then
if(c==5)
{
//exit from the program
exit(0);
}
//if c is 1 then
else if(c==1)
{
//taking name as an input from user
printf("Enter name:");
scanf("%s",username);
//setting its flag to 1
flag_name=1;
}
//if c is 2
else if(c==2)
{
//infinite loop
while(1)
{
//asking for number of exams
printf("How many exams do you have?");
//taking number of exams as an input from user in n
scanf("%d",&n);
//if n is less than equal to 20 and greater than 1( as n has to be positive) then
if(n<=20 && n>=1)
{
//come out of this loop
break;
}
//if n >20 then loop will be continue until user enters n value less then 20 and greater than 1

}
//loop in which i is going from 0 to n
for(int i=0;i<n;i++)
{
//calling getScoreFromUser function and storing its returned value in array scores_array at its index i
scores_array[i] = getScoreFromUser();
}
//setting its flag to 1
flag_scores=1;
}
//if c is 3
else if(c==3)
{
//if flag_scores is not 1 that means user has not yet entered test scores
if(flag_scores==0)
printf("Please use the menu to enter test scores first.\n");//print the error message
//otherwise
else
{
//print the average of test scores by calling getAvg and passing array and its size to it
printf("Average of all test scores is %.2f\n",getAvg(scores_array,n));
}

}
//if c is 4
else if(c==4)
{
//if user has entered both scores and name then
if(flag_scores==1 && flag_name==1)
{
//store the average in average variable
average=getAvg(scores_array,n);
//if average is in between 100 and 90
if(average<=100 && average>=90 )
ch='A';//set ch to A
//else if average is in between 80 and 90
else if (average<90 && average>=80 )
ch='B';//set ch to B
//else if average is in between 70 and 80
else if (average<80 && average>=70 )
ch='C';//set ch to C
//else if average is in between 60 and 70
else if (average<70 && average>=60 )
ch='D';//set ch to E
//else if average is in between 60 and 40
else if (average<60 && average>=40 )
ch='E';//set ch to E
//else if average is less than 40
else if (average<40)
ch='F';//set ch to F
//printing the name
printf("Hello %s,your test scores were ",username);
//printing the test scores
for(int i=0;i<n;i++)
{
//if i is the last index
if(i==n-1)
printf("%.2f. ",scores_array[i]);//print the array element at i index and a "." after it with 2 digits after decimal place
//otherwise
else
printf("%.2f, ",scores_array[i]);//print element at i index and a "," after it with 2 digits after decimal place
}
//printing the average with 2 digits after decimal place
printf("Your average is %.2f with letter grade: %c.\n",average,ch);
}
//if test scores are entered but name is not entered
else if (flag_scores==1 && flag_name==0)
{
//print the appropriate message
printf("Please enter the name first\n");
}
//if test scores are not entered but name is entered by user
else if(flag_scores==0 && flag_name==1)
{
//print the appropriate message
printf("Please enter the test scores first\n");
}
//otherwise
else
{
//print the appropriate message
printf("Please enter the name and test scores first\n");
}

}
}//loop ends
return 0;
}//main ends


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Write a function that will accept two integer matrices C and D by reference parameters. The...
Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks] Explain the time complexity of this function inside of the function code as a comment. [3 marks] in C++
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
In C language: Write a function which can receive a float array, an integer number(number of...
In C language: Write a function which can receive a float array, an integer number(number of elements) as input arguments and print all the elements in the array with 2 decimal precision.
C# Discussion question Why can reference parameters not be optional parameters? What are the conditions for...
C# Discussion question Why can reference parameters not be optional parameters? What are the conditions for generating ambiguous methods? What arethe differences between mandatory parameters and optional parameters? Explain the use of arguments in reference parameters, output parameters, and parameter arrays. Expalin overloading.
In C language: Write the fnFindMin function which can receive a float array, an integer number(number...
In C language: Write the fnFindMin function which can receive a float array, an integer number(number of elements) as input arguments and finds the minimum value in the array. This function should return the array index (subscript) of the minimum value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT