Coppin State College
COSC 199 – Fall 2005
Programming Assignment 6
Loop Control Structures (Chapters 6. 9.2, and 9.3)
Due Date: Tuesday, November 22, 2005
(35 points)
Student’s Name: ______
NOTE: (1pt) Staple this instruction page(s) (all the pages) as a cover to your submitted assignment. Your assignment will be graded based on style and correctness of your program.
NOTE: For all tasks, use H: drive instead of C: drive.
Example Program 1: Write the following problem in C++ program using ISO/ANSI 1989 standard (New).
Print out the total numbers entered and their total in formatted form as shown below (right aligned).
OUTPUT
Total numbers entered = 5
Accumulator = 26.95
- Solve the problem using while-loop control structure
- Test the program with the data: 5.583, 5.499, 5.215 , 5.652, 5.005
The program
/*/*
Description: A while loop demo: This program will allow the user to enter some integers
interactively in iterative process for 5 times and calculate the accumulation of numbers
- Use standard inpt and output
- Use New C++ Standard (ISO/ANSI 1989)
- use of output formatting functions: setw(), fixed, setprecision()
- Test with five numbers: 5.583, 5.499, 5.215 , 5.652, 5.005
Programmer: Sisir Ray
Program Name: While loop example.cpp
Course Name: COSC 199 001
Start date: 04/24/03
Completion date: 04/24/03
*/
// Preprocessor directives
#include<iostream>
#include<iomanip> // required for setw() and setprecision() functions and fixed statement
using namespace std;
int main ()
{
// Declaration statements
int numberCount;
float number, total;
// initialization
total = 0;
numberCount = 1;
while (numberCount <= 5)
{
//input
cout < "Please enter numbers: ";
cin > number;
// process
total = total + number;
numberCount++;
}
numberCount = numberCount - 1; // number of integers entered is one lass
// output
cout < endl; // provide with a blank line
cout < "Total numbers entered = " < setw(10) < numberCount < endl;
cout < fixed < setprecision(2);
cout < "Accumulator = " < setw(20) < total < endl;
// terminate and return to os
return 0;
}
/* RESULTS or OUTPUT
Please enter numbers: 5.583
Please enter numbers: 5.499
Please enter numbers: 5.215
Please enter numbers: 5.652
Please enter numbers: 5.005
Total numbers entered = 5
Accumulator = 26.95
*/
Example Program 2: Solve the above problem using do-loop control structure
The Program
/*
Description: A do loop demo: This program will allow the user to enter some integers
interactively in iterative process for 5 times and calculate the accumulation of numbers
- Use standard inpt and output
- Use New C++ Standard (ISO/ANSI 1989)
- use of output formatting functions: setw(), fixed, setprecision()
- Test with five integers: 5.583, 5.499, 5.215 , 5.652, 5.005
Programmer: Sisir Ray
Program Name: Do loop example.cpp
Course Name: COSC 199 001
Start date: 04/24/03
Completion date: 04/24/03
*/
// Preprocessor directives
#include<iostream>
#include<iomanip> // required for setw() and setprecision() functions and fixed statement
using namespace std;
int main ()
{
// Declaration statements
int numberCount;
float number, total;
// initialization
total = 0;
numberCount = 1;
do
{
//input
cout < "Please enter numbers: ";
cin > number;
// process
total = total + number;
numberCount++;
} while (numberCount <= 5);
numberCount = numberCount - 1; // number of integers entered is one lass
// output
cout < "Total numbers entered = " < setw(10) < numberCount < endl;
cout < fixed < setprecision(2);
cout < "Accumulator = " < setw(20) < total < endl;
// terminate and return to os
return 0;
}
/* RESULTS or OUTPUT
Please enter numbers: 5.583
Please enter numbers: 5.499
Please enter numbers: 5.215
Please enter numbers: 5.652
Please enter numbers: 5.005
Total numbers entered = 5
Accumulator = 26.95
*/
Example Program 3: Solve the above problem using for-loop control structure
The Program
/*
Description: A for loop demo: This program will allow the user to enter some integers
interactively in iterative process for 5 times and calculate the accumulation of numbers
- Use standard inpt and output
- Use New C++ Standard (ISO/ANSI 1989)
- use of output formatting functions: setw(), fixed, setprecision()
- Test with five integers: 5.583, 5.499, 5.215 , 5.652, 5.005
Programmer: Sisir Ray
Program Name: For loop example.cpp
Course Name: COSC 199 001
Start date: 04/24/03
Completion date: 04/24/03*/
// Preprocessor directives
#include<iostream>
#include<iomanip> // required for setw() and setprecision() functions and fixed statement
using namespace std;
int main ()
{
// Declaration statements
int numberCount;
float number, total;
// initialization
total = 0;
for(numberCount = 1; numberCount <= 5; numberCount++)
{
//input
cout < "Please enter numbers: ";
cin > number;
// process
total = total + number;
}
numberCount = numberCount - 1; // number of integers entered is one lass
// output
cout < "Total numbers entered = " < setw(10) < numberCount < endl;
cout < fixed < setprecision(2);
cout < "Accumulator = " < setw(20) < total < endl;
// terminate and return to os
return 0;
}
/* RESULTS or OUTPUT
Please enter numbers: 5.583
Please enter numbers: 5.499
Please enter numbers: 5.215
Please enter numbers: 5.652
Please enter numbers: 5.005
Total numbers entered = 5
Accumulator = 26.95
*/
TASK 1
(2pts) Type the Example Program 1, compile, run, and test. Use the name of the program as it is.
TASK 2
(2pts) Type the Example Program 2, compile, run, and test. Use the name of the program as it is.
TASK 3
(2pts) Type the Example Program 3, compile, run, and test. Use the name of the program as it is.
Exercise Problem: Modify the problem given in Example Program 1. Instead of five, allow the user to enter ten numbers. Calculate total (accumulator) as well as average of the entered numbers. The output should be formatted and should look like as below (example) and the results of total and average should be rounded after one decimal point: (all output digits should be right aligned).
OUTPUT
Total numbers entered = 10
Accumulator = 278.8
Average = 27.9
TASK 4
(5pts) Solve the Exercise Problem using while-loop control structure. Use the name of the program: COSC199Lab6a.cpp
TEST data: 5.345, 10.014, 15.5, 20.23, 25.805, 30.199, 35.689, 40.069, 45.429, 50.519
TASK 5
(5pts) Solve the Exercise Problem using do-loop control structure. Use the name of the program: COSC199Lab6b.cpp
Test data: same as Task 4
TASK 6
(5pts) Solve the Exercise Problem using for-loop control structure. Use the name of the program: COSC199Lab6c.cpp
Test data: same as Task 4.
TASK 7
(10pts ) in one of your previous Programming Assignments, you wrote a program for computing Addition, Subtraction, Multiplication, Division, and Remainder of two integers. Rewrite the C++ program for the same problem for any number of uses without leaving from the execution of your program. Use appropriate loop structure (while, do-while, or for loop) to write your program. Also, safeguard your program from division by zero. Use the name of the program: COSC199Lab6d.cpp
Test your program with at least 5 sets of data of your choice. Also, at least one of your data must have the denominator value zero of a division
REQUIREMENTS:
1. Your source code for each task should clearly indicate the preprocessor directives, local declarations, inputs, processes, and outputs, if there be any.
2. (2pts) After you compile and run successfully your programs from H: drive instead of C: drive, save all the .cpp files with their outputs in a folder, COSC199Lab6 in a floppy disk. Submit the floppy disk and hardcopies of all the programs.
3. 1 pt is reserved.
NOTE: General Format of your Submitted program listing and output:
/*
Standard information
*/
The Source Code with proper styles and comments (your .cpp program)
/* Results:
Paste your output here
*/