-
Array problem
Hi I am making a program that uses multi dimensional arrays to display numbers from 1 to 54 in 6 rows and 9 columns. Here is my code:
Code:
#include <stdio.h>
#define MAXCOL 6
#define MAXROW 9
void fillArray(int array[][MAXROW]);
void printArray(int array[][MAXCOL]);
int main()
{
int array[MAXROW][MAXCOL];
fillArray(array);
printArray(array);
return 0;
}
void fillArray(int array[][MAXROW])
{
/* fill the array with the numbers 1 to 54
in ascending order going from left to right
then top to bottom. Therefore 1 will be in
the top left hand corner, 6 in the top right
hand corner, 49 in the bottom left corner
and 54 in the bottom right hand corner.
*/
}
void printArray(int array[][MAXCOL])
{
int row, col;
int size = 1;
printf("\n");
for (row=0; row<=MAXROW; row++)
{
for (col=0; col<=MAXCOL; col++)
{
array[row][col]=size;
printf("%9d",array);
}
printf("\n");
}
}
It gives me an error in line 13: array.c(13): error #2140: Type error in argument 1 to 'fillArray'; found 'int [6] *' expected 'int [9] *'.
I am not sure why? I have tried changing the MAXCOLS AND MAXROWS around but it's not doing anything except giving more errors.
Please help.
-
Shouldn't this:
void fillArray(int array[][MAXROW])
read like this:
void fillArray(int array[][MAXCOL])
?
-
void fillArray(int array[][MAXROW]);
void printArray(int array[][MAXCOL]);
Why aren't these both [][MAXCOL];
-
OK, I have changed the code to look like this:
Code:
#include <stdio.h>
#define MAXCOL 6
#define MAXROW 9
void fillArray(int array[][MAXCOL]);
void printArray(int array[][MAXCOL]);
int main()
{
int array[MAXROW][MAXCOL];
fillArray(array);
printArray(array);
return 0;
}
void fillArray(int array[][MAXCOL])
{
/* fill the array with the numbers 1 to 54
in ascending order going from left to right
then top to bottom. Therefore 1 will be in
the top left hand corner, 6 in the top right
hand corner, 49 in the bottom left corner
and 54 in the bottom right hand corner.
HINT. While there are some errors in the
printArray function it should give you a good
idea as to how to complete this function.
*/
}
void printArray(int array[][MAXCOL])
{
int row, col;
int size = 1;
printf("\n");
for (row=0; row<=MAXROW; row++)
{
for (col=0; col<=MAXCOL; col++)
{
array[row][col]=size;
printf("%9d",array[row][col]);
}
printf("\n");
}
}
However, it is only printing an array of 1s something like:
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
I need it to print an array for numbers 1 to 54 in 9 rows and 6 columns. Thanks for your inputs.
-
Never mind, don't bother replying. I got it to work.
Thanx anyway.
-
> for (row=0; row<=MAXROW; row++)
Arrays run from 0 to N-1, so this loop should be
for (row=0; row<=MAXROW-1; row++)
or more usually
for (row=0; row<MAXROW; row++)
The same goes for the columns as well.
-
You're never modifying the size variable in your printArray function.
Either you do this:
Code:
array[row][col]=size++;
OR
Code:
array[row][col]=row+col;