how do I subtract the ascii value of 'A' from a string array?

closed account (zNASE3v7)
I am trying to perform a count of occurrence of the letters of various string arrays that are passed from reading in a file:
I was given the following example but I keep getting errors in building:

/*You can use a 26 element integer array to keep track of the letter counts by subtracting ‘A’ from the Ascii value of the character.Like,
letterCount[letter – ‘A’] = letterCount[letter – ‘A’] + 1;*/

So here is what I tried and it still gives error after error:
const int letter = { ' ' };

for (int count = 0; count < SIZE; count++)
{
if (count == letter)
{
Alpha_Array++;
}
else if (count > letter)
{
int letterCount[letter] - 65] = letterCount[[letter] – 65] + 1;
high == letterCount[0];
}
else
{
low = letter;
}
}
return high;

I am attempting to get the highest occurrence in this loop.
Can somebody please advise...
> I keep getting errors in building:
> it still gives error after error:
but for some reason you though that it was not relevant to tell us the errors, just that there are errors.
And also you wouldn't give an snip that we could compile and get those errors.


I don't understand your snip at all.
¿where are the strings that you want to count the frequency?
¿what's the purpose of `count'? (because it is not counting)
¿why is `letter' an space?

I'll suggest you to do it in two pass.
1
2
freq = get_frequencies(word);
max_element(freq);
closed account (zNASE3v7)
@ne555
Thank you for reviewing and giving support...I am actually reading data in from a file and the file is passed through a function that converts all the alpha characters to Uppercase, then it checks in the next function if isalpha, and stores the chars it sees in the array that this function testing for the high occurrences recieves. I am pasting the entire program here and if yu would like to reply to the mess I have I will hope you are blessed for days to come.
So here it is...(I have some of the stuff commented out for testing and debugging, and there are some comments I made to myself due to my jumbled confusion...)

//This program is written to perform: readin from a file, and checking the
//number of alpha characters, using a pointer and forcing the characters to
//uppercase and documenting the highest and lowest occurances of the alpha
//characters found in the file.

#include<iostream>
#include<cctype>
#include<string>
#include<fstream>
#include<locale>
using namespace std;

//function prototypes
void Make_Uppercase(string &Str_Lines);
void IncrementLtrs(string Str_Lines, int[]);
//int Hi_Occur(int Alpha_Array[], int &high, int &low);
//int Lo_Occur(int Alpha_Array, int &high, int &low);

//*********************************
// Function main *****
//*********************************
int main()
{
//int high;
//int low;
const int SIZE = 26;
int Alpha_Array[SIZE] = { 0 }; //alpha character array for storing converted alpha Uppercase
string Str_Lines; //declaration for the varaible to store the string lines from the file
ifstream readinFile; //creates the filestream object and link to work with the file.

readinFile.open("letter_count.txt"); //opens the file

if (readinFile)
{
//loops until all lines of the file are read in.
while (getline(readinFile, Str_Lines))
{
//call to function Make_Uppercase.
Make_Uppercase(Str_Lines); //This function returns the chars to upper

cout << Str_Lines << "\n"; //used for testing and debugging

//call to function IncrementLtrs
IncrementLtrs(Str_Lines, Alpha_Array);
} //end of while loop

// call to FInd highest letter occurances
//int Hi_Occur(int Alpha_Array[SIZE], int &high, int &low);

// call to FInd Lowest letter occurances
//int Lo_Occur(int Alpha_Array[SIZE], int &high, int &low);

//cout << "The letter with the highest occurrence is:\t" << high << endl;
//cout << "The letter with lowest occurrence is:\t" << low << endl;
}
else
{
//if errors opening the file.
cout << "Unable to open your file...\n";
cout << "Please check the file location and try again?" << endl;
}

readinFile.close();

cin.get();

return 0; //exit main function
}
//**********************
// end of main() function
//**********************

void Make_Uppercase(string &Str_Lines) //function to convert to uppercase
{
int count;
for (int count = 0; count < Str_Lines.length(); count++)
{
Str_Lines[count] = toupper(Str_Lines[count]);
//cout << Str_Lines << endl; //for testing and debugging
}
return ;
}


void IncrementLtrs(string Str_Lines, int Alpha_Array[])
// For each character in Str_Lines
// if the character is a letter
// increment the element of Alpha_Array that corresponds to the letter
// Alpha_Array[0] for 'A'
// Alpha_Array[2] for 'C', etc
// end if
// End For
{
//const int SIZE = 26;

for (int count = 0; count < Str_Lines.length(); count++)
{
if (isalpha(Str_Lines[count]))
{
Alpha_Array[Str_Lines[count] - 'A']++;
//cout << Alpha_Array[6]; //for testing and debugging
}
else if (!isalpha(Str_Lines[count]))
continue;
}
return;
}
/*
int Hi_Occur(int Alpha_Array[], int &high, int &low) //examples for some of this code is from Gaddis Ch-10 ppp578-9
{
const int letter = { ' ' };

for (int count = 0; count < SIZE; count++)
{
if (count == letter) //examples for this are on pg 401...
{
Alpha_Array++;
}
else if (count > letter)
{

//this translates out to an example on Gaddis pps 400-1
//'A' = ascii char 65, and all other capital alpha chars increase in value,
//therefore the difference equates to int letterCount[letter] -= letterCount[letter] - '65'] + 1; //this is really a question, because I want to ensure that the ascii value in the element being scanned is

int letterCount[letter] - 65] = letterCount[[letter] – 65] + 1;
high == letterCount[0];
}
else
{
low = letter;
}
}
return high;
*/
/*You can use a 26 element integer array to keep track of the letter counts by subtracting ‘A’ from the Ascii value of the character.Like,
letterCount[letter – ‘A’] = letterCount[letter – ‘A’] + 1;*/

//}
/* //Lowest occurance header and definition
int Lo_Occur(int Alpha_Array[], int &high, int &low)
{
int ch;

for (int count = 0; count < Alpha_Array[]; count++)
{
if (count == ch)
{
Alpha_Array[]++;
}
else if (count > ch)
{
int letterCount[ch - 'A'] = letterCount[ch – 'A'] + 1;
high == ch;
}
else
{
low = ch;
}
return low;
}
} */

/*You can use a 26 element integer array to keep track of the letter counts by subtracting ‘A’ from the Ascii value of the character.Like,
letterCount[letter – ‘A’] = letterCount[letter – ‘A’] + 1;*/

oh and the test file is some of the following, which I pasted into the visual studios folder right where main is. The file name is "Letter_count.txt"... again thank you for your help!!

To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished. To die, to sleep--
To sleep--perchance to dream: ay, there's the rub,
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil,
Must give us pause. There's the respect
That makes calamity of so long life.
Last edited on
¿who did the ` IncrementLtrs()' function?
That function already takes care of computing the frequency of the letter.

You only need to do a `max_element()' that'll give you the index of the biggest element in the array. You may abstract of the rest, you are giving an array of number and you've got to say which element is the biggest (the index of it).
Topic archived. No new replies allowed.