Hello all,
I was trying to learn some C programming skills. Presently I am just doing very basic stuff. I created a 2d array, which is defined by the user with scanf. I am able to initialise the array and print the array when this is done in the same function. However i am unable to initialise and print the array when I seperate these two tasks into 2 seperate function.
I have only a limited understanding of how arguments are passed to array(I surprised myself when I actually got my one function to work).
Here is an outline of the code:
Code:
#include<stdio.h>
int print_array();
in_initialise_array();
main(){
int rows;
int columns;
scanf("%d %d", &rows, &columns);
initialise_array(rows, columns);
/*print_array(rows, columns);*/
}
int initialise_array(x, y)
{
char matrix[x][y];
int i, j;
for(i=0;i<x; i++)
for(j=0; j<y; j++)
matrix[i][j]='*';
for(i=0;i<x; i++)
{
for(j=0;j<y;j++)
{
printf("%c ", matrix[i][j]);
}
printf("\n");
}
}
int print_array(x, y)
{
int i, j;
char matrix[x][y];
for(i=0;i<x; i++)
{
for(j=0;j<y;j++)
{
printf("%c ", matrix[i][j]);
}
printf("\n");
}
}
If there are any typos, appologies, I couldnt work out how to copy from my VMware into this message board. I had to type this in manually from my VMWare session
A couple of thing:
1. The argument in the functions(Icalled them x and y) I can call these anything right? C will know in the line:
Code:
print_array(rows, columns)
that x is rows and y is columns?
2. I suspect i dont have to declare the array matrix a second time in print_array().
When I tried to seperate out the 2 function it would not print properly.
Can anyone give me an insight please?