Overview:
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, Analysis, , Design(program design, pseudocode), Test Plan, and implementation with C code. The example provided uses sequential, repetition statements and nested repetition statements.
Program Description:
This program will calculate the average of 10 positive integers. The program will ask the user to 10 integers. If any of the values entered is negative, a message will be displayed asking the user to enter a value greater than 0. The program will use a loop to input the data.
Analysis:
I will use sequential, selection and repetition programming statements.
The program will loop for 10 positive numbers, prompting the user to enter a number.
I will define three integer variables: count, value and sum. count will store how many times values greater than 0 are entered. value will store the input. Sum will store the sum of all 10 integers.
I will define one double number: avg. avg will store the average of the ten positive integers input.
The sum will be calculated by this formula: sum = sum + value For example, if the first value entered was 4 and second was 10: sum = sum + value = 0 + 4
sum = 4 + 10 = 14
Values and sum can be input and calculated within a repetition loop: while count <10
Input value
sum = sum + value End while
Avg can be calculated by: avg = value/count
A selection statement can be used inside the loop to make sure the input value is positive.
If value >= 0 then count = count + 1 sum = sum + value
Else
input value End If
(
7
)
Program Design:
Main
// This program will calculate the average of 10 integer numbers
// Declare variables
// Initialize variables
// Loop through 10 numbers
// Prompt for positive integer
// Get input
// test input value for gt 0 if (value > 0)
//Increment counter
//Accumulate sum Else
// display msg to enter a positive integer
// Prompt for positive integer
// Get input Endif
// End loop
//Calculate average
//Print the results (average)
End
Test Plan:
To verify this program is working properly the input values could be used for testing:
Test Case
Input
Expected Output
1
1 1 1 0 1
2 0 1 3 2
Average = 1.2
2
100 100 100 100 -100
Input a positive value
100 200 -200 200 200
Input a positive value
200 200
average is 120.0
NOTE: test #2 has 12 input numbers because there are two negative numbers.
Pseudocode: Main
// This program will calculate the average of 10 positive integers.
// Declare variables
Declare count, value, sum as Integer Declare avg as double
//Initialize values
Set count=0 Set sum = 0 Set avg = 0.0;
// Loop through 10 integers While count < 10
Input value
If (value >=0)
sum = sum + value count=count+1
Else
Pr *** Value must be positive *** Input value
End if End While
// Calculate average avg = sum/count
// Print results
End //End of Main
C Code
The following is the C Code that will compile in execute in the online.
OverviewThis hands-on lab allows you to follow and experiment w.docx
1. Overview:
This hands-on lab allows you to follow and experiment with the
critical steps of developing a program including the program
description, Analysis, , Design(program design, pseudocode),
Test Plan, and implementation with C code. The example
provided uses sequential, repetition statements and nested
repetition statements.
Program Description:
This program will calculate the average of 10 positive integers.
The program will ask the user to 10 integers. If any of the
values entered is negative, a message will be displayed asking
the user to enter a value greater than 0. The program will use a
loop to input the data.
Analysis:
I will use sequential, selection and repetition programming
statements.
The program will loop for 10 positive numbers, prompting the
user to enter a number.
I will define three integer variables: count, value and sum.
count will store how many times values greater than 0 are
entered. value will store the input. Sum will store the sum of all
10 integers.
I will define one double number: avg. avg will store the average
of the ten positive integers input.
The sum will be calculated by this formula: sum = sum + value
For example, if the first value entered was 4 and second was 10:
sum = sum + value = 0 + 4
sum = 4 + 10 = 14
Values and sum can be input and calculated within a repetition
loop: while count <10
Input value
sum = sum + value End while
Avg can be calculated by: avg = value/count
2. A selection statement can be used inside the loop to make sure
the input value is positive.
If value >= 0 then count = count + 1 sum = sum + value
Else
input value End If
(
7
)
Program Design:
Main
// This program will calculate the average of 10 integer numbers
// Declare variables
// Initialize variables
// Loop through 10 numbers
// Prompt for positive integer
// Get input
// test input value for gt 0 if (value > 0)
//Increment counter
//Accumulate sum Else
// display msg to enter a positive integer
// Prompt for positive integer
// Get input Endif
// End loop
//Calculate average
//Print the results (average)
End
Test Plan:
To verify this program is working properly the input values
could be used for testing:
3. Test Case
Input
Expected Output
1
1 1 1 0 1
2 0 1 3 2
Average = 1.2
2
100 100 100 100 -100
Input a positive value
100 200 -200 200 200
Input a positive value
200 200
average is 120.0
NOTE: test #2 has 12 input numbers because there are two
negative numbers.
Pseudocode: Main
// This program will calculate the average of 10 positive
integers.
// Declare variables
Declare count, value, sum as Integer Declare avg as double
//Initialize values
4. Set count=0 Set sum = 0 Set avg = 0.0;
// Loop through 10 integers While count < 10
Input value
If (value >=0)
sum = sum + value count=count+1
Else
Pr *** Value must be positive *** Input value
End if End While
// Calculate average avg = sum/count
// Print results
End //End of Main
C Code
The following is the C Code that will compile in execute in the
online compilers.
#include <stdio.h>
#include <stdbool.h> // needed for the Boolean variable
debug.
int main ()
{
/* variable definition: */ int count, value, sum; float avg;
bool debug;
/* Initialize */ count = 0;
sum = 0;
avg = 0.0; debug = false;
5. // Loop through to input values while (count < 10)
{
printf("Enter a positive Integern"); scanf("%d", &value);
if (debug) printf(" value is %dn " , value ); //note the value
of debug if (value >= 0) {
sum = sum + value; count = count + 1;
}
else {
printf("Value must be positiven");
}
}
// Calculate avg.
// Since sum and count are both integers, this will give you an
integer division
// Hence, we need to either
// Declare sum as a float or...
// type cast sum as float (as shown below) avg = (float)
sum/count;
printf("sum is %dn", sum); printf("average is %fn " , avg );
return 0;
}
Setting up the code and the input parameters in ideone.com:
Note the input integer values are 1, 1, 1, 0, 1, 2, 0, 1, 3, 2 for
this test case.
You can change these values to any valid integer values to
match your test cases.
You should also test with a negative number to make sure the
positive integer logic works properly.
Note: the input data needs to be entered into the stdin window
separated by a space of new line.
Results from running the programming at ideone.com:
6. Learning Exercises for you to try:
1. Change the code to average 20 integers as opposed to 10.
Support your experimentation with screen captures
of executing the new code. What happens if you change the
formatter to %.2f printf("average is %.2fn " , avg );
2. What happens if you entered a value other than an integer?
(For example a float or even a string). Support your
experimentation with screen captures of executing the code. To
help in your analysis try as input 1 2 3 a 4 5 Hint: activate this
line if(debug) printf(" value is %dn " , value ); after the scanf
statement that reads in the value. Activate by changing the
value of debug.
Next try as input 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.5
What happens?
3. Modify the code to allow the user to enter an unspecified
number of positive integers and calculate the average. In other
words, the user could enter any number of positive integers.
(Hint: You can prompt the user for how many they want to
enter. Or; you could use a sentinel value to trigger when the
user has completed entering values). You may need to conduct
some research on your own to solve this problem.
4. Prepare a new test table with at least 3 distinct test cases
listing input and expected output for the code you created.
Support your experimentation with screen captures of executing
the new code.
Loop (Repetition) with Nested selection n
statement
7. v Hide Description
(
10/3/2016
) (
Loop
(Repetition)
with
Nested
selection
statement
-
CMIS
102
6386
Introduction
to
Problem
Solving
and
8. Algorithm
Design
(2168)
)
Whne {Cost > 0) If (Cost > 150)
If (Staff > 12)
Fee = 110
Else
Fee = 115
Endlf
NESTED LOOPS, Ifs
Else
Fee = 12.0,
nplf
EndWhi le
Part 1:
Create a While-End repetition structure that includes a nested
if-then selection structure. Provide an overview (i.e. Program
Design) of what your repetition structure is doing as well as the
Pseudocode.
Create a program that handles 150 passengers of an Airline with
the following requirements: The first 25 passenger the ticket
price is $90. The remaining passengers the ticket price is $100.
The first 100 passengers get a ticket. The next 25 (i.e 101-
125) are put on standby. No more passengers are accepted after
9. 125. Print out a message for each of the above categories .
Part 2:
Convert Discussion 1 to C-code. Don't for get to indent your
code. Put Discussion 2 - problem no.X in the Subject area and
submit a .txt (or .c) file for your code.
1/2
CurrDate (i.e. 2016mmdd)
StartDate (i.e. 2016mmdd) // Mon start of week EndDate (i.e.
2016mmdd) // Sun end of week maxRecs 37 II the number of
data records
Here is what you have to work with. Don't worry about writing
the data, just setting the background color. Hint: you need to
compare the Date variables : CurrDate , StartDate, EndDate.
What is special about the Yellow background ? To set the Beige
background you need to work with prevYear and Year - Year
you determine from CurrDate, prevYear you need to set in your
loop appropriately.
Declare CurrDate, StartDate, EndDate, Year, prevYear,
maxRecs, recCount AS Integer Declare bckColor AS String
Set recCount = 0 Set prevYear = 0
Set bckColor = "Yellow" Set bckColor = "Pink"
Set bckColor = "LgtGreen" Set bckColor = 'DrkGreen" Set
bckColor = "Beige"
Set recCount = recCount + 1
11. Create an Parallel Arrays
PreviousNext
Hide Description
Parallel Arrays
In Pseudocode, declare two 1-dimensional parallel arrays to
store the information requested in the problems below.
Use a one loop to prompt the user for the information and
assign it to the parallel arrays.
Then use a second loop to display the values of the parallel
arrays.
Put Part 1 - problem no. (1,2,3,4,5,6,7,8) in the Subject area.
Collect the following information, State and Population
You may do additional problems, if you want.
Part 2:
Convert Discussion 1 to C-code. Don't for get to prototype your
function before the main and to define your function after the
main. Put Discussion 2 - problem no.X in the Subject area and
submit a .txt (or .c) file for your code.