Putting numbers in a Matrix.

This is a discussion on Putting numbers in a Matrix. within the C Programming forums, part of the General Programming Boards category; hello, I want to build a function in C that create a matrix with values of number in it. the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    12

    Putting numbers in a Matrix.

    hello,

    I want to build a function in C that create a matrix with values of number in it.
    the user's input is the dimensions of the matrix (height and width) and then he is entering the matrix.
    for example, the user enter a matrix of 4 and 5 (4 is the number of columns and 5 is the number of rows).
    then he is entering the following matrix:
    -3 1 -4 2 -5
    3 5 -8 8 10
    6 -5 20 -3 4
    -7 3 2 16 -5

    building the matrix by the number of columns and rows i know how to do. but how can i scan the user input of the matrix and actually put each number in the right place in the matrix i create?
    to be more specific, how does the scanf order should look, and how can i run it in loops in order to put each number in the right place? (for example how can i scan the number 1 in the user input and then put it the cell mat[0][1]?)
    thank you for your help in advance...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Can you write loops that would print a matrix? Is there a difference between those loops and the one you want to use to read a matrix?

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Can you write loops that would print a matrix? Is there a difference between those loops and the one you want to use to read a matrix?
    I need to create a matrix that looks like the user input. the just enter the example i showed above of how the matrix should look, and i need to scan it and put it in an actuall matrix i create by the user inputs of hight, width and the matrix example that user entered. the user dosent actually insert a matrix. i need to transform it into a matrix.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mfm12002 View Post
    I need to create a matrix that looks like the user input. the just enter the example i showed above of how the matrix should look, and i need to scan it and put it in an actuall matrix i create by the user inputs of hight, width and the matrix example that user entered. the user dosent actually insert a matrix. i need to transform it into a matrix.
    I am aware of all of that. What I am asking is, if I handed you this:
    Code:
    int a_matrix[3][7] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15},{16,17,18},{19,20,21}};
    and asked you to print it out, could you do so? (I'm also trying to hint that this is the "same" problem as what you're attempting.)

    (And now that I look at it, how you do this will depend somewhat on how you create your matrix, of which there are three ways to do that I can think of right now, so we'd have to get that straight as well.)

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    I am aware of all of that. What I am asking is, if I handed you this:
    Code:
    int a_matrix[3][7] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15},{16,17,18},{19,20,21}};
    and asked you to print it out, could you do so? (I'm also trying to hint that this is the "same" problem as what you're attempting.)

    (And now that I look at it, how you do this will depend somewhat on how you create your matrix, of which there are three ways to do that I can think of right now, so we'd have to get that straight as well.)
    I'll show you how the proggrram should look like on the screen:

    Enter dimensions (height,width) of the matrix:
    4 5
    Enter the matrix:
    -3 1 -4 2 -5
    3 5 -8 8 10
    6 -5 20 -3 4
    -7 3 2 16 -5

    the is what the user is entering. I need to transform it into my own created matrix...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    My goal, here, is to get you to have the answer without having me just type it out.

    So let's look at this another way: Your numbers are coming in as -3, 1, -4, 2, -5, 3, 5, -8, etc. What subscript(s) do you want to put that -3 in? What subscript(s) do you want to put that 1 in? And so on -- what pattern do you see?

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    My goal, here, is to get you to have the answer without having me just type it out.

    So let's look at this another way: Your numbers are coming in as -3, 1, -4, 2, -5, 3, 5, -8, etc. What subscript(s) do you want to put that -3 in? What subscript(s) do you want to put that 1 in? And so on -- what pattern do you see?
    i'll show you what i was thinking of, tell me if i'm the right way:
    Code:
    void main(){
    int i,int j, row, col,num,mat[100][100];
    printf("Enter dimensions (height,width) of matrix:\n");
    scanf("%d%*c%d",&col,&row);
    printf("Enter the matrix:\n");
    for(i=0;i<row;i++)
      for(j=0;j<col;j++){
          scanf("%d",&num);
          mat[i][j]=num; }
    printArray(mat);
    }
    
    void printArray(int A[][]){
    	int i,j;
    	for(i=0;i<100;i++)
                 for(j=0;j<100;j++)
    		printf("%d",A[i][j]);
    	printf("\n");
    	return;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mfm12002 View Post
    i'll show you what i was thinking of, tell me if i'm the right way:
    Code:
    void main(){
    int i,int j, row, col,num,mat[100][100];
    printf("Enter dimensions (height,width) of matrix:\n");
    scanf("%d%*c%d",&col,&row);
    printf("Enter the matrix:\n");
    for(i=0;i<row;i++)
      for(j=0;j<col;j++){
          scanf("%d",&num);
          mat[i][j]=num; }
    printArray(mat);
    }
    
    void printArray(int A[][]){
    	int i,j;
    	for(i=0;i<100;i++)
                 for(j=0;j<100;j++)
    		printf("%d",A[i][j]);
    	printf("\n");
    	return;
    }
    That looks eminently reasonable for filling in a matrix row by row.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    That looks eminently reasonable for filling in a matrix row by row.
    first of all thanks for helping
    but for some reason i can't outcome the user input. he is typing something like a mention earlier but in the way i did it, it only scan the first number and put it mat[0][0]. I need to outcome the spaces and scan the other numbers follwing the first number and also outcome the rows. the users input contains numbers seprated by spaces, and new rows (\n).

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mfm12002 View Post
    first of all thanks for helping
    but for some reason i can't outcome the user input. he is typing something like a mention earlier but in the way i did it, it only scan the first number and put it mat[0][0]. I need to outcome the spaces and scan the other numbers follwing the first number and also outcome the rows. the users input contains numbers seprated by spaces, and new rows (\n).
    If by "outcome" you mean "output", then yes you will need to put a space in your print statement if you want to see spaces between the numbers, and you will need to put your new-line statement inside your row loop as well if you want blank lines between rows.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I agree with tabstop. This is good work, you just need to edit your printouts a little bit. Other than that, the logic is good.

  12. #12
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    If by "outcome" you mean "output", then yes you will need to put a space in your print statement if you want to see spaces between the numbers, and you will need to put your new-line statement inside your row loop as well if you want blank lines between rows.
    i don't want to print the matrix with spaces. The user gives me a input the looks like
    2 0 1
    3 4 5
    7 4 3
    and i need to scan it and put each numberin the proper place in the matrix.
    the question is, should i run a scanf command in loop inside the loops i did for searching the matrix, for example:
    Code:
    While {
    scanf("%d%*c.......................................

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mfm12002 View Post
    i don't want to print the matrix with spaces. The user gives me a input the looks like
    2 0 1
    3 4 5
    7 4 3
    and i need to scan it and put each numberin the proper place in the matrix.
    That's what you posted, was code that took exactly that input and put it exactly where it was supposed to. What do you think is wrong with it?

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    That's what you posted, was code that took exactly that input and put it exactly where it was supposed to. What do you think is wrong with it?
    the problem is that when i print the matrix i created it looks like this
    3343534636346455634564563456
    5643654564353245532..........
    it looks like i didn't scaned the user's input or the matrix i try to create contain junk instead of the numers the users wanted me to insert....

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, that's just because you are printing them that way. It has nothing to do with how they are stored internally, otherwise you wouldn't be able to print any values by using your two for loops.

    In order to get what you want modify your print function to this:

    Code:
    void printArray(int A[][]){
    	int i,j;
    	for(i=0;i<100;i++){
                 for(j=0;j<100;j++)
    		printf("%d ",A[i][j]);
                 printf("\n");
            }
    	printf("\n");
    }
    Notice the space after %d in printf and the printf("\n") new line after each inner for loop (each row) is printed.

    Also there is no need to return at the end of a void function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 10:00 PM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Replies: 1
    Last Post: 03-06-2006, 06:57 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21