Reputation: 29
So, what I'm trying to do here is printout a matrix or simply just an array. Now, the input file has a set of integers. My problem is that when I output the matrix it's a bunch of random numbers. It's single digits too so I don't think it's an address like those associated with pointers. The first integer in the input file is suppose to be the array length or as I call it here "col".
#define MAX 10
int main()
{
int col,row; /* array dimensions */
int i,j,k; /* counter variables */
int n; /* temporary variable for elements*/
int temp[MAX*MAX]; /* temporary array for row counting */
int counter=0; /* counter variable for number of rows */
int matrix[MAX][MAX]; /* input matrix */
FILE *in; /* pointer for input file of elements */
/* INPUT */
printf("\nPlease enter your desired number of columns and the elements of your matrix.\n");
fscanf(in,"%d",&temp[0]); /* Enter matrix length */
col = temp[0]; /* assign integer value to col variable */
if( col < 1 || col > MAX){ /* Standard Error for invalid input */
fprintf(stderr,"\nInvalid Length\n");
exit(1);
}
/* PROCESSING */
while(fscanf(in,"%d",&n)!=EOF){ /* determine number of rows */
temp[counter++]=n;
}
if(counter % col == 0){ /* standard error for exceeding/lesser amount of input*/
fprintf(stderr,"\nInvalid amount of input\n");
exit(1); /* quits out the program for error */
}
else{
row = counter / col; /* determine number of rows */
}
for(i = 0; i < row; i++){ /* read the elements */
for(j = 0; j < col; j++){
for(k=1; k < counter; k++){
matrix[i][j] = temp[k];
}
}
}
/* OUTPUT */
fprintf(stdout,"\nHere is your matrix:"); /* Matrix Header */
for(i=0; i < row; i++){ /* print out the input matrix */
fprintf(stdout,"\n");
for(j = 0; j < col; j++){
fprintf(stdout,"%3d ",matrix[i][j]);
}
}
fprintf(stdout,"\n");
The INPUT: 2 1 2 3 4 5 6
The OUTPUT: Please enter your desired number of columns and the elements of your matrix.
Here is your matrix:
6 6
6 6
6 6
^ Now I don't know if our UNIX system is telling me something about myself or what. I'm scared now.
EDIT: Okay, I removed the comments between the lines of code. EDIT: I compiled and tested the program.
Upvotes: 0
Views: 87
Reputation: 126
Problem is here, Every matrix i,j will get the same k.
for(i = 0; i < row; i++){ /* read the elements */
for(j = 0; j < col; j++){
for(k=1; k < counter; k++){
matrix[i][j] = temp[k];
}
}
}
change it to,
int count=0;
for(i = 0; i < row; i++){ /* read the elements */
for(j = 0; j < col; j++){
count++;
matrix[i][j] = temp[count];
}
}
Upvotes: 2
Reputation: 871
If that is the complete program, it seems like you never fopen()ed the file.
for(i = 0; i < row; i++){ /* read the elements */
for(j = 0; j < col; j++){
for(k=1; k < counter; k++){
matrix[i][j] = temp[k];
}
}
}
There's your problem. How many dimensions does the matrix have? How many for loops are you using? Exactly.
Since for every iteration of the innermost loop i and j stay the same, while k goes from 1 to counter, you're rewriting matrix[i][j] over and over, until you always reach the same end value of k. Your code is equivalent to:
for(i = 0; i < row; i++){ /* read the elements */
for(j = 0; j < col; j++){
matrix[i][j] = temp[counter-1];
}
}
Upvotes: 1