• Support PF! Buy your school textbooks, materials and every day products Here!

Matrix in C program help

  • Thread starter kole08
  • Start date
  • #1
1
0
hello
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!
 

Answers and Replies

  • #2
33,496
5,188
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
a[j];

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.
 
  • #3
17
0
also, I'd like to point our that you have several syntax errors in the code. (i numbered the lines)

1: /*input subsection*/
2:
3: printf("please input the values of n and m"\, n, m);
4: scanf("%d %d &n, &m");
5:
6: printf("please input the value of nxm\n", nxm);
7: scanf(""%d" , &nxm);
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.
 
  • #4
2
0
there are many typo check them nd then compile
 
  • #5
459
0
Code:
#include "stdafx.h"
using namespace std;

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

int _tmain(int argc, _TCHAR* argv[])
{
	cout<<"M:";
	cin>>m;
	cout<<"N:";
	cin>>n;
	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<<endl;
			}
			cout<<"row "<<i+1<<" "<<" col "<<j+1<<":";
			cin>>A[i][j];
			if((i<m)||(j<n)){
				system("cls");
				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++){
		t[z][1]=0;

				for(int k=0;k<m;k++){
					for(int l=0;l<n;l++){
						
						if(A[i][j]==A[k][l])
						{
							t[z][0]=A[k][l];
							t[z][1]++;
						
						}
					}
		}

					cout<<t[z][0]<<" occurs "<<t[z][1]<<" times."<<endl;
				z++;
	}
	}
			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<<endl;
			}

	system("PAUSE");
	return 0;
}
 
Last edited:
Top