Matrix in C Program: Count Repeated Numbers | Help & Direction

  • Thread starter Thread starter kole08
  • Start date Start date
  • Tags Tags
    Matrix Program
AI Thread Summary
The discussion focuses on creating a C program to count repeated numbers in an integer matrix defined by user-input dimensions n and m, both between 2 and 4. The user is prompted to enter values for n, m, and the matrix elements, which must be integers from 0 to 20. Several coding errors are identified, including incorrect syntax in printf and scanf statements, as well as the improper declaration of the matrix array. Suggestions include defining a fixed-size array and calculating the total number of elements based on n and m, rather than using a separate variable for nxm. The conversation emphasizes correcting these issues to ensure the program functions as intended.
kole08
Messages
1
Reaction score
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!
 
Physics news on Phys.org
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.
 
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.
 
there are many typo check them nd then compile
 
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:

Similar threads

Replies
9
Views
4K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
4
Views
1K
Replies
1
Views
4K
Replies
1
Views
10K
Back
Top