Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and write a program in C++ that reads a text file and counts the number o

ID: 3887469 • Letter: D

Question

  Design and write a program in C++ that reads a text file and counts               the number of times each NON-WHITESPACE character occurs, then               computes a single integer (long) value based on the character               code(ASCII value) and occurrence frequency of each such character.    Calculation: hash value = sum of products (char_code x char_frequency)  
  EXAMPLE:     The file simple.dat contains:                   Fat cat ran slow.                  But the butterball turkey                  got stuck on the Thanksgiving table.                             Program output: (note the FORMATTING--up to 10 per line, spacing)                   SYMBOL FREQUENCIES:                                         2.    1B    1F    1T    6a    3b    2c    5e    3g    3h                      2i    3k    4l    4n    3o    3r    3s   11t    4u    1v                      1w    1y                     simple.dat HASH VALUE = 7037 

Explanation / Answer

SOLUTION :

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<stdio.h>

#include<fstream.h>

void main()

{

clrscr();

char str[20],ch;

int arr1[26],arr2[26],dot=0;

int sum=0;

cout<<" Enter the filename : ";

gets(str);

ifstream fin(str,ios::in);

for(int i=0;i<26;i++)

{

arr1[i]=0;

arr2[i]=0;

}

while(fin)

{

fin.get(ch);

if(ch!=' ' && ch!=' ')

{

if(ch>='A' && ch<='Z')

arr1[ch-65]++;

else if(ch>='a' && ch<='z')

arr2[ch-97]++;

else if(ch=='.')

dot++;

}

}

cout<<" SYMBOL FREQUENCIES : "<<endl;

if(dot!=0)

{

sum+=dot*46;

cout<<dot<<". ";

}

for(i=0;i<26;i++)

{

if(arr1[i]!=0)

{ ch=i+65;

cout<<arr1[i]<<ch<<" ";

sum+=(i+65)*arr1[i];

}

}

for(i=0;i<26;i++)

{

if(arr2[i]!=0)

{ ch=i+97;

cout<<arr2[i]<<ch<<" ";

sum+=(i+97)*arr2[i];

}

}

fin.close();

cout<<" HASH VALUE : "<<sum;

getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote