What is the Pseudocode of this C++ code? #include<iostream> #include<string> #in
ID: 3874907 • Letter: W
Question
What is the Pseudocode of this C++ code?
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
int CountWordsAndLetters(char* str, int& words, int& letters)
{
words = 0;
unsigned int i = 0;
letters = 0;
while (str[i] == ' ')
i++;
for (; str[i]; i++)
{
if (((str[i] >= 'a') && (str[i] <= 'z')) || ((str[i] >= 'A') && (str[i] <= 'Z')))
letters++;
if (str[i] == ' ')
{
words++;
while (1)
if (str[i] == ' ')
i++;
else
{
i--;
break;
}
}
}
words = words + 1;
return (words);
}
float avg(float words, float letters)
{
float a = (double)(letters / words);
return a;
}
void CountVowelsAndConsonents(char* line, int &vowels, int &consonents)
{
for(int i = 0; line[i]!=''; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if ((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonents;
}
}
}
int main()
{
FILE *f = fopen("example.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *string = (char *)malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
string[fsize] = 0;
int words = 0;
int letters = 0;
int vowels=0;
int consonents = 0;
cout << "Data successfully read from example.txt file.. "<<endl;
CountWordsAndLetters(string, words, letters);
CountVowelsAndConsonents(string,vowels,consonents);
cout << "Please open output.txt file to see the results.. "<<endl;
freopen("output.txt","w",stdout);
cout<<"Original string-> " << string<<endl;
cout << " word count= " << words;
cout << " letter count=" << letters;
cout << " average number of letters per word=" << avg(words, letters)<<endl;
cout << " Number of vowels are : " << vowels<< endl;
cout << " Number of consonents are : " << consonents<< endl;
return 0;
}
Explanation / Answer
Pseudocode is the way of writing computer programming code in the high-level language like English in a simple manner. Pseudocode does not use variable declarations and specific programming language syntax.it does not include detailed methods or subroutines.
for the given c++ code pseudocode is written as below.
------------------------------------------------------------------------------------------------------------------------------------------------------
1.start program
2.opens binary file "example.txt" in read mode and set it to file pointer f .
3.set file pointer f to end of file
4.set fsize to the position of file pointer f in the file.
5. allocate memory to char array named strting[] equal to fsize+1.
6. read file to string[] array.
7. close file pointer.
8.set string[],words,letters,vowels and consonants to zero.
9.CALL CountWordsAndLetters function with string, words, letters.
10.CALL CountVowelsAndConsonents function with string,vowels,consonents.
11.open "output.txt" file
12.print word count stored in words and letter count stored in letters.
13.CALL avg function with words, letters to print average number of letters per word.
14.print Number of vowels and Number of consonents.
15.end program
-----------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.