Assistance with looping without array

Greetings, I am trying to write a program that takes in numbers and outputs only specific ones; highest number, lowest number, the total numbers entered, the sum of all numbers, and lastly the average of all numbers. This is program is to be done with arrays, so used a mixture of if-else & do-while statements. Unfortunately I have run into a halt as only three numbers will be processed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;

int main()
{
	float large, small, average, total, total2, sum, a, b, c;
	
	//the next entered third number
	char thirdnum, y, n;
	
	///do	{
	cout << "Please enter two numbers:";
	cin >> a >> b;
	
	//comparing for high & low
	if( a > b)
	{
        large = a;
        small = b;
    }
    else
        {
        	large = b;
        	small = a;
		}
	//calculate sum
	sum=a+b;	
	//calculate total numbers entered
    total = 2;
	//calculate average
	average = sum / total;

	cout << "Do you wish to enter more numbers? Enter Y or N to continue:";
	cin >> thirdnum;

	if (thirdnum = y);
    {
	cout << "Please enter you number:";	
	cin>>c;
	sum = sum + c;
	total = total + 1;
	average = sum / total;
	if( c > large)
	{
        large = c;      
    }
    else
        {
        	if (c < small);
        	small = c;
		}
	}	
	cout << "Do you wish to enter another numbers? Enter Y or N to continue:";
	cin >> thirdnum;
		
	
	
    if (thirdnum = n);
    {	
		cout << "The largest number is:" << large << "\n";
		cout << "The smallest number is: "  << small << "\n";
		cout << "The average is:" << average << "\n";
		cout << "The total number of numbers entered is:" << total << "\n";
		cout << "The sum of all numbers is:" << sum << "\n";
	}
			
    return 0;
}

Suggestions? Ideas? Assistance?
Thanks for anything provided.
Last edited on
Do consider:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

int main()
{
  // initialize
  float value {};
  size_t count {};

  // handle input
  while ( std::cin >> value ) {
    // do something with value
    ++count;
  }

  if ( 0 < count ) {
    // show results
  }

  return 0;
}
is it with or without? The title and post are mutex.
@jonnin This program to be done without the use of arrays. I have seen similar programs done using arrays however I wanted to do this without them.
The task is actually easier without arrays.

What would you add to my sample program in order to get the smallest value?
@keskiverto I make use comparisons of < and > as seen here
1
2
3
4
5
6
7
8
9
10
11
//comparing for high & low
if( a > b)
{
large = a;
small = b;
}
else
{
large = b;
small = a;
}
Which is ok, if you have exactly two values.

The sample program that I did post has only one value at a time, but you don't know how many times.

Besides, finding the largest and finding the smallest are independent operations, are they not? A single new value might be larger than anything before or smaller than anything before, but it could be both too (it is on the first value), and most of the time it is probably neither.

What would you add to my sample program in order to get the smallest value?
Would you need to initialize something before the loop?
What should you do in the loop with the value?
What should you show on line 16?
Couldn't you use a std::list as a container (which is not an array but a doubly linked list). Then you can iterate over the list and determine the high, low, average and total count using list iterators. It is not using an array but a list container so you can get the min val, max val, average (sum/count) and count with one full iteration through the list.
with one full iteration

For these computations one full iteration is indeed enough, but a list is not required any more than an array.

If you do look at this:
1
2
3
4
  while ( std::cin >> value ) {
    // do something with value
    ++count;
  }

you should notice that it does one full iteration over every input value.
Topic archived. No new replies allowed.