In: Computer Science
In C++, use SML programs to accomplish each of the following tasks:
a) Use a sentinel-controlled loop to read positive numbers and compute and display their sum. Terminate input when a negative number is entered.
b) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute and display their average.
c) Read a series of numbers, and determine and display the largest number. The first number read indicates how many numbers should be processed.
For input you should read the instructions from a file (after prompting the user for the name of the file to read). Make the intro banner say "Welcome to Simpletron! Enter the name of the file containing your program:"
Here is a sample of the output expected:
Execution halted normally
REGISTERS:
accumulator -1
instructionCounter 6
instructionRegister 4300
opcode 43
operand 0
MEMORY:
0 1 2 3 4 5 6 7 8 9
0 1007 1008 2007 3008 2109 1109 4300 4 -5 -1
10 0 0 0 0 0 0 0 0 0 0
. . .
90 0 0 0 0 0 0 0 0 0 0
Please show the output files for a, b, and c. Thank you.
Question a: sentinel controlled loop to calculate the sum of numbers entered in a file (It should terminate if a negative value is found in the file)
C++ Program
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char sumNumberFileName[25] ;
string inputLine;
int sum=0;
cout<<"Welcome to Simpletron! Enter the name of the file
containing your program:";
cin>>sumNumberFileName;
cout<<endl<<"The file name entered is "
<<sumNumberFileName;
ifstream myfile (sumNumberFileName);
if (myfile.is_open())
{
while ( getline (myfile,inputLine) )
{
//stoi function is used to convert from a string value to an
interger value
while(stoi(inputLine) >= 0)
{
sum=sum+stoi(inputLine);
break;
}
if(stoi(inputLine) <= 0)
break;
}
myfile.close();
cout<<endl<<"The sum of the values in the file is "
<<sum;
}
else
cout << "Unable to open file";
return 0;
}
Output Screenshot
Question b: Counter controlled loop to read 7 numbers from a file and calculate its average
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;
int main()
{
char averageFileName[25] ;
string inputLine;
int sum=0;
int count = 0;
double average;
cout<<"Welcome to Simpletron! Enter the name of the file
containing your program:";
cin>>averageFileName;
cout<<endl<<"The file name entered is "
<<averageFileName<<endl;
ifstream myfile (averageFileName);
if (myfile.is_open())
{
while ( getline (myfile,inputLine) )
{
cout<<"Line from file is "
<<inputLine<<endl;
//stoi function is used to convert from a string value to an
interger value
while(count<7){
sum=sum+stoi(inputLine);
count++;
break;
}
}
myfile.close();
average = sum/7.0;
cout.precision(4);
cout<<"The average of first seven numbers in the file is "
<< fixed <<average;
}
else
cout << "Unable to open file";
return 0;
}
Output
Question C: Finding the largest number from a series of numbers. The numbers are stored in file and the first number in the file will be limit
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char largestNumberFile[25] ;
string inputLine;
int sum=0;
int count = 0;
int largest = 0;
int countfromFile = 0;
cout<<"Welcome to Simpletron! Enter the name of the file
containing your program:";
cin>>largestNumberFile;
cout<<endl<<"The file name entered is "
<<largestNumberFile<<endl;
//cout<<endl<<"Please enter limit"
ifstream myfile (largestNumberFile);
if (myfile.is_open())
{
getline (myfile,inputLine);
cout<<endl<<"We will be calculating the largest of
first "<< inputLine <<" numbers in the
file"<<endl;
countfromFile = stoi(inputLine);
while ( getline (myfile,inputLine) )
{
//cout<<endl<<"We will be calculating the largest of
first "<< inputLine <<" numbers in the file";
// getline (myfile,inputLine);
cout<<"Line from file is "
<<inputLine<<endl;
//stoi function is used to convert from a string value to an
interger value
while(count < countfromFile){
if(largest < stoi(inputLine))
largest = stoi(inputLine);
count++;
break;
}
}
myfile.close();
cout<<endl<<"The largest number of first "<<
countfromFile<<" numbers from the file is " <<
largest;
}
else
cout << "Unable to open file";
return 0;
}
Output
Please let me know for any help, Thank you!
Please find the updated code with all three functionalities consolidated in one program
#include<iostream>
#include<fstream>
#include<string>
#include<limits>
using namespace std;
int main()
{
char sumNumberFileName[25];
string inputLine;
int sum=0;
int choice;
int sum1=0;
int count=0,count1=0;
double average;
char averageFileName[25];
string inputLine1,inputLine2;
char largestNumberFile[25];
int largest=0;
int countfromFile;
cout<<"Welcome to Simpletron";
cout<<endl<<"1. Calculate the sum of numbers entered in
a file";
cout<<endl<<"2. Read numbers from a file and calculate
its average";
cout<<endl<<"3. Find largest number from a series of
number";
cout<<endl<<"4. Exit the Simpletron program";
cout<<endl<<"Enter your choice(1-4) : ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<endl<<"Welcome to Simpletron! Enter your file
contains the program :";
cin>>sumNumberFileName;
cout<<endl<<"The filename entered is
"<<sumNumberFileName;
ifstream myfile(sumNumberFileName);
if(myfile.is_open())
{
while(getline(myfile,inputLine))
{
while(stoi(inputLine) >= 0)
{
sum=sum+stoi(inputLine);
break;
}
if(stoi(inputLine) <= 0)
break;
}
myfile.close();
cout<<endl<<"The sum of the values in the file is :
"<<sum;
}
else
cout<<endl<<"Unable to open the file";
}
break;
case 2:
{
cout<<endl<<"Welcome to Simpletron! Enter your file
containing your program : ";
cin>>averageFileName;
cout<<endl<<"Filename entered is :
"<<averageFileName;
ifstream myfile1(averageFileName);
if(myfile1.is_open())
{
while(getline(myfile1,inputLine1))
{
while(count<7)
{
sum1=sum1+stoi(inputLine1);
count++;
break;
}
}
myfile1.close();
average=sum1/7.0;
cout.precision(4);
cout<<endl<<"The average of first 7 numbers in the file
is : " <<fixed<<average;
}
else
cout<<endl<<"unable to open the file";
}
break;
case 3:
{
cout<<endl<<"Welcome to Simpletron! Enter your file
containing your program : "<<endl;
cin>>largestNumberFile;
cout<<endl<<"File name entered is "
<<largestNumberFile;
ifstream myfile2(largestNumberFile);
if(myfile2.is_open()){
getline(myfile2,inputLine2);
cout<<"We will be calculating the largest of first
"<<inputLine2<<" numbers";
countfromFile=stoi(inputLine2);
while(getline(myfile2,inputLine2)){
while(count1<countfromFile){
if(largest<stoi(inputLine2))
largest=stoi(inputLine2);
count1++;
break;
}
}
myfile2.close();
cout<<endl<<"The largest number of first
"<<countfromFile <<" is "<<largest;
}
else
cout<<endl<<"Unable to open the file";
}
break;
case 4:
exit(0);
default :
cout<<endl<<"Enter a valid choice";
}
return 0;
}
Output