#include <iostream> 
using namespace std; 

int main() 
{ 

int num, greatest, least; 
char doAgain; 

do 
	{ cout << "This program lets you input a series of numbers and finds\nthe greatest and least values.\n"; 
	cout << "Type in a value. -99 will finish the series.\n";
	cin >> num;

if (num == -99)
	cout << "\nNo values were entered, so there is no smallest and greatest value.\n";
	greatest = num, least = num;

while (num != -99)
{
	if (num > greatest)
	greatest = num;
	else if (num < least)
	least = num; 

	cout << "Enter another value.\n";
	cin >> num;
}
if (greatest != -99 && least != -99)
	{ cout << "\nSmallest value: " << least << endl; 
	cout << "Greatest value: " << greatest << endl;}

	cout << "\nWould you like to do this again?(Y/N)\n"; 
	cin >> doAgain; 
	cout << endl;}
while (doAgain == 'y' || doAgain == 'Y');

return 0; 

}

The code asks for a series of integers and when -99(sentinel) is typed in it will find the greatest and smallest values.

The code works as I want, but I need to add a way for num's multiple values to be outputted along with the greatest and smallest values. With the class I'm taking, I've only learned multiple loops, if/else statements/switch, relation/logical operaters, not too complicated things, I've been having trouble writing a simple code that will do this. Huge kudos to anyone that can help or atleast direct me into the right path. This has been killing me for a while not being able to figure it out.

Recommended Answers

All 3 Replies

use an array and a for loop

see an example

int num[size];

for(int i=0;i<size;i++)
{
   
   cout << "Enter another value.\n";
  cin >> num[i];
   if (num[i] > greatest)
   greatest = num[i];
   else if (num[i] < least)
   least = num[i]; 
}

cout << " and to output " << endl;

for(int i=0;i<size;i++)
{
   cout << num[i]; // thats will output all of    the values 
}

This for example purpose only I may have syntax or logic error but you got the idea

for more information :
http://www.cplusplus.com/doc/tutorial/arrays.html

I haven't gotten into arrays yet, but it seems like that's the way to go. For this, how would I state tha size can be unlimited? It's a series of integers, so I couldn't set it to a constant.

then you need to use dynamic arrays

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.