Jump to content

two-dimentional arrays allowing for user input

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
Hi there. I'm trying to make a program that allows the user to enter a 3x3 matrix but I can't even get the input right at the moment :S

Heres what I've done so far:
int main (void) {

	int status=0, i=0, j=0;

	int x[3][3];

	char line[100];

    

	      printf("Enter 3 x 3 matrix:\n");

		  

		      for(i=1; i<=3; i++) {

				  printf("Enter points of row %d:\n", i); 

				  for(j=0; j<=2; j++) {

					  while(1) {

					  

				      fgets(line, sizeof(line), stdin);

                      status = sscanf(line, "%d", &x[i][1+j]);

				      if (status==0) printf("Invalid point.\n");

					  else break;

				  }

			  }

		  }                   	  

	

	      printf("Data input:\n\n");

	     

		  for(i=1; i<3; i++) { 

			  for(j=0; j<2; j++) {

			  printf("%d\n", x[i][1+j]);

			  }

		  }

  

  return 0;

}

It needs to calculate the transpose of the matrix too, but actually getting the thing to input properly is my major concern right now.

Help please!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The indices on your array are from 0-2, not 1-3.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
lol cheers dude. Thought it was something stupid :)