Basic Repetition finding max and minimum

Hello everyone, I gots a question. This is how I wrote it, the run and compiler works fine but here's the problem. Note this is homework.

1. The last two numbers I input are the answer to this code equation.
2. All numbers must be identified giving out only two answers besides the last two.

What must be used :

1. Repetition such as For and While.
2. This is just C++

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
#include <iostream>
#include <ctime>
#include <cstdlib>


	int times, s=0, l=0, val;
	cout << "How many numbers do you want to input?" << endl;
	cin >> times;
	
	for(int i = 0; i < times; i++)
	{
		cout << "Enter a number " << endl;
		cin >> val;
	if (val < rand()% 100 + 1)  // This is my range from 1 to 99.
	{
   		l = val; // L means largest
   	}
   	else
	   {
   		s = val; // S means smallest
	   }
	}
	
	cout << "The largest number was " << l << endl;
	cout << "The smallest number was " << s << endl;
	return 0;
}


Do you guys see my problem?
Last edited on
Your program isn't written right.
Main thing is it doesn't have main function at all! If you are using ctime, you should use the line srand(time(0)) for really, but I don't get why you need to use the rand function


If you only want to find the largest and smallest, you could do this:

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
#include <iostream>
using namespace std;

int main()
{

	int times, s, l=0, val;
	cout << "How many numbers do you want to input?" << endl;
	cin >> times;

	for(int i = 0; i < times; i++)
	{
		cout << "Enter a number " << endl;
		cin >> val;
		if (val>l)
            l = val;
        if (val<s)
            s = val;
	}

	cout << "The largest number was " << l << endl;
	cout << "The smallest number was " << s << endl;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.