Question

In: Computer Science

write a program in C Write a function that is passed an array of characters containing...

write a program in C

Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use a switch statement, and your function should accept an array of any size. Feel free to pass in the array size as a parameter so you know how many grade values you'll need to check in your loop. For example, if you passed a function the following array: char grades [ ] = {'A', 'b', 'C', 'x', 'D', 'c', 'F', 'B', 'Y', 'B', 'B', 'A'}; It would return the following information within a structure (gradeTotals) to the calling function. Grade Total --------- ------ A 2 B 4 C 2 D 1 F 1 I 2 Hint: Design the function as: struct gradeTotals gradeCount (char gradeArray [ ], int arraySize)

Solutions

Expert Solution

//C code

#include<stdio.h>

//struct
struct gradeTotals
{
   char gradeA, gradeB, gradeC,
       gradeD, gradeF, gradeI;
   int countA=0, countB=0, countC=0,
       countD=0, countF=0, countI = 0;
};

//Fucntion prototype
struct gradeTotals gradeCount(char gradeArray[], int arraySize);

//Function defintion
struct gradeTotals gradeCount(char gradeArray[], int arraySize)
{
   gradeTotals gt;
   for (int i = 0; i < arraySize; i++)
   {
       // match both lower and upper case grades
       switch (gradeArray[i])
       {
       case 'a':
       case 'A':
           gt.gradeA = 'A';
           gt.countA++;
           break;
       case 'b':
       case 'B':
           gt.gradeB = 'B';
           gt.countB++;
           break;
       case 'c':
       case 'C':
           gt.gradeC = 'C';
           gt.countC++;
           break;
       case 'd':
       case 'D':
           gt.gradeD = 'D';
           gt.countD++;
           break;
       case 'F':
       case 'f':
           gt.gradeF = 'F';
           gt.countF++;
           break;
       default:
           gt.gradeI = 'I';
           gt.countI++;
           break;

       }
   }
   return gt;
}
//main function
int main()
{
   char grades[] = { 'A', 'b', 'C', 'x', 'D', 'c', 'F', 'B', 'Y', 'B', 'B', 'A' };
   //call function
   gradeTotals gt = gradeCount(grades, 12);

   //print info
   printf("Grade Total------%c%d %c%d %c%d %c%d %c%d %c%d\n", gt.gradeA, gt.countA,
       gt.gradeB, gt.countB, gt.gradeC, gt.countC, gt.gradeD, gt.countD, gt.gradeF, gt.countF, gt.gradeI, gt.countI);
   return 0;
}

//Output

//If you need any help regarding this solution........ please leave a comment......... thanks


Related Solutions

c++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype. int maximum ( int *p [ ], int n); 2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the...
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a C++ function to sort the characters in words in a inputed phrase. DO not...
Write a C++ function to sort the characters in words in a inputed phrase. DO not sort the characters in the string. Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output. Make the program as simple as possible. Use introductory Object-Oriented C++ programming. If all the requirements are met, then this will be upvoted. Do use the C++ Standard Library headers, do not use the C Standard...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT