Question

In: Computer Science

In C++, use SML programs to accomplish each of the following tasks: a) Use a sentinel-controlled...

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.

Solutions

Expert Solution

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


Related Solutions

Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
Write a single C++ statement to accomplish each of the following.                               &nbs
Write a single C++ statement to accomplish each of the following.                                                  (6 pts) Read an integer from the user at the keyboard and store the value entered in an integer variable age. Compute the product of the 3 integers contained in variables x, y and z and assign the result to the variable result
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
SML Complete the following programs. 1. Write a function listify that takes a list and returns...
SML Complete the following programs. 1. Write a function listify that takes a list and returns a list of lists where each element of first list becoming its own single-element list (Fill in the code here) fun main() = let val lst = (explode "rad"); in print (PolyML.makestring ( listify lst )) end; 2. Write a function splitlist that takes a list of pairs and returns a pair of lists that has the firsts of the pairs in one list...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a select statement to show the invoicelineitemdescriptions that have the total invoicelineitemamount >1000 and the number of accountno is >2. 10. Write a select statement that returns the vendorid, paymentsum of each vendor, and the number of invoices of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number...
How to use or embed assembly routines/codes in C programs?
How to use or embed assembly routines/codes in C programs?
Complete the following tasks. In each exercise, represent your answer only in DBDL. Do not use...
Complete the following tasks. In each exercise, represent your answer only in DBDL. Do not use diagrams a this point. Submit either a text file or a Word document with your work. Make sure you follow chapter 6's DBDL notation (****** Just to clarify: this is the first question on Page 220. It says to 'produce the following reports', but what you are asked to do is use DBDL notation to create 5 tables: Guide, Trip, Customer, Reservation and TripGuides...
Use the SML to determine the required rate of return for the following securities: Security 1...
Use the SML to determine the required rate of return for the following securities: Security 1 has a beta of 5, Security 2 has a beta of 0.5. The risk free rate is 0.025 and the market required rate of return is 0.1 Suppose that the expected rate of return for Security 1 is 0.13 and the expected rate of return for Security 2 is 0.10. Explain what happens in this market, if anything. Suppose that the required rate of...
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT