Two Dimensional Array Resulting to Garbage value..

Here is the problem:
"create a program that reads student test scores in the range 0 to 100.
The Program will ast the user the size of rows and columns and print the following statistics to two decimal places(use magic formula)
*The table of test scores."

Now i tried creating the code but unfortunately when i pass the value of x to the array. it only displays garbage values. here is my code.

#include<iostream>
#include<iomanip>
using namespace std;

double x;
void main()
{
int ctr, ctc, row, col;
double grades[100][100];
cout<<"Enter the size of the Row: "<<endl;
cin>>row;
cout<<"Enter the size of the Column: "<<endl;
cin>>col;
cout<<"It is a "<<row<<" by "<<col<<" table: "<<endl;
cout<<"Enter "<<row*col<<" Elements"<<endl;
for(ctr=0;ctr<row;ctr++)
{
for(ctc=0;ctc<col;ctc++)
{
do
{
cin>>x;

}
while(!(x>=0 || x<=100));
if(x<0 || x>100)
{
cout<<"Invalid Input\n Please input a valid number"<<endl;
ctc--;
}
}
cout<<endl;
}

for(ctr=0;ctr<row;ctr++)
{
for(ctc=0;ctc<col;ctc++)
{
grades[ctr][ctc]=x;
ctr==row;
ctc==col;
ctr++;
ctc++;

}
cout<<endl;
}


for(ctr=0;ctr<row;ctr++)
{
for(ctc=0;ctc<col;ctc++)
{
cout<<setprecision(5)<<grades[ctr][ctc];
}
cout<<endl;
}

}


PS: I dont know the magic formula so i just used setprecision for the decimal places..

Heres a screen shot of what happened.
http://i190.photobucket.com/albums/z222/penumbra43/error-2.jpg
Last edited on
use [code] tags when posting code.

In your first loop "cin>>x" reads to x and does nothing with it. Future cycles overwrite this value. You should read to grades[ctr][ctc] instead.

Your second for loop expects x to contain all entered values and also does something weird. I have no idea what that was supposed to be.. Anyway, you don't need this loop at all.
Topic archived. No new replies allowed.