Can any one please help me i have been working on this program since morning but couldnot figure it out.....pls...
the program reads an input file and counts the frequency of characters in it i.e. how many times each character occuring.

i know how to read from an input file but i m not able to figure out the frequency part......help me pls

Recommended Answers

All 9 Replies

a simple counter comes to mind.
Store those in a map indexed on character and loop through the input character by character.

It seems that on line 10 you forgot to use the index onto your string array, so your bleglelstar gave a wrong gringledor 4 lines later.

In other words, help with what? Don't you have to show us what's wrong, or are we supposed to write it for you? ;)

i was thinking about the integral values like A =65 and so on but i dont know how to proceed ...help me pls.....

//File: array.cpp
//PURPOSE: To create a program that reads input from a text file
//and counts the frequency of all characters in the file.

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main ()
{
    ifstream myinput;
    ofstream myoutpt;

    myinput.open();
    if (myinput.fail())
    {
        cout << "Input file opening failed. \n" << endl;
        exit(1);
    }

upto this point i m sure tht this is fine but after this i have deleted and deleted again so many times whatever code i wrote my head is exploding !!!!!!!!!!!!

Well, even though you didn't use CODE tags prominently explained on the background of every input box you've typed in so far, the problem with your code is you didn't even try to do anything yet. There's no read attempt at all.

The idea is for us to help you with your program. There's nothing here for us to help with.

//File: array.cpp
//PURPOSE: To create a program that reads input from a text file
//and counts the frequency of all characters in the file.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

const int a = 91;
void  PrintOccurrences(const int[]);       // prototype 

int main ()
{
    int freq_count[a];
    char character, index;
    ifstream myinput;
    ofstream myoutpt;

    myinput.open();
    if (myinput.fail())
    {
        cout << "Input file opening failed. \n" << endl;
        exit(1);
    }
    for(int i = 0; i < a; i++)
    {
        freq_count[i] = 0;
    }
    while(myinput)
    {
         if (isalpha ( ch ) )
        {    
            if ( islower ( ch ) )
                ch  =  toupper ( ch ) ;
            
            freqCount [ ch ]  =  freqCount [ ch ] + 1 ;
        }
        dataFile. get ( ch ) ;         // get next character
     }
    PrintOccurrences ( freqCount ) ;
    return  0;
}
void  PrintOccurrences(/* in */ const int freqCount[]) 

// Prints each alphabet character and its frequency
// Precondition:
//    freqCount [ ‘A’ . . ‘Z’ ] are assigned
// Postcondition:
//    freqCount [ ‘A’ . . ‘Z’ ] have been printed
{    char  index ;
    
    cout  <<  “File contained “  << endl ;
    cout  <<  “LETTER      OCCURRENCES”  << endl ;
    for   (  index = ‘A’  ;  index < = ‘Z’ ;  index ++ )
    {
             cout   <<  setw ( 4 )  << index  <<  setw ( 10 )  
                  << freqCount [ index ]  << endl ;
    }
}

this is what i came up with and which makes the most sense to me but my compiler is still giving me errors so i dont understand wut is wrong now

#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

struct print_it
{
  void operator() ( const pair<char,int>& p ) const
  {
      cout << p.first << " occurs " << p.second << " times\n" ;
  }
};

int main( int argc, char** argv )
{
  if( argc != 2 )
  {
     cout << "usage: " << argv[0] << "file_name\n" ;
     return 1 ;
  }
  ifstream file( argv[1] ) ;
  if( !file )
  {
     cout << "could not open file " << argv[1] << '\n' ;
     return 2 ;
  }
  map<char,int> char_count ;
  char c ;
  while( file >> c ) ++char_count[c] ;
  for_each( char_count.begin(), char_count.end(), print_it() ) ;
  return 0 ;
}

I was obviously too subtle about CODE tags in my previous post. So, please use CODE tags!!! Instructions are on the background of the input box where you are typing your messages, or you can look here

this is what i came up with and which makes the most sense to me but my compiler is still giving me errors so i dont understand wut is wrong now

Neither do we. You get errors. Maybe you can tell us what the errors are? This way we don't have to guess and probably be wrong.

We would love to help you. But you need to give us as much info as you have. We're not in front of your computer, only you are. Details are important.

The logic of your program seems reasonable but your use of variables seems weak, for example where did datafile and ch come from; and you need to look up the declaration of the istream open() method--is it supposed to be myinput.open(sesame?) or is it something else you're trying to open, because clearly it's not open(void)?.

This is probably just be picking the cherries. Without your error messages we'll never know if there are other errors or not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.