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++
#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;
}
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:
#include <iostream>
usingnamespace 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;
}