You are here:

C++/word counter

Advertisement


Question
I was wondering if you have time to look at this new program. I am sending the whole instructions but right now I am only trying to do the first two lines. Could you tell me if it is correct or not and any other help explaining what I need to do next would be great. Thank you ahead of time for looking at it. I have 5 files that go with it. Do I need to have the files in the same folder as the program to get it to run? I really believe that I am way over my head in this:)I sent this into one of the other guys but he was busy planning a wedding, I just didn't want you to think I was trying to get double the help:)

INSTRUCTIONS:
Read the words from a file named “SelectWords.txt” into a 100-element array of strings.
Declare and initialize to zeroes a parallel array of whole numbers for word counts
Declare and initialize to zeroes a parallel array of doubles for word frequencies.
Declare two strings to hold the shortest and longest words in the data file. Initialize the shortest word to “thisisarunonsentencewithlotsofletters” and the longest word to “a”.
Declare a whole number variable to hold the total word count, and initialize it to zero.
Open and read one of three data files, provided by the instructor, examining each word as it is read.
If the word matches one in the SelectWords array, increment the corresponding count in the parallel array of whole numbes.
If the word is shorter than the word currently in the ShortestWord string, replace the contents of that string with the word from the data file.
If the word is longer than the word currently in the LongestWord string, replace the contents of that string with the word from the data file.
Increment the TotalWordCount by one for each word read from the data file.
After all words have been read from the data file, calculate the frequency of occurrence of each of the SelectedWord’s by dividing its count by the TotalWordCount, and save the calculated value in the corresponding element of the array of doubles.
Display the name of the data file, the shortest word, longest word, and total word count.
Display the arrays of SelectedWord’s, their counts, and their frequency of occurrence in a columnar fashion. Format the frequency of occurrence with 2 significant digits and a field width of 6 characters.
For bonus credit: Declare an array of at least 30 integers and initialize each of them to zero. Use each to accumulate a count of all the words of that length. For instance, array[5] should hold a count of all words that were read from the data file that had a length of 5 characters. After all other output is complete, display the contents of this array, formatted as “N words of length M”, substituting the count for N and the array index for M. Do not display word lengths for which the count is zero.



WHAT I HAVE SO FAR:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main()
{
  int words[100];
  string wholeNum, count;
  int wholeNumbers[40];
  
  ifstream InFile;
  InFile.open( "SelectWords.txt" );
  for (int i = 0; i < 100; i++ )
  {
     getline(InFile, words[i]);   
  }
  for(row = 0; row < whole_numbers; row++)
     for(col = 0; col < whole_numbers; col++)
        matrix[row][col] = 0'
  
  system("pause");
  return 0;
}

Answer
Let us take this one step at a time.

> Read the words from a file named “SelectWords.txt” into a 100-element array of strings.

std::getline() reads a complete line - if the line in the file is:
How now brown cow
getline() will read "How now brown cow" into a single string.

To read a word (assuming that words are white-space delimited), use formatted input ( >> ).


The code assumes that there are at least 100 words in the file SelectWords.txt Perhaps you need to keep a count of the words.

Something like this:

const int MAX_WORDS = 100 ;
std::string words[ MAX_WORDS ] ;

int word_count = 0 ;
std::ifstream file( "SelectWords.txt" ) ;
while( file >> words[ word_count ] ) ++word_count ;
// at this point word_count contains the number of words read.



> Declare and initialize to zeroes a parallel array of whole numbers for word counts

int word_counts[ MAX_WORDS ] = { 0 } ; // initialize to zeroes

> If the word matches one in the SelectWords array, increment the corresponding count in the parallel array of whole numbes.

For this, you need to check if the word is in the words array and if it is, return its position in the array. So write a function to do that:

// find the position of a_word in the array words.
// If a_word os found, return the position; if not return -1

int find( std::string words[], int word_count, std::string a_word )
{
      for( int i=0 ; i < word_count ; ++i ) if( words[i] == a_word ) return i ;
      return -1 ; // didn't find it
}

and then for each word read from the file:
int pos = find( words, word_count, word_read_from_file ) ;
if( pos != -1 ) ++word_counts ; // if found, increment the count.


Do this much, and write the code to print out the count of each word.
Once that is done, post the next question that you have.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.