Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 18.116.15.22
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0705) :  /home/cchansen/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/cchansen/assignment2_new.cpp
// Author:      Caitlin Hansen
// Instructor:  Dr. Wang
// Class:       Cs 212
// Due Date:    Feb. 9, 2009
// Compile:     g++ assignment2.cpp
// Run:         ./assign2.out
//
// Goal: Print the smallest, largest, and average number found in an array.
//


#include <iostream>
#include <iomanip>

using namespace std;

const int MAX = 10;

// Function Prototypes

void readNum(int data[]);
int gradeSmallest(int data[]);
int gradeLargest(int data[]);
float gradeAverage(int data[]);

int main()
{
	int num[MAX];
	int small;
	int large;
	float average;

	// read the data to array
	readNum(num);   // call function

	cout << fixed << setprecision(2);	// force fixed for floats

	// find the smallest
	small = gradeSmallest(num);
	large = gradeLargest(num);
	average = gradeAverage(num);

	// out
	cout << endl;
	cout << "The smallest is: " << small << endl;
	cout << "The largest is: " << large << endl;
	cout << "The average is: " << average << endl;
	cout << "\n\nDone.Bye-Bye.\n\n";
	return 0;
}

// function that reads 10 numbers to an array
void readNum(int data[])
{
	cout << "Input " << MAX << " integers: " << endl;
	
	for(int i = 0; i < MAX; i++)
		cin >> data[i];
}

// function to find the smallest
int gradeSmallest(int data[])
{
	int s = data[0];
	for(int i = 1; i < MAX; i++)
	{
		if(s > data[i])
			s = data[i];
	}
	return s;
}

// function to find the largest
int gradeLargest(int data[])
{
	int l = data[0];
	for(int i = 1; i < MAX; i++)
	{
		if(l < data[i])
			l = data[i];
	}
	return l;
}

// function to find the average
float gradeAverage(int data[])
{
	int sum = 0;
	float average = 0.0;
	for(int i = 0; i < MAX; i++)
		sum = sum + data[i];
	average = (static_cast<int>(sum * 100.0) / MAX) / 100.0;
	cout << "Average is: " << average << endl;
	return average;
}

Stv3n404 - 2023