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

In summary, the code tries to read in n, m, and nxm values from the user, and calculates the matrix entries for k and l where A[k][l] is the value of k repeated l times in the matrix.
  • #1
kole08
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!
 
Physics news on Phys.org
  • #2
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
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
there are many typo check them nd then compile
 
  • #5
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:

1. How do I declare a matrix in a C program?

In C, a matrix is declared using the syntax data_type array_name[row_size][column_size];. For example, to declare a matrix of integers with 3 rows and 4 columns, we would use int matrix[3][4];.

2. How do I input values into a matrix in a C program?

To input values into a matrix in C, we can use nested for loops. The outer loop will iterate through the rows, and the inner loop will iterate through the columns. Within the nested loops, we can use scanf() to input values from the user into each element of the matrix.

3. How do I perform operations on matrices in a C program?

To perform operations on matrices in C, we can use nested for loops to iterate through each element of the matrices. We can then use arithmetic operators such as +, -, and * to perform addition, subtraction, and multiplication respectively. It is important to ensure that the dimensions of the matrices are compatible for the desired operation.

4. How do I print a matrix in a C program?

To print a matrix in C, we can use nested for loops to iterate through each element of the matrix. Within the nested loops, we can use printf() to print the elements in a formatted manner. For example, we can use %d to print integers and %f to print floating-point numbers.

5. How do I find the transpose of a matrix in a C program?

To find the transpose of a matrix in C, we can use nested for loops to iterate through each element of the matrix. Within the nested loops, we can swap the elements at positions (i, j) and (j, i) to obtain the transpose. It is important to keep track of the dimensions of the original and the transpose matrix to avoid any errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
884
  • Engineering and Comp Sci Homework Help
Replies
4
Views
927
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top