COS120 hw 4 - cin >> temp; sum += temp; //product *=...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ccd4dcad84fbb5b4d254aa36dc8f02ccd4d0ed9f.doc Page 1 of 2 COS120 Software Development Using C++ Student: Homework Nmr 04 (Loops) Tasks: Problem 1: Write a program that will find the sum and the average value in a collection of N numbers. Get the value of N before scanning each value in the collection of N numbers. Mark the modifications as comment lines in case you are asked to find the product of N numbers instead the sum. Problem 2: Modify your program to compute and display the range, i.e. the smallest and the largest values in the collection of N numbers. Problem 1 # include <iostream> using namespace std; int main (void) { int number, sum = 0, temp; float average; //int product = 1; cout << "Enter the number of elements: "; cin >> number; for (int i = 1; i <= number; i++) { cout << "Enter a number: ";
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cin >> temp; sum += temp; //product *= temp; } average = (float)sum/number; cout << "The sum is: " << sum << "\n"; cout << "The average is: " << average << "\n"; // cout << "The product is: " << product; return 0; } ccd4dcad84fbb5b4d254aa36dc8f02ccd4d0ed9f.doc Page 2 of 2 Problem 2 # include <iostream> # include <limits> using namespace std; int main (void) { int number, temp, min = INT_MAX, max = INT_MIN, range; cout << "Enter the number of elements: "; cin >> number; for (int i = 1; i <= number; i++) { cout << "Enter a number: "; cin >> temp; if (temp > max) max = temp; if (temp < min) min = temp; } cout << "The smallest number is: " << min << "\n"; cout << "The largest number is: " << max << "\n"; cout << "The range is: " << max - min; return 0; }...
View Full Document

This note was uploaded on 04/24/2010 for the course COS 120 taught by Professor Bonev during the Fall '08 term at American University in Bulgaria.

Page1 / 2

COS120 hw 4 - cin >> temp; sum += temp; //product *=...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online