Help with min and max of letter count array

Hey again guys. I got another noob mistake filled program. I have a program that needs to read a file and out put the occurrence of each letter aswell as show the max and min letters and occurrences of said letters. I've got it to read the file and display the occurrence of each letter but am having trouble setting up the program to get the minimum and maximum letters and numbers. I have looked around for help but majority of the solutions are way more advanced than what is expected of me for my skill level aswell as a lot that are helpful seem to be in c-strings when I can only use the standard c++ string class. Please advise. Thank you in advance.
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  // Lab 2.cpp : Defines the entry point for the console application.


#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


using namespace std;

char mostFrequent(string text);

int main()
{

	
	string letterCount = ("letter_count.txt");

	ifstream inFile;
	char letter;
	int totalLetters[26] = { 0 };
	int n = 0;
	char maxCharacter=0;


	//opens document and reads
	inFile.open("letter_count.txt");

	//Error message if document not found
	if (inFile.fail())
	{
		cout << "Error: letter_count.txt could not be found" << endl;
		system("Pause");
	}


	//Converts all letters to upper case to simplify
	while (inFile.get(letter))
	{
		if (isalpha(letter))
		{
			letter = toupper(letter);
			int i = letter - 'A';
			totalLetters[i]++;
		}
	}




	//array for displaying letters and times of appearence
	for (int i = 0; i < 26; i++)



	{
		cout << "  " << char(i + 'A') << " Appears " << totalLetters[i] << " times" << endl;

	}

	


//function to get max letter and occurence
	char mostFrequent(string text);
	{
		int max = 0;
		int count = 0;
		char maxCharcter;
		for (char q = 'a'; q <= 'z'; q++)
		{
			int count = 0;
			for (int i = 0; i<strlen("letter_count.txt"); i++)
			{
				if (letterCount[i] == q)
					count++;
			}

			if (count>max)
			{
				max = count;
				maxCharcter = q;
			}
		}

		cout << max << endl;
		cout << maxCharcter << endl;
		system("Pause");

	}
	
	return 0;
}


Last edited on
Hi,

The first part of the code is OK. It gives an array of 26 ints that represents the number of occurrences of each letter. So there is no need to re-read the file to get the min and max; just use the array :

1
2
3
4
5
int idMax = 0;
for (int i = 0; i < 26; i++)
    if (totalLetters[idMax] < totalLetters[i])
         idMax = i;
cout << "  " << char(idMax + 'A') << " Appears " << totalLetters[idMax];



Hope tht helps
Thank you so much it helps a lot. Now I got the data to display properly. The only thing is now I need to have atleast one function in this code so I'll probably use what you gave me as a function instead.
Last edited on
I got it, now with functions. Thanks for the help!!!

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

	//call to function max 
	idMax = max(totalLetters, SIZE);
		cout << " The letter that appears the most is: " << char(idMax + 'A') << " Appears " << totalLetters[idMax] << endl;

		//call to function min
		idMin = min(totalLetters, SIZE);
		cout << " The letter that appears the least is: " << char(idMin + 'A') << " Appears " << totalLetters[idMin] << endl;
	


	
	system("Pause");
	return 0;
}

//function for max letter count
int max(int totalLetters[], int SIZE)
{
	int idMax=0;
	
	for (int i = 0; i < 26; i++)
	{

		if (totalLetters[idMax] < totalLetters[i])
			idMax = i;

	}
	return idMax;
}

//function for min letter count
int min(int totalLetters[],int SIZE)
{
	int idMin=0;

	for (int i = 0; i < 26; i++)
	{
		if (totalLetters[idMin] > totalLetters[i])
			idMin = i - 1;
		
	}
	
	return idMin;
}
Last edited on
Topic archived. No new replies allowed.