Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX_GRADE 100
- #define MIN_GRADE 0
- void fillArr(int* arr, int len);
- void fillGrade(int* arr, int index);
- void printAvgGrade(int* arr, int len);
- void changeGrade(int* arr, int len);
- void printGrades(int* arr, int len);
- void changeNumGrades(int* arr, int* len);
- int validGrade(int grade);
- enum options
- {
- avarage = 1,
- gradeChange,
- changeAmountGrades,
- print,
- exitProgram
- };
- int main()
- {
- int numGrades = 0;
- enum options choice = 0;
- int* arrGrades = 0;
- //getting input from user
- printf("Please enter the number of grades: ");
- scanf("%d", &numGrades);
- fillArr(arrGrades, numGrades);
- //executing choice
- do {
- printf("%d - Calculate avarage Grade\n", avarage);
- printf("%d - Change grade\n", gradeChange);
- printf("%d - Change number of grades\n", changeAmountGrades);
- printf("%d - Print Grades\n", print);
- printf("%d - Exit\n", exitProgram);
- printf("Choose an option: ");
- scanf("%d", &choice);
- switch (choice) {
- case avarage:
- printAvgGrade(arrGrades, numGrades);
- break;
- case gradeChange:
- changeGrade(arrGrades, numGrades);
- break;
- case changeAmountGrades:
- changeNumGrades(arrGrades, numGrades);
- break;
- case print:
- printGrades(arrGrades, numGrades);
- break;
- case exitProgram:
- break;
- default:
- printf("Invalid choice! only 1-5, try again...\n");
- }
- } while (choice != exitProgram);
- return 0;
- }
- /*
- This function fill the array with a given len
- input: a pointer to the array, its len
- output: NONE
- */
- void fillArr(int* arr, int len)
- {
- int i = 0;
- arr = (int*)malloc(len * sizeof(int));
- if (arr == NULL)
- {
- printf("Malloc fail!\n");
- return 1;
- }
- for (i = 0; i < len; i++)
- {
- fillGrade(arr, i);
- }
- }
- /*
- This funcion calc's and prints the avarage of grade
- input: the arr, its len
- outpu: NONE
- */
- void printAvgGrade(int* arr, int len)
- {
- int i = 0;
- int sum = 0;
- for (i = 0; i < len; i++)
- {
- sum += arr[i];//error here
- }
- printf("The avarage grade is %2f", (float)sum / len);
- }
- /*
- This function changes a given grade in the array
- input: The array, its lengh
- output: NONE
- */
- void changeGrade(int* arr, int len)
- {
- int position = 0;
- printf("Which grade would you like to change?%d - %d: ", 1, len);
- do
- {
- scanf("%d", &position);
- (position < 1 && position > len) ? printf("Invalid position try again: ") : printf("\n");
- } while (position < 1 && position > len);
- fillGrade(arr, (position - 1));
- }
- /*
- This function changes the amount of grade if it greater then the previous amount
- input: the array, a pointer to its len
- output: NONE
- */
- void changeNumGrades(int* arr, int* len)
- {
- int newLen = 0;
- int i = 0;
- printf("Please enter a new number of grades: ");
- scanf("%d", &newLen);
- if (newLen > *len)
- {
- for (i = *len; i < newLen; i++)
- {
- fillGrade(arr, i);
- }
- //changing the len of the arr to the new len
- *len = newLen;
- }
- }
- /*
- This function prints the grades
- input: the array and its len
- output: NONE
- */
- void printGrades(int* arr, int len)
- {
- int i = 0;
- for (i = 0; i < len; i++)
- {
- printf("%d ", arr[i]);//error here
- }
- }
- /*
- This function fills a grade of a given index
- input: the index
- output:NONE
- */
- void fillGrade(int* arr, int index)
- {
- printf("Enter Grade %d: ", index + 1);
- do
- {
- scanf("%d", &arr[index]);
- if (!validGrade(arr[index])) {
- printf("Invalid grade only 0-100 try again: ");
- }
- } while (!validGrade(arr[index]));
- }
- /*
- This function check if a grade is valid
- input: a number
- output: 0 or 1
- */
- int validGrade(int grade)
- {
- return (grade >= MIN_GRADE && grade <= MAX_GRADE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement