Sorting Arrays / C ++ I / O Files

I was wondering, someone could help me in sorting this array, I lost a little how to accurately implement it in this project. Because this HW does not reveal the whole answer, but pushes me in the right direction. The project is as follows: Write a program that will read a line of text and list all letters that appear in the text, along with the number of times each letter appears in the line. End the line with a period that serves as a reference value. Letters should be used in the following order: from highest to lowest. Assume that all lowercase letters are used at the input. A couple of questions. 1. Am I following the correct way to sort an array? 2. Before putting the sorting array into my code, a blank screen appears when compiling the code. Is there any way to fix this?

Sorry if this is spelled poorly, and in advance for your help!

inlcude <iostream> #inlcude <fstream> using namespace std; void initialize(int list[]); void Sort(int list[],int& num); void characterCount(char ch, int list[]); void readText(ifstream& intext, char& ch, int list[]); void totalCount(int list[]); int main() { int index,letterCount[26]; char ch; ifstream inFile; infile.open("C:/temp/Data_Chapter_7_8.txt"); if (!inFile) { cout << " Cannot open file." <<endl; } initialize(letterCount); infile.get(ch); while (inFile) { int index; readText(inFile,ch,letterCount) index++; inFile.get(ch); } totalCount(index, letterCount); inFile.close(); system("PAUSE"); return 0; } //initializes array letterCount to 0 void initialize(int list[]) { for(int x = 0;x<26;x++) list[x] = 0 } //increments the letter count. Makes sure counting letters. void characterCount (char ch, int list[]) { int index; ch = tolower(ch); if(static_cast<int>(ch)>=97&&(static_cast<int>(ch)<=122)) letterCount[static_cast<int>(ch)-97]++; } void readText(ifstream& intext, char& ch, int list[]) { while (ch != '.') { characterCount (ch,list); intext.get(ch); } } //displays data void totalCount(int list[]) { for(int x=0;x<26;x++) if(letterCount[x]>0) cout<<static_cast<char>(x+97)<<" "<<letterCount[x]<<endl; } void Sort(int list[],int& num) { int i,j,flag = 1; int temp; int numLength = num.length(); for (i=1;(i<=numLength)&&flag; i++) { flag = 0; for (j=o; j<(numLength-1);j++0 { if(num[j+1]>num[j]) { temp = num[j]; num[j] = num[j+1]; num[j+1]=temp; flag = 1; } } } return; } 
+4
source share
2 answers

Instead of using promiscuous sorts of bubbles and other fun things, we can simply track the number of occurrences of each letter, as there are only 26 possibilities. This should result in a cleaner (and much faster) code:

 int numOccur[26]; ... for (int i = 0; i < numCh; i ++) numOccur[letters[i] - 'a'] ++; for (i = 25; i >= 0; i --) if (i > 0) cout<<static_cast<char>(i+97)<<" "<<numOccur[i]<<endl; 

Of course, you should replace the for loop with the corresponding file reading cycle.

+1
source

Just a few comments.

  • I see no good reason for most of the casting here.
  • Use isalpha , isupper or islower to check letters and tolower or toupper to toupper them (Note: using these functions is one of the few excuses for casting).
  • It is probably easiest to initialize the lettercount array with `int lettercount [26] = {0};
  • If you do not need to write your own sort procedure, just use std::sort .
  • It’s usually easiest to open the file as you define your ifstream: std::ifstream infile("whatever");
  • Do not use while (infile) . This is almost a guaranteed error. Read and check if this has been done, for example: while (infile.get(ch)) ...
0
source

Source: https://habr.com/ru/post/1446591/


All Articles