William F. Slater, III
1409 N. Ashland Ave.
Chicago, IL  60622
United States of America

Home:    773-235-3080
Cellular:  312-758-0307

 

Home

CSS 561 Coursework Examples

 

Flowchart from Week 4

 

Simple C++ Programming Assignment  from Week 5

Specification: 
Write a small application that will ask a user to enter a series of numbers (with a minimum of two and a up to a maximum of 10).  Display the series of numbers back to the user, then show the average of the numbers (total of entries/ # of entries).  You may also calculate of fun things such as the median, etc.
 
Be sure to use arrays and loops.
 
Please post to the Assignments group by Wednesday, Day 7.

 

Program: (to see code formatted and indented, click here)

/////////////////////////////////////////////////////////////////////////////////////
//
// Program: Working with an Array, and Doing Calculations with the Elements
//
// Author: William F. Slater, III
//
// Description: When run, it requests input of from two to ten 
// integers from the keyboard, loading each into an array
// and then performs calculations on the array elements and prints 
// (1) the sum, 
// (2) the average, 
// (3) the ungrouped median
// (4) the product
// (5) the smallest value
// (6) the largest value
//
//
// Date: August 20, 2003
//
// File Name: CSS561Week5-William_Slater.cpp
//
/////////////////////////////////////////////////////////////////////////////////////

#include <iostream>
#include <stdlib.h>

void main()
{

// Declare variables -- These are listed alphabetically
char answer; // char variable to ask user if they want to repeat a run
double array0[10]; // array for storing first array, before sort
double array1[10]; // array for numbers
double average; // variable for average
int ArrayMax; // variable for maximum array size
int index1; // variable for array index
double largest; // variable for the largest array element value
double median; // variable for the median value
int next; // variable to reference the next element in the array
double numswitch; // variable used to switch numbers
double product; // variable used to store all elements multiplied together
double smallest; // variable for the smallest array element value
double sorted[10]; // array for sorted numbers
double sum; // variable used to store all elements added together
int top; // variable for top array element

answer = 'y';

// Remain in loop as long as user wants to run the program
while (answer != 'n')
{
// Section for Printing Heading
cout << endl;
cout << endl;
cout << "===================================================================" << endl;
cout << " **** Welcome to the Array Program ****" << endl;
cout << endl;
cout << endl;
cout << "You will be asked to enter between two and ten numbers." << endl;
cout << endl;
cout << "This program will perform sort and arithmetic operations, then print:" << endl;
cout << endl;
cout << "The array you just loaded. " << endl;
cout << endl;
cout << "And the array before and after the sort - for comparison. " << endl;
cout << endl;
cout << "The Sum of these numbers." << endl;
cout << endl;
cout << "The Average of these numbers." << endl;
cout << endl;
cout << "The Ungrouped Median Value of these numbers." << endl;
cout << endl;
cout << "The Product of these numbers." << endl;
cout << endl;
cout << "The Smallest Value of these numbers." << endl;
cout << endl;
cout << "The Largest Value of these numbers." << endl;
cout << endl;
cout << "Note 1. You must enter numbers for this to work properly." << endl;
cout << "===================================================================" << endl;
cout << endl;
cout << endl;

// Re-initialize variables
ArrayMax = 0;
index1 = 0;
// Remain in loop asking for array elements until all elements are input
// request number of elements, set this to ArrayMax
cout << endl;
cout << "Please enter the number of elements you want for this Array." << endl;
cout << endl;
cout << "This will be a number between 2 and 10: " ;
cin >> ArrayMax;
if (ArrayMax > 10 || ArrayMax == 1) {
cout << "Error - Array size must be at least 2 and less than or equal to 10. Please try again." << endl;
continue;
}

cout << endl;
cout << endl;
cout << endl;

// User Input Section for Array -- Request Array Elements and Load array1
while (index1 < ArrayMax)
{
cout << "Enter a value for array element number " << index1 << ": ";
cin >> array1[index1];
index1 = index1++;
}

cout << endl;
cout << endl;
cout << endl;

// Echo back the numbers to show input was received correctly. Also, fill another array.
// Re-initialize index1 first.
index1 = 0;
while (index1 < ArrayMax)
{
cout << "Array element number " << index1 << " = " << array1[index1] << endl;
array0[index1] = array1[index1];
index1 = index1++;
}

// Do the calculations. Re-initialize variables first.
index1 = 0;
sum = 0;
product = 1;
average = 0;

// Calculate the sum and product here
while (index1 < ArrayMax)
{
sum = array1[index1] + sum;
product = array1[index1] * product;
index1 = index1++;
}

// Sort array so we can find the largest and smallest, and calculate the median. Re-initialize variables first.
// After the sort, the first array element will be the smallest value;
// and the highest array element will be the largest value.
index1 = 0;
largest = 0;
next = 0;
numswitch = 0;
smallest = 0;

for (index1 = 0; index1 < ArrayMax; index1++)
{
for (next = index1; next < ArrayMax; next++)
{
if (array1[index1] > array1[next])
{
numswitch = array1[index1];
array1[index1] = array1[next];
array1[next] = numswitch;
}
} // end of inside For Loop
} // end of outside For Loop


// set the largest and smallest values
smallest = array1[0];
top = ArrayMax - 1;
largest = array1[top];

cout << endl;
cout << endl;
cout << endl;

// verification that the Sort Worked.
index1 = 0; // reset index1
while ( index1 < ArrayMax)
{
cout << "The old & new values of Array element no. " << index1 << " old = " << array0[index1] << " new = " <<array1[index1] << endl;
index1++ ;
}

// Calculate the average and median here
average = (sum/ArrayMax);
if (ArrayMax == 2)
median = average;
if (ArrayMax == 3)
median = array1[1];
if (ArrayMax == 4)
median = ((array1[1] + array1[2])/2);
if (ArrayMax == 5)
median = array1[2];
if (ArrayMax == 6)
median = ((array1[2] + array1[3])/2);
if (ArrayMax == 7)
median = array1[3];
if (ArrayMax == 8)
median = ((array1[3] + array1[4])/2);
if (ArrayMax == 9)
median = array1[4];
if (ArrayMax == 10)
median = ((array1[4] + array1[5])/2);

// The Output Section - with all the Answers to the Calculations.
cout << endl;
cout << "======================================" << endl;
cout << endl;
// Show Sum, Average, Median, Product of these array elements.
cout << "The Sum is " << sum << endl;
cout << endl;
cout << "The Average is " << average << endl;
cout << endl;
cout << "The Ungrouped Median is " << median << endl;
cout << endl;
cout << "The Product is " << product << endl;
cout << endl;
cout << "The Smallest value is " << smallest << endl;
cout << endl;
cout << "The Largest value is " << largest << endl;
cout << endl;
cout << endl;
cout << "======================================" << endl;
cout << "Want to load and analyze another array? (y for yes, n for no) " ;
cin >> answer;

} // End of big while loop

cout << endl;
cout << endl;
cout << endl;
cout << "*** Program is terminating *** " << endl;
cout << endl;
cout << " **** Thank you for your time and Goodbye! **** " << endl;

} // End of main()

Program Output:

===================================================================
**** Welcome to the Array Program ****


You will be asked to enter between two and ten numbers.

This program will perform sort and arithmetic operations, then print:

The array you just loaded.

And the array before and after the sort - for comparison.

The Sum of these numbers.

The Average of these numbers.

The Ungrouped Median Value of these numbers.

The Product of these numbers.

The Smallest Value of these numbers.

The Largest Value of these numbers.

Note 1. You must enter numbers for this to work properly.
===================================================================



Please enter the number of elements you want for this Array.

This will be a number between 2 and 10: 10



Enter a value for array element number 0: 3
Enter a value for array element number 1: 12
Enter a value for array element number 2: 1
Enter a value for array element number 3: 14
Enter a value for array element number 4: 77
Enter a value for array element number 5: 55
Enter a value for array element number 6: 47
Enter a value for array element number 7: 88
Enter a value for array element number 8: 90
Enter a value for array element number 9: 38



Array element number 0 = 3
Array element number 1 = 12
Array element number 2 = 1
Array element number 3 = 14
Array element number 4 = 77
Array element number 5 = 55
Array element number 6 = 47
Array element number 7 = 88
Array element number 8 = 90
Array element number 9 = 38



The old & new values of Array element no. 0 old = 3 new = 1
The old & new values of Array element no. 1 old = 12 new = 3
The old & new values of Array element no. 2 old = 1 new = 12
The old & new values of Array element no. 3 old = 14 new = 14
The old & new values of Array element no. 4 old = 77 new = 38
The old & new values of Array element no. 5 old = 55 new = 47
The old & new values of Array element no. 6 old = 47 new = 55
The old & new values of Array element no. 7 old = 88 new = 77
The old & new values of Array element no. 8 old = 90 new = 88
The old & new values of Array element no. 9 old = 38 new = 90

======================================

The Sum is 425

The Average is 42.5

The Ungrouped Median is 42.5

The Product is 3.01919e+13

The Smallest value is 1

The Largest value is 90


======================================
Want to load and analyze another array? (y for yes, n for no) n



*** Program is terminating ***

**** Thank you for your time and Goodbye! ****

 

 


Last Updated on Friday, August 29, 2003

Home