#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void fill(int data[8][8]) {
srand(time(0));
for (int x=0; x<8; x++){
for (int y=0; y<8; y++)
data[x][y] =(rand()%10);
}
}
void data(int data[8][8]){
for (int x=0; x<8; x++){
for (int y=0; y<8; y++){
cout<<data[x][y];
if (y<7) cout<<" ";
}
cout<<endl;
}
}
void row(int data[8][8]){
for (int x=0; x<8; x++){
int total=0;
for (int y=0; y<8; y++) total += data[x][y];
cout<<"The sum of row "<<x+1<<" is "<<total<<"."<<endl;
}
}
void column(int data[8][8]){
for (int x=0; x<8; x++){
int total=0;
for (int y=0; y<8; y++) total += data[x][y];
cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
}
}
void diagonal(int data[8][8]){
int total=0;
int sum=0;
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
if (i==j) total += data[i][j];
if ((i+j)==7) sum += data[i][j];
}
}
cout<<"The sum of the diagonal "<<total<<"."<<endl;
cout<<"The sum of the diagonal "<<sum<<"."<<endl;
}
int main(){
int a[8][8];
fill(a);
data(a);
row(a);
column(a);
diagonal(a);
return 0;
}
This is the error i get.
sija8.cpp: In function âvoid column(int (*)[8])â:
sija8.cpp:35: error: name lookup of âyâ changed for ISO âforâ scoping
sija8.cpp:35: note: (if you use â-fpermissiveâ G++ will accept your code)
Now i put semicolon after the second for loop to fix it, but that messes up the program and messes up the order it needs to be in.
Your variable y is not defined in that scope. Remember that your for loop on line 34 ends in the same line and any variable you declare in there is only available in that loop.
Here I modified the indentation to aid legibility.
1 2 3 4 5 6 7 8 9 10
void column(int data[8][8])
{
for (int x=0; x<8; x++)
{
int total=0;
for (int y=0; y<8; y++)
total += data[x][y];
cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
}
}
At line 6 variable int y is declared inside the for loop. It exists only within the scope of that loop, that is until the end of line 7.
Then on line 8 an attempt is made to use that variable y, but it no longer exists, as it is now out of scope.
The proper solution depends upon what the is intention, what is the code supposed to achieve?
Possibly this might be correct:
1 2 3 4 5 6 7 8 9 10
void column(int data[8][8])
{
for (int y=0; y<8; y++)
{
int total=0;
for (int x=0; x<8; x++)
total += data[x][y];
cout<<"The sum of column "<< y+1 <<" is "<<total<<"."<<endl;
}
}