Question
Lab#2 Sentinel Loop & Function In this lab, you will explore the use of function. Functions are a convenient way to organize and encapsulate code,
Lab#2
Sentinel Loop & Function In this lab, you will explore the use of function. Functions are a convenient way to organize and encapsulate code, so that the complexity hidden inside the function pushed away from the programmer (unless he or she wants to look at it).
Often when programming, you come across situations in which you write almost the same block code over and over. Usually, this is a sign that the repetitive code should be "refactored" in a function, and that you should call the function instead of repeating the code.
|
Directions
1. Lab2 Sentinel loop & functions
1.1 Complete the following program named Lab2.cpp
//Lab Lab#2
//C++ Programming
//
//
//
#include
#include
#include
using namespace std;
double readData(double SENT, ifstream *Openfile);
int main ( )
{
const double SENT = -999.99;
ifstream OpenFile("Lab3AData.txt");
//Declare variables
double overallSum;
double groupSum;
// initialize overall variables
// [Add code here]
// Write a While loop that will loop through and
// read in as many inputs as are in the file
// use (OpenFile.eof))
// [Add code here]
// initialize group variables
// [Add code here]
// Call the readData Function here.
// It will calculate and return the sum of
// a group of numbers
// [Add code here]
// Increase the overall sum with the
// group sum
// [Add code here]
// Display needed output.
// [Add code here]
//end of "end of file" loop
// [Add code here]
// Display needed output.
// [Add code here]
return 0;
} // end of main function
double readData(double sent, ifstream *Openfile)
{
// Declare any variables need for the function
// [Add code here]
// read in the first
// number from the file.
// [Add code here]
// Write a SENTINEL loop to continue until the SENTINEL
// is read in
// [Add code here]
// Calculate the sum
// [Add code here]
// read in the next
// number from the file.
// [Add code here]
// End of SENTINEL loop
// [Add code here]
// Return the sum
// [Add code here]
} // end of sumData Function
1.2 Add code where it says to finish the program. Make sure you import any needed classes.
1.3 Use the following input to test your program using file redirect.
//Name this in file Lab2data.txt
0.7497790548868464
0.6462076498775827
0.32616335534841256
0.37582682849958304
-999.99
0.41621372481399
0.6463706270883212
0.7655177221930171
0.2650562905607654
0.14762994129539841
0.9627229328823885
-999.99
0.08133929860959621
0.9560335264804165
0.8155875415237163
-999.99
0.019074104888079813
0.8270564200270186
-999.99
1.4 Compile & Run your program with input data (Lab2A.data.txt)
1.5 Given the in file in Step 3 your out file should look like the following.
The previous group had a sum of 2.10.
The current overall total had a sum of 2.10.
The previous group had a sum of 3.20.
The current overall total had a sum of 5.30.
The previous group had a sum of 1.85.
The current overall total had a sum of 7.15.
The previous group had a sum of 0.85.
The current overall total had a sum of 8.00.
The overall total had a sum of 8.00.
6) Upload your work to the black board
Lab2.cpp, Labdata.txt, LabA.doc
#Maximum number of changes to upload: only Five Times
Academic Integrity Policy: Academic integrity at SAU is an organizational and individual responsibility to honesty in all learning experiences. Any act of dishonesty in academic work constitutes academic misconduct and is subject to disciplinary action. Acts of dishonesty include, but are not limited to:
- Plagiarism--the act of taking and/or using the ideas, work, and/or writings of another person as ones own.
- Cheating--an act of dishonesty with the intention of obtaining and/or using information in a fraudulent manner.
Fabrication--faking or forging a document, signature or findings of a research project
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started