kb3292
Sep 9, 2015 1:02 AM
There are too many ways of achieving what you need. The following is a very simple way. It is done in c. Also note that declaring arrays like I have done in the little program below (saying int array[6]), is pretty useless and can lead to dangerous consequences. Always need to allocate memory dynamically using dynamic memory allocation. i use the c89 standard. I am sure you know how to compile but heres what i do to check the integrity with c89.
gcc -std=c89 -pedantic -o filename filename.c
/*
Author: Kb3292
September 9, 2015
A little help for a friend on Sajha.com
"""Instructions:
User inputs six grades which gets stored in array.
It drops the lowest grade and computes avg of remaining 5 grades."""
*/
# include
# include
# include
# define Integer int
# define Float float
# define Character char
/* Function Prototypes */
void Enter_Grades(Float *Array);
void Print_Grades(Float *Grade_List, Integer Size_Of_Grade_List);
void Find_Minimum_And_Average_Grade(Float *Grade_List, Integer Size_Of_Grade_List);
/* End of Function Prototypes */
/* Functions */
void Enter_Grades(Float *Array)
{
Integer Index;
printf("Please enter 6 grades! \n:");
for (Index = 0; Index < 6; Index ++)
{
scanf("%f", &Array[Index]);
}
}
void Print_Grades(Float *Grade_List, Integer Size_Of_Grade_List)
{
Integer Index;
printf("The 6 grades that were entered and kept in an array are: \n");
for (Index = 0; Index < Size_Of_Grade_List; Index ++)
{
printf("%.2f ", Grade_List[Index]);
}
printf("\n");
}
void Find_Minimum_And_Average_Grade(Float *Grade_List, Integer Size_Of_Grade_List)
{
Float My_Minimum;
Integer Index;
Float Sum = 0;
Float Average;
My_Minimum = Grade_List[0]; /* Assume that the first index element is the lowest and keep changing it*/
for (Index = 0; Index < Size_Of_Grade_List; Index ++)
{
if (Grade_List[Index] <= My_Minimum)
{
My_Minimum = Grade_List[Index];
}
}
printf("The minumum grade is: %.2f\n", My_Minimum);
/* Find average here */
for (Index = 0; Index < Size_Of_Grade_List; Index ++)
{
if (Grade_List[Index] != My_Minimum)
{
Sum += Grade_List[Index];
}
}
printf("The sum of the remaining grades is : %.2f\n", Sum);
Average = Sum/(Size_Of_Grade_List-1);
printf("The average of the remaining grades is: %.2f \n", Average);
}
/* End of Functions */
Integer main (Integer argc, Character *argv[])
{
Float Array[6];
Enter_Grades(Array);
Print_Grades(Array, 6); /* just use six because we know its six for this instance, but in reality it should be dynamically allocated*/
Find_Minimum_And_Average_Grade(Array, 6);
return 0;
}
Notice, doing something like the above (void Enter_Grades(Float Array[])) or (void Enter_Grades(Float *Array) is not passing the array itself into the methods neither is calling the method like this (Enter_Grades(Array)). In reality what happens is pointers are used and what pointers allow us to do is do whats called "pass by reference" instead of passing values you send the "address" or the "reference of the pointer" if you will, to the method which will directly be accessed by that method. Pointers are the heart and soul of c and c++. I hope you can read further into mr ritchie's book . It is so very helpful. Studied it all the way during several of my classes, long after i was done with my c class. I hope this helps.