write a pseudo code for the following c++ program #include<iostream> #include<cm
ID: 3579216 • Letter: W
Question
write a pseudo code for the following c++ program
#include<iostream>
#include<cmath>
#include<fstream>
using namespace std;
int main(){
ifstream fin;
string ins;
cout<<"Input file name to read marks:";
cin>>ins;
//check for valid file
fin.open(ins.c_str());
if(!fin.is_open()){
cout<<"Unable to open file.";
return 0;
}
int data[500];
int count = 0;
//open file and start reading numbers
while(true){
if(fin.eof())
break;
fin>>data[count];
count++;
}
count--;
fin.close();
float mean=0, sd=0, max = data[0], min = data[0], range, sum=0;
//calculate everything other than standard deviation
for(int i=0;i<count;i++){
sum += data[i];
max = (data[i] > max) ? data[i] : max;
min = (data[i] < min) ? data[i] : min;
}
range = max - min;
mean = sum/count;
//calc sd
sum = 0;
for(int i=0;i<count;i++){
sum += (data[i] - mean) * (data[i] - mean);
}
sd = sqrt(sum/count);
ofstream fout("numbers.txt");
if(!fout.is_open()){
cout<<"Unable to save data.";
}
//printing stats
fout<<"Mean: "<<mean<<endl;
fout<<"Standard Deviation: "<<sd<<endl;
fout<<"Max: "<<max<<endl;
fout<<"Min: "<<min<<endl;
fout<<"Range: "<<range<<endl;
fout<<" Grades Number of scores: "<<endl;
//calc grade and print to file
for(int i=0;i<count;i++){
float x = data[i];
if(mean + 4/3 * sd <= x)
fout<<"A "<<x<<endl;
else if(mean + 3/3 * sd <= x && x < mean + 4/3 * sd)
fout<<"A- "<<x<<endl;
else if(mean + 2/3 * sd <= x && x < mean + 3/3 * sd)
fout<<"B+ "<<x<<endl;
else if(mean + 1/3 * sd <= x && x < mean + 2/3 * sd)
fout<<"B "<<x<<endl;
else if(mean + 0/3 * sd <= x && x < mean + 1/3 * sd)
fout<<"B- "<<x<<endl;
else if(mean - 1/3 * sd <= x && x < mean + 0/3 * sd)
fout<<"C+ "<<x<<endl;
else if(mean - 2/3 * sd <= x && x < mean - 1/3 * sd)
fout<<"C "<<x<<endl;
else if(mean - 3/3 * sd <= x && x < mean - 2/3 * sd)
fout<<"C- "<<x<<endl;
else if(mean - 4/3 * sd <= x && x < mean - 3/3 * sd)
fout<<"D+ "<<x<<endl;
else if(mean - 5/3 * sd <= x && x < mean - 4/3 * sd)
fout<<"D "<<x<<endl;
else if(x < mean - 5/3 * sd)
fout<<"F "<<x<<endl;
}
cout<<"Processed data saved in numbers.txt";
return 0;
}
Explanation / Answer
PSEUDO CODE
1) Declare an object for file input stream (ifstream fin;)
2) Declare a string ins (string ins;)
3) Ask the user to enter filename in the string variable ins (cin>>ins;)
4) Open the file.
5) Check the file can be opened or not. It file is unable to open show an error message as: “Unable to open file.”, and return 0.
6) Declare an integer array as data
7) Declare an integer variable count and initialize it to zero.
8) Read till end of the file reached.
8.1) Store the value in the integer array data.
8.2) Increase the counter by 1.
9) Decrease the counter by 1.
10) Close the file.
11) Declare float variable mean, sd, max, min, range, sum;
12) Initialize mean, sd, sum to zero.
13) Initialize max and min to the stating position of the integer data array.
14) Set the variable I to 0
15) Loop until I is less than counter
15.1) Add I position of integer array data to sum and store it in sum
15.2) Check if I position of data is greater than max
15.2.1) Set max to data of I position
15.3) Check if I position of data is less than min
15.2.1) Set min to data of I position
15.4) Add 1 to I
16) Set range = max – min
17) Set mean = sum / count
18) Initialize sum to zero
19) Set the variable I to zero
20) Loop until I is less than the counter
20.1) Subtract mean from I position of data
20.2) Find out the square of subtracted result
20.3) Add the sum with the square result and store it in sum
20.4) Add 1 to I
21) Find out the square rot after divide sum by counter and store it in sd.
22) Open file “numbers.txt” for writing
23) Check whether the file can be open or not. If unable to open display an error message as: <<"Unable to save data."
24) Write message “Mean: ” and calculated value stored in mean
25) Write message “Standard Deviation: ” and calculated value stored in sd
26) Write message “Max: ” and calculated value stored in max
27) Write message “Min: ” and calculated value stored in min
28) Write message “ Grades Number of scores: ”.
29) Set the variable I to 0
30) Loop till I is less than counter
30.1) Declare a float type variable x and store the I index position of integer array data
30.2) Check if (mean + 4/3 * st less than or equal to variable x) then
30.2.1) Write message "A " with variable contents x with new line character.
30.3) Otherwise check if (mean + 3/3 * sd less than or equal to variable x and variable x less than mean + 4/3 * sd) then
30.3.1) Write message "A- " with variable contents x with new line character.
30.4) Otherwise check if (mean + 2/3 * sd less than or equal to variable x and variable x less than mean + 3/3 * sd) then
30.4.1) Write message "B+ " with variable contents x with new line character.
30.5) Otherwise check if (mean + 1/3 * sd less than or equal to variable x and variable x less than mean + 2/3 * sd) then
30.5.1) Write message "B " with variable contents x with new line character.
30.6) Otherwise check if (mean + 0/3 * sd less than or equal to variable x and variable x less than mean + 1/3 * sd) then
30.6.1) Write message "B- " with variable contents x with new line character.
30.7) Otherwise check if (mean - 1/3 * sd less than or equal to variable x and variable x less than mean + 0/3 * sd) then
30.7.1) Write message "C+ " with variable contents x with new line character.
30.8) Otherwise check if (mean - 2/3 * sd less than or equal to variable x and variable x less than mean - 1/3 * sd) then
30.8.1) Write message "C " with variable contents x with new line character.
30.9) Otherwise check if (mean - 3/3 * sd less than or equal to variable x and variable x less than mean - 2/3 * sd) then
30.9.1) Write message "C- " with variable contents x with new line character.
30.10) Otherwise check if (mean - 4/3 * sd less than or equal to variable x and variable x less than mean - 3/3 * sd) then
30.10.1) Write message "D+ " with variable contents x with new line character.
30.11) Otherwise check if (mean - 5/3 * sd less than or equal to variable x and variable x less than mean - 4/3 * sd) then
30.11.1) Write message "D " with variable contents x with new line character.
30.12) Otherwise check if (x is less than mean - 5/3 * sd) then
30.12.1) Write message "F " with variable contents x with new line character.
30.13) Add 1 to I
31) Display message: "Processed data saved in numbers.txt"
32) End
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.