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

  • Thread starter Thread starter kole08
  • Start date Start date
  • Tags Tags
    Matrix Program
Click For Summary

Discussion Overview

The discussion revolves around writing a C program that utilizes a matrix to count the occurrences of numbers within a specified range. Participants are seeking guidance on the implementation details, syntax errors, and overall approach to the problem.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the variable nxm is unnecessary since the total number of values can be calculated from n and m.
  • Another participant points out that the matrix declaration is incorrect due to undeclared variables i and j, which would lead to compilation errors.
  • There are several syntax errors identified in the original code, including issues with printf and scanf statements, which need correction for proper functionality.
  • A later post provides a more complex implementation of the matrix input and counting logic, but it raises questions about clarity and efficiency.
  • Participants highlight the need for proper initialization of the matrix and suggest using placeholder values to avoid confusion.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct syntax errors and improve the matrix handling. However, there are differing opinions on the best approach to implement the matrix and count occurrences, indicating that multiple competing views remain.

Contextual Notes

Limitations include unresolved syntax issues, the need for clearer variable declarations, and potential inefficiencies in the proposed solutions. The discussion does not reach a consensus on the optimal implementation strategy.

Who May Find This Useful

Students learning C programming, particularly those working on matrix manipulation and input/output operations, may find this discussion beneficial.

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 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 21 ·
Replies
21
Views
4K