Reputation: 11
Good evening, I having only started learning C this week so please bear with me. I need my program to arrange the grades from highest to lowest value and give a maximum, minimum, and average calculation. If you can point me in the right direction that would be great. Please keep in mind that I am still learning C lingo.
Thanks! -Tony
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name[40];
char mood[20];
char a1name[15];
char a2name[15];
char a3name[15];
char a4name[15];
int numberofassignments;
int score;
int firstgrade;
int a1, a2, a3, a4, a5, a6;
/* Intro */
printf("Enter your first name: \n");
scanf("%s", name);
printf("Hello, %s, How are you? \n", name);
scanf("%s", mood);
printf("Glad you're %s. \n\r", mood);
printf("How many assignments do you have? \n");
scanf("%d", &numberofassignments);
printf("Ok so you have %d assignments. \n", numberofassignments);
/* Assignment Names */
printf("What is the name of your first assignment? \n");
scanf("%s", a1name);
printf("What is the name of your Second assignment? \n");
scanf("%s", a2name);
printf("What is the name of your Third assignment? \n");
scanf("%s", a3name);
printf("What is the name of your Fourth assignment? \n");
scanf("%s", a4name);
/* Assignment Grades */
printf("What is your grade for %s? ", a1name);
scanf("%d", &a1);
printf("What is your grade for %s? ", a2name);
scanf("%d", &a2);
printf("What is your grade for %s? ", a3name);
scanf("%d", &a3);
printf("What is your grade for %s? ", a4name);
scanf("%d", &a4);
/* Assignment Grade and name overview */
printf("First Assignment: %s \n", a1name);
printf("Grade: %d \n", a1);
printf("First Assignment: %s \n", a2name);
printf("Grade: %d \n", a2);
printf("First Assignment: %s \n", a3name);
printf("Grade: %d \n", a3);
printf("First Assignment: %s \n", a4name);
printf("Grade: %d \n", a4);
return 0;
}
Upvotes: 1
Views: 133
Reputation: 1977
There is a lot of room for improvement in your code, but I am going to try to focus only on the questions that you have asked and see if I can help direct you to a solution. The first suggestion (about an array of structures) in the comment given by @BLUEPIXY is where I would start as well. For example:
typedef struct
{
char name[15];
int grade;
} Assignment;
With the above structure, you can now declare a variable (or in this case, an array) of the type Assignment
like this:
Assignment assignmentData[15];
There are two advantages to using a structure in this case. One is that, for a given assignment (i.e. assignmentData[0]
), we have now associated the name with the grade. The other is that you can now take advantage of loops, like this:
unsigned char i;
/* Note 1: numberofassignemnts must be less than 15. */
for (i = 0; i < numberofassignments; i++)
{
/* Note 2: As @Mauricio points out in the comment, you
will have problems if the user inputs more than 15 characters. */
printf("What is the name of assignment number %d?\n", (i+1));
scanf("%s", assignmentData[i].name);
printf("What is your grade for %s?\n", assignmentData[i].name);
scanf("%d", &assignmentData[i].grade);
}
At this point you can see the advantage of using the structure above. Sorting the structures can be done in different ways... for example, you could declare a second array like this:
Assignment sortedAssignments[15];
Then, utilize a while()
loop to iterate through your original assignments. Place the assignment with the highest grade into sortedAssignments
first, continuing until you've moved all of the assignmentData into sortedAssignments. It's not the most elegant solution, but it is easy to understand and implement. I would also encourage you to search for information on linked-lists and other sorting methods. They can be far more efficient and allow you to dynamically create larger data sets based on user input... but I digress.
Finally, functions for the maximum, minimum and average values are quite simple to implement. After sorting, you know that your maximum value is the first assignment in your array (sortedAssignments[0].grade
) and the minimum value is the last assignment in your array (sortedAssignments[numberofassignments - 1].grade
). The average can be accomplished in another loop... or even better, in the original for()
loop that we used above:
...
scanf("%d", &assignmentData[i].grade);
/* Assuming that we declared: int avg = 0;*/
avg = avg + assignmentData[i].grade;
} /* End of for() loop */
avg = avg / numberofassignments;
I hope that helps to get you on the right track. I purposely left out the code to actually sort your assignments because that is something you should try on your own. Good luck!
Upvotes: 2