Hmm... it would be better if you used
either
int exam[100][3];
or
1 2 3
|
int exam1[100];
int exam2[100];
int exam3[100];
|
not both.
In the first case, exam[i][j] indicates the number of the (i+1)-th row and (j+1)-th column, where 0<=i<=99 and 0<=j<=2. In the second, well, exam1[i] is the (i+1)-th row of the first column (as you have exam
1), exam2[i] is the (i+1)-th row of the second column etc... and 0<=i<=99.
I see here that you input the data from file to
exam
and then try to place the data from
exam
to
exam1
,
exam2
and
exam3
. You could avoid this copying if you only used one way to represent your data.
Another thing is that the copying itself isn't done correctly.
here:
1 2 3
|
exam1[][]=exam[100][3];
exam2[][]=exam[100][3];
exam3[][]=exam[100][3];
|
maybe you meant something like:
1 2 3
|
exam1[][]=exam[100][0];
exam2[][]=exam[100][1];
exam3[][]=exam[100][2];
|
but neither does what you expect. If you want to copy the data copy each element separately with a loop like this:
1 2 3 4 5 6
|
for (int i=0; i<100; i++)
{
exam1[i]=exam[i][0];
exam2[i]=exam[i][1];
exam3[i]=exam[i][2];
}
|
But I would suggest avoid storing data in two places and use only one of the above mentioned ways.
I also noticed the return statements of your functions:
return exam[i][j];
for read_file_in_array
and
return exam1[i],exam2[i],exam3[i];
for calculate_total
Both are syntactically correct but the first one doesn't do what you want and the second one is unnecessary (if I understand why you are using it). Let me elaborate on these two...
For the first:
What value do you want calc_tot to hold?? Is it the number of the records in your file? Then perhaps read_file_in_array should return i+1 or i or i*3+j, I don't know, you figure this one out, but exam[i][j] definitely is not what you want. This is the
value of the element in the (i+1)-th row and (j+1)=th column...
For the second:
Are you doing this because you want the changes you made to exam1, exam2 and exam3 to be actually stored? This is not necessary. The values are stored. Also
exam1[i],exam2[i],exam3[i]
evaluates to
exam3[i]
, so you actually return only the last one.
That's all I can see for the time being... Fix these and let me know where you get after that.
EDIT: Also heed Duoas' suggestion and avoid putting ';' in the head of your loops, it is not wrong syntax, but it doesn't do what you want:
You use it like:
1 2 3 4
|
for (/*...*/) ; //<- this ';' musn't be put here. This here is an empty loop...
{ //this is not
//... //part of the loop
} //it will only run once!
|
And it should be like:
1 2 3 4
|
for (/*...*/) //<- do it like this, no ';'. Now, this is the head of your loop...
{ //and this
//... //is the body
} //of your loop!
|
EDIT 2: I just took a look at your global declarations and your main function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int read_file_in_array(int exam[100][3]);
int calculate_total(int exam1[], int exam2[], int exam3[]);
// function that calcualates grades to see how many 90,80,70,60
void display_totals();
int exam1[100][3];// array that can hold 100 numbers for 1st column
int exam2[100][3];// array that can hold 100 numbers for 2nd column
int exam3[100][3];// array that can hold 100 numbers for 3rd column
int main()
{
int go,go2,go3;
go=read_file_in_array;
go2= calculate_total(exam1[],exam2[],exam3[]);
go3=display_totals;
cout << go,go2,go3;
return 0;
}
|
Functions read_file_in_array and calculate_total return an int each. Function display_totals returns void (i.e. it doesn't return anything). So, when you call display_totals call it like:
display_totals(); //<- don't forget the parentheses!!!
Also, it would be a good idea to make your other functions void, since (if I understand correctly) they don't have to return a value. Then you don't need the variables go, go2 and go3.
Another thing I noticed is that you declare calculate_total to take three arguments and when you call it, the arguments you pass are global variables. You could just not pass them at all! Consider the following examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
using namespace std;
int global_int;
void my_function();
int main()
{
global_int=10;
my_function();
global_int=20;
my_function();
cout << "hit enter to quit..." << endl;
cin.get();
}
void my_function()
{
//see, you don't have to pass global_int as an argument
//coz it's a global variable. You can simply access it directly
cout << "your int is: " << global_int << endl;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
using namespace std;
void my_function(int some_int);
int main()
{
int local_int;
local_int=1;
my_function(local_int);
local_int=2;
my_function(local_int);
cout << "hit enter to quit..." << endl;
cin.get();
}
void my_function(int some_int)
{
//now, since local_int is declared inside main, you can't
//access it directly from here... You have to pass it as
//an argument to your function
cout << "your int is: " << some_int << endl;
}
|
If you want more info about multi/single dimensional arrays and/or functions you can have a look at these links:
http://cplusplus.com/doc/tutorial/arrays/
http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/
One more (
very important) thing:
im trying to read a file with 3 columns and each column has a 100 numbers with decimal places. |
Use
double
instead of
int
like:
1 2 3
|
double exam1[100];
double exam2[100];
double exam3[100];
|
or
double exam[100][3];