-
Array Function
I've asked about this program in the past but have just can't figure out what to do.
I can't figure out how to make an array in a function and call it back to a different procedure. In this program I want to be able to fill an array for the Lowest Grade and return it back. I also have to do the same with the average, but if I could figure that out if I could figure out how to do it with the Lowest.
Only thing I'm even sure works is getting the 4 scores, then I don't know if I have to make a new array inside of the findLowest function or what.
Objective is to enter 4 test scores for 3 students, which is done in getData no problem but then I need to figure out of those scores which one is the lowest in the "int findLowest" function and then somehow put those into an array and return the values in the array to display in the display procedure along with the average.
I need some test format or something just plain lost tried a few things can't figure out something that works :(
Code:
Look at code below please
-
Okay what can I do to return these arrays properly, at the moment it is taking the lowest of the first row and it wont change for any other.
And the average is doing the same.
Code:
//Low Score
#include<iostream>
#include<iomanip>
using namespace std;
void getData(int score[][4], int rows)
{
for(int R=0; R < rows; R++)
{
for(int C=0; C < 4; C++)
{
do
{
cout << "Enter test score " << (C+1) << " for student " << (R+1) << ": ";
cin >> score[R][C];
}while(score[R][C] < 0 || score[R][C] > 100);
}
cout << endl;
}
}
int findLowest(int score[][4], int rows)
{
const int LOWS = 3;
int arr[LOWS];
for(int R=0; R < rows; R++)
{
int low = 100;
for(int C=0; C < 4; C++)
{
if (score[R][C] < low)
low = score[R][C];
}
arr[R] = low;
return *arr;
}
}
double calcAvg(int score[][4], int rows, int low)
{
const int AVGS = 3;
double arr[AVGS];
double avg;
for(int R=0; R < rows; R++)
{
int TOT=0;
for(int C=0; C < 4; C++)
{
TOT += score[R][C];
}
avg = (TOT - low) / 3.0;
arr[R] = avg;
return *arr;
}
}
void display(int score[][4], int rows, int low, double avg)
{
for(int R=0; R < rows; R++)
{
for(int C=0; C < 4; C++)
{
cout << "Score " << (C+1) << " for student " << (R+1) << ": " << score[R][C] << endl;
}
cout << "Dropped Grade: " << low << endl;
cout << "Average: " << avg << endl;
cout << endl;
}
}
int main()
{
int sc[3][4], low;
double avg;
getData(sc, 3);
low = findLowest(sc, 3);
avg = calcAvg(sc, 3, low);
display(sc, 3, low, avg);
return 0;
}
-
Code:
int findLowest(int score[][4], int rows)
{
const int LOWS = 3;
int arr[LOWS];
for(int R=0; R < rows; R++)
{
int low = 100;
for(int C=0; C < 4; C++)
{
if (score[R][C] < low)
low = score[R][C];
}
arr[R] = low;
return *arr;
}
}
What do you think the red code is doing? Because it probably isn't doing what you THINK it's doing.
Also, you are returning before you have checked all your data - check where your return statement is.
--
Mats
-
Well i was hoping that
would assign the 0,1,2 addresses in he array with the lowest grade for each student, but as for the
I was just trying to figure out a way to return something to point to every spot of the array, but as I said I don't know.
But I do see the return should be after the following brace, but even then still doesn't work properly.
-
staring at this now
Code:
int findLowest(int score[][4], int rows)
{
const int LOWS = 3;
int arr[LOWS];
for(int R=0; R < rows; R++)
{
int low = score[R][0];
for(int C=0; C < 4; C++)
{
if (score[R][C] < low)
low = score[R][C];
}
arr[R] = low;
}
return *arr;
}
-
So, do I have it right you want to return an array of the lowest values in each row?
Let's get one thing straight immediately: You can not return an array from a function. You have three choices:
1. Pass in another array to be filled in by the function.
2. Somehow modify the original array (e.g. sort the rows in falling/rising order) - this may not be the right thing for your program, but it can work sometimes.
3. Allocate memory in the function and return the POINTER to the allocated array storage. Note that whilst you can make the compiler return the address of a local variable like the "arr" variable in your code, the memory used by the array will be "lost" when you leave the function - so the next function called will most likely overwrite your arr variable with garbage. Never return the address of a local variable or array.
--
Mats
-
How would I go about?
"1. Pass in another array to be filled in by the function."
If you have an example.
-
Roughly the same way as your getData function. Obviously, you don't need a 2D array to get the lowest single value for each row.
--
Mats
-
So would i have to enter different parameters? I'm just having trouble wrapping my brain around it.
The function will have to first test the row then take that and fill it in the array like I atempted above but does that mean to make this new array I have to make a new one in main and in the function? How to make this new array just is hard for me to visualize at the moment.
-
You are already calling a function getData to fill in a 2D array. It should be fairly straightforward to translate the way that works into a function findLowest() that takes a 1D array to be filled in based on the 2D array passed in.
Try to write some code, rather than overthinking it.
--
Mats
-
I guess I'm not sure how to pass the 2D array in.