-
Help With Arrays
/*I want to load arrays and display arrays that will have numbers 1-25 in this format
1 2 3 4 5
6 7 8 9 10
11 12 and so on...
i have got 2 functions
1st one will load arrays n second one will display */
Code:
void loadArray()
{
int r, c, cnt=1;
for( r=0; r<5; r++ )
for( c=0; c<5; c++ )
data[r][c] = cnt++;
}
void displayArray( )
{
int r, c, temp;
cout << "The 5 x 5 2D array:\n\n";
for (r=0;r<5;r++)
{
for(c=0;c<5;c++)
{
cout << " " << data[r][c];
}
}
displayArray();
getchar();
pause();
}
//i want to display arrays when ever i call func DisplayArrays();
-
Well, displayArray is recursive.. with no sign of stopping. Therefore, if you call it once itt'l continue to output the data... forever.
-
so tell me wat to do
n u didnt actually answere my question witch was
how would i display the array in 5x5 format
:confused:
-
Code:
void displayArray( )
{
int r, c, temp;
cout << "The 5 x 5 2D array:\n\n";
for (r=0;r<5;r++)
{
for(c=0;c<5;c++)
{
cout << " " << data[r][c];
}
cout << endl;
}
getchar();
pause();
}
Ooops, sorry about that :) There ya go
-
thx now let me see if it works
it works thx man
-
ok now i am stuck on adding rows n colums
Code:
void sumRows( )
{
int r, c, row;
cout << "Enter row number:\t ";
cin >> row;
for (r=0;r<5;r++)
{
for(c=0;c<5;c++)
{
csum = data[row][c] + data[row][c];
}
}
cout << " "<<csum;
cout << "\n\nSwapping done:\n";
displayArray();
}
codewarrior gives me this error:
illegal operrand
and it is on the semicolon
and i was also having trouble showing the array backwards
(it is not that i dont know, i have done this program b4, but i keep forgetting how i did.)
:D
-
Is the sumRow function supposed to output the sum of a row, or swap elements in that row?
To output the array backwards just do
for (int i = 4; i >= 0; i--)
{
for (int ii = 4; ii >= 0; ii--)
{
cout << data[i][ii];
}
cout << endl;
}
-
the sumRow is suppose to output the sum of the row that is inputted by the user
for example
sum of row 1 is 15
-
Quote:
Code:
for(c=0;c<5;c++)
{
csum = data[row][c] + data[row][c];
}
try this instead csum+=data[row];
or csum=csum+data[row];
also don't forget to declare csum
-
i can seem to get it to work
my
int csum[5]; // it is global
and it is giving me the same error when i tried with all those different methods.
-
if csum is a global array of 5 integers each holding the sum of a row then try this instead
csum[row]=csum[row]+data[row,c];
-
here is the code:
Code:
void sumRows( )
{
int r, c, row;
cout << "Enter row number:\t ";
cin >> row;
for (r=0;r<5;r++)
{
for(c=0;c<5;c++)
{
csum[row]=csum[row]+data[r,c];
}
}
cout << "\n\nSwapping done:\n";
displayArray();
}
here is the error promped by CODEWARrior
Error : illegal implicit conversion from 'int *' to
'int'
!U1_A4_2.CPP line 193 csum[row]=csum[row]+data[r,c];
it gave me the same error when i tried
csum[row]=csum[row]+data[row,c];
and my csum is an array ie
csum[5] // it is global