metrix

hello , i need some help with this code .
im trying to program a metrix of 5x5, that get from the user a letter, than the
program export the column index which the letter shows in the most.
if the letter has not shown at all then the program export a message about it.
i cant go through the metrix in the begining to check it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <iostream>

using namespace std;

const int ROWS = 5;
const int COLS = 5;

void main()
{
	char chars[ROWS][COLS] = { {'a','b', 'c', 'd', 'e'}, {'d','e','f','g','k'}, {'s','h','m','s','h'},
	{ 's','f','g','k','e'}, {'s','t','s','t','n'} };

	char orig;
	char freqLetter;
	char letter;
	int i, j, max=0;

	cout << "Please enter a letter " << endl;
	{
		cin >> letter;
	}

	cout << "chars" << endl;
	for (int i = 0; i < ROWS; i++)
	{
		cout << "MATRIX" << ": " << i + 1 << endl;
		for (int j = 0; j < COLS; j++)
			cout << chars[i][j] << " ";
		cout << "\n";
	}
	
	for (i = 0; i < COLS; i++)
	{
		for ( freqLetter = 0,j = 0; j < ROWS ; j++)
		{
			orig = chars[j][i];
			if (letter == orig)
			{
				cout << orig << endl;
				cout << j+1 << "  " << i+1 << endl;
			
			}
			
		}
		
		
	}
	
		

	system("pause");
}
Should be
int main()
not
void main()

Apart from that it's fine. The following is the output in C++ shell:
Please enter a letter 
s
chars
MATRIX: 1
a b c d e 
MATRIX: 2
d e f g k 
MATRIX: 3
s h m s h 
MATRIX: 4
s f g k e 
MATRIX: 5
s t s t n 
s
3  1
s
4  1
s
5  1
s
5  3
s
3  4


Not sure what you mean by
i cant go through the metrix in the begining to check it.


What is the problem? At present it displays all positions that this letter is in.
Last edited on
the problem is that i need to program in which columns the letter shows the most .
if the letter has not shown at all than also print a message that says that.
When
letter == orig
you can increase frequency by 1.

If you also keep a tally of max_frequency (initially 0) and max_col (initially anything) then when frequency for that column exceeds max_frequency you can update them.

At the end ...
- if max_frequency is still 0 then print out that this letter wasn't found
- otherwise you can print out max_col

Over to you to code.
#include <iostream>

using namespace std;

const int ROWS = 5;
const int COLS = 5;

void main()
{
char chars[ROWS][COLS] = { {'a','b', 'c', 'd', 'e'}, {'d','e','f','g','k'}, {'s','h','m','s','h'},
{ 's','f','g','k','e'}, {'s','t','s','t','n'} };

char orig;
char letter;
int i, j,freqLetter ;


cout << "Please enter a letter " << endl;
{
cin >> letter;
}

cout << "chars" << endl;
for (int i = 0; i < ROWS; i++)
{
cout << "MATRIX" << ": " << i + 1 << endl;
for (int j = 0; j < COLS; j++)
cout << chars[i][j] << " ";
cout << endl;
}

int max = 0;
for (i = 0; i < COLS; i++)
{

for (freqLetter = 0, j = 0; j < ROWS ; j++)
{
orig = chars[j][i];
if (letter == orig)
{
cout << orig << endl;
cout << j+1 << " " << i+1 << endl;
freqLetter++;
}

}
if (freqLetter > max)
{
max =i+1;
}


}
cout << endl;
cout << max << endl;


system("pause");
}

work
Topic archived. No new replies allowed.