1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matrix in C program help

  1. Apr 4, 2010 #1
    ok i think i posted this in the wrong area so i hope this is right.

    so I have to write a program using matrix, however i have some of it done, i just want to know if i am on the right track before i continue on with my program.

    here is what i have to do:
    Give an integer matrix array of size n by m, (m and n must be greater than 1 and less than 5) whose contents are numbers from 0 to 20, write a program that ask the users for the values of n, m and nxm integer values (from 0 to 20) and count how many times each different number is repeated in the matrix.

    i have to prompt the user for the values of n and m however they must be greater than one and less than 5

    i also have to prompt the user for nxm with integer values in the range of 0 and 20

    here is what i have so far:

    #include <stdio.h>
    #include <math.h>
    int n;
    int m;
    int nxm;
    int max = 5;
    int min =1;
    int a[j];

    /*input subsection*/

    printf("please input the values of n and m"\, n, m);
    scanf("%d %d &n, &m");

    printf("please input the value of nxm\n", nxm);
    scanf(""%d" , &nxm);

    now i know this is probably wrong because i still have to figure out the ranges of the numbers and all but if someone could help lead me into the right direction that would be great. Also i am not finished working on this too so it may not make since i just want to make sure i am going in the right direction!
  2. jcsd
  3. Apr 4, 2010 #2


    Staff: Mentor

    You don't need a variable nxm. The matrix will have n x m values in it, but the number of values can be calculated once n and m are known.

    In this line: int

    i and j are not declared, so the compiler will generate an error because of this.

    Since you won't know until run time how big the matrix needs to be, one solution would be to define an array with 5 rows and 5 columns, and initialize each element to a value that won't be used, say -1.

    When n and m are known, you could assign the values to the part of the array corresponding to the values for n and m. All values that come from the user will be in the range 1 through 20.
  4. Apr 4, 2010 #3
    also, I'd like to point our that you have several syntax errors in the code. (i numbered the lines)

    the slash in line 3 gives an error. i assume you want \n inside the quotes, because that generates a newline. ex.: printf("foo\nBAR"); foo and Bar would print on different lines. also in 3, you're not outputting variables, so the string should be your only argument.
    4 should be: scanf("%d %d", &n, &m);
    6 is unnecessary as you can do nxm=n*m; (that's what computers are for).
    and in 7 you have an extra quotation mark.
  5. Apr 7, 2010 #4
    there are many typo check them nd then compile
  6. Apr 11, 2010 #5
    Code (Text):

    #include "stdafx.h"
    using namespace std;

    int A[4][4];
    int m,n;

    int _tmain(int argc, _TCHAR* argv[])
        cout<<m<<"x"<<n<<" matrix, enter values"<<endl;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                    for(int l=0;l<n;l++)
                     cout<<"  "<<A[k][l]<<"  ";
                     if(A[k][l]<9){cout<<" ";}
                cout<<"row "<<i+1<<" "<<" col "<<j+1<<":";
                    cout<<m<<"x"<<n<<" matrix, enter values"<<endl;

        int t[32][1];

    int z=0;

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){

                    for(int k=0;k<m;k++){
                        for(int l=0;l<n;l++){

                        cout<<t[z][0]<<" occurs "<<t[z][1]<<" times."<<endl;
                for(int k=0;k<m;k++)
                    for(int l=0;l<n;l++)
                     cout<<"  "<<A[k][l]<<"  ";
                     if(A[k][l]<9){cout<<" ";}

        return 0;
    Last edited: Apr 11, 2010
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Matrix in C program help
  1. C program help (Replies: 5)

  2. C++ program help! (Replies: 1)

  3. C program help needed (Replies: 3)