Description
Overview
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, design (using pseudocode), and implementation with C code. The example provided uses sequential and selection statements.
Program Description
This program will calculate the sum and average of 5 integers. The program will ask the user to 5 integers. If the average of the numbers is greater than 100, a message is printed stating the average is over 100. The design step will include both pseudocode.
Analysis
I will use sequential, and selection programming statements.
I will define six integer numbers: value1, value2, value3, value4, value5 and sum.
I will define a float number: average.
The value1, value2, value3, value4 and value5 variables will store the integers input by the user.
The sum will store the sum of the 5 values.
The average will store the average of the 5 values.
The sum will be calculated by this formula:
sum = value1 + value2 + value3 + value4 + value5
The average will be calculated using this formula:
average = sum / 5
For example, if the first values entered were value 1=1, value 2=1, value 3=2,value 4=2 and value 5=3 respectively:
sum = 1 + 1 + 2 + 2 + 3 = 9
average = 9/5 = 1.8
The additional selection statement will be of this form:
If average > 100 then
print “Average is over 100”
End If
1
To verify this program is working properly, the following input values could be used for testing:
Test Case |
Inputs |
Sum |
Average |
Average Over 100? |
1 |
11223 |
9 |
1.8 |
No |
2 |
11102 |
5 |
1 |
No |
3 |
100 100 100 100 200 |
600 |
120 |
Yes |
4 |
-100 -100 -200 0 200 |
-200 |
-40 |
No |
Pseudocode
-
This program will calculate the sum of 5 integers.
-
Declare variables
Declare value1, value2, value3, value4, value5, sum as Integer
Declare average as float
//Initialize Sum to 0
Set sum = 0
-
Enter values
Print “Enter an Integer for value1” Input value1
Print “Enter an Integer for value2”
Input value2
Print “Enter an Integer for value3”
Input value3
Print “Enter an Integer for value4”
Input value4
Print “Enter an Integer for value5”
Input value5
-
Calculate sum
sum = value1 + value2 + value3 + value4 + value5 average = sum / 5
-
Print results and messages Print “Sum is: “ + sum
Print “Average is: “ + average
If (average > 100)
Printf “Average is over 100”
End if
2
The following is the C Code that will compile in execute in the online compilers.
-
C code
-
This program will calculate the sum and average of 5 integers.
-
Developer: Faculty CMIS102
-
Date: Jan 31, XXXX
#include <stdio.h>
int main ()
{
/* variable definition: */
int value1,value2,value3,value4,value5,sum; double average;
/* Initialize sum */
sum = 0;
printf(“Enter an Integer for value1\n”); scanf(“%d”, &value1);
printf(“Enter an Integer for value2\n”); scanf(“%d”, &value2);
printf(“Enter an Integer for value3\n”); scanf(“%d”, &value3);
printf(“Enter an Integer for value4\n”); scanf(“%d”, &value4);
printf(“Enter an Integer for value5\n”); scanf(“%d”, &value5);
sum = value1 + value2 + value3 + value4 + value5;
average = sum / 5.0;
printf(“Sum is: %d\n ” , sum );
printf(“Average is: %f\n ” , average);
if (average > 100)
printf(“Average is over 100\n”);
return 0;
}
3
Results from running within repl.it
Setting up the code and the input parameters in ideone.com:
Note the input integer values are 100, 100, 100, 200 and 100, for this test case.
You can change these values to any valid integer values to match your test cases.
Conversation Panel:
4
Learning Exercises for you to complete
-
Demonstrate you successfully followed the steps in this lab by preparing screen captures of you running the lab as specified in the Instructions above.
-
Change the C code to sum 10 integers as opposed to 5?
Support your experimentation with a screen capture of executing the new code NOTE: Please don’t use arrays or Loops for this.
We will be using those features in another week.
-
Modify the code to print an additional selection statement if the sum of the values are negative. Support your experimentation with a screen capture of executing the new code.
-
Prepare a new test table with at least 3 distinct test cases listing input and expected output for the selection statement you created in the previous exercise.
-
Create your own C code implementation of one of the following mathematical formulas:
-
-
y = mx + b; (slope of a line) Assume the user will be prompted to input m, x and b and the program will calculate y. If the value of y is greater than 10, inform the user the value is greater than 10.
-
-
-
a = PI * r*r; (area of circle). Assume the user will be prompted to input the radius r. You can define PI as 3.1416. . If the value of a is greater than 10, inform the user the value is greater than 10.
-
-
-
v = 4/3 PI r*r*r; (volume of sphere) Assume the user will be prompted to input the radius r. You can define PI at 3.1416. If the value of v is greater than 10, inform the user the value is greater than 10.
-
You should consider the difference between the following lines of code:
v = 4.0/3 * PI * r*r*r;
v = 4/3 * PI * r*r*r;
Be sure you provide not only the C code but a test table with at least 3 distinct test cases listing input and expected output your mathematical formula.
For better precision, you might consider using the value of π provided by the C math.h library:
#include <math.h>
area = M_PI * r * r; // for example
5
Submit a neatly organized word (or PDF) document that demonstrates you successfully executed this lab on your machine using an online compiler. You should provide a screen capture of the resulting output. Submit all C code you created in files.
Also, provide the answers and any associated screen captures of your successful completion of all exercises.
Submit your document no later than the due date listed in the syllabus or calendar.
Grading guidelines |
||
Submission |
Points |
|
Demonstrates the successful execution of this Lab within an |
20 |
|
online compiler. Provides supporting screen captures. |
||
Modifies the C code to sum 10 integers as opposed to 5. |
20 |
|
Support your experimentation with screen captures of |
||
executing the code. |
||
Modifies the code to print an additional statement if the sum |
20 |
|
of the values are negative. Support your experimentation |
||
with a screen capture of executing the new code. |
||
Provides a new test table with at least 3 distinct test cases |
10 |
|
listing input and expected output for the selection statement |
||
you created in the previous exercise. |
||
Creates your own unique C code implementation of one of |
20 |
|
the provided mathematical formulas. Provides a new test |
||
table with at least 3 distinct test cases listing input and |
||
expected output for your mathematical formula. Supports |
||
your experimentation with screen captures of executing the |
||
new code. |
||
Document is well-organized, and contains minimal spelling |
10 |
|
and grammatical errors. |
||
Total |
100 |
|
6