Need help with this project. User inputs six grades which gets stored in array. It drops the lowest grade and computes avg of remaining 5 grades. Need to pass array to method .. #include using namespace std; void grade(double[]); double lowestGrade(double[]); double avg(double[]); void grade(double grade[]) { for (int i = 0; i > grade[i]; } } double avg(double grade[]) { double temp = 0.00; for (int i = 0; i > grade[count]; } // Compute the average of total grade. avg = total / 6; // Following code displays the result cout << "Your average grade is " << avg << endl; return 0;
cp21 · Sep 7, 2015 12:19 PM · 16,914 views
Please need help on this project
cp21 · Sep 8, 2015 6:15 PM
La bro this is in c#.. i think you can convert it to C++ easily.. namespace ConsoleApplication3 { class Program { public static int dropTheLowest(int[] passTheArray) { int min = int.MaxValue; int index=-1; //find the lowest numebr in array.. for (int i = 0; i < passTheArray.Length; i++) { if (passTheArray[i] < min) { min = passTheArray[i]; index = i; } } int[] arrayWithTheLowest = new int[passTheArray.Length - 1]; int position = 0; for (int i = 0; i < passTheArray.Length; i++) { if (i != index) { arrayWithTheLowest[position] = passTheArray[i]; position++; } } int totalSum = arrayWithTheLowest.Sum(); return totalSum; } static void Main(string[] args) { int[] grade = new int[3]; for (int i = 0; i <grade.Length; i++) { Console.WriteLine("Enter a number"); grade[i] =Convert.ToInt32( Console.ReadLine()); } int num =dropTheLowest(grade); Console.WriteLine(num); Console.ReadKey(); } } }
alece · Sep 8, 2015 7:12 PM
Hah! just realized i forgot to do the average part. Hope you can figure that out.
alece · Sep 8, 2015 7:29 PM
I should not do this but do practice on your own too. Unless you practice you wont get it. (This is in c++) #include using namespace std; float lowestGrade(float []); void gradeIn(float grade[]) { cout > grade[i]; cout grade[i]) { low = grade[i]; } } return low; } //main program int main() { float grade[5]; float average; gradeIn(grade); average = avg(grade); // Following code displays the result cout << "Your average grade is " << average << endl; return 0; }
kathmandubasinda · Sep 8, 2015 7:57 PM
My friend, you can't "pass" arrays in c or c++, what is passed is pointers. Please read the c documentation or a good c book. The one by Dennis ritchie called "The C programming language" or something along that line is a pretty good one.
kb3292 · Sep 8, 2015 11:42 PM
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.
kb3292 · Sep 9, 2015 1:02 AM
Not from C background but why not create a Global variable holding array and access it from any methods?
helpjava11 · Sep 9, 2015 8:47 AM
@helpjava11.. coz global variables are bad ?
alece · Sep 9, 2015 8:58 AM
Help java,global variables in general are a easy way to cope out. Although it may seem easier because you don't have to pass around information between methods, they are dangerous and can potentenially lead to unintended consequences. It could be okayto use for a tiny little program like this just for demonstration. Just like I mentioned earlier there are too many ways to do this. The original poster was asking how to "pass" arrays in functions. That's why I had to show that you can't pass arrays in c. You pass the address of the first index or the row depending on the dimension of your arrays. Pointers are the way to communicate between functions in c. Global variable are not recommended
kb3292 · Sep 9, 2015 10:36 AM
kb3292 , thanks for explanation. Generally, global variables are bad in all languages.
helpjava11 · Sep 10, 2015 8:27 AM
This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.
Start a New Discussion