0

generate 10 random integers in the range [- 50, 50 ], then outputs the largest among them. What i have tried is

#include <iostream>
#include <ctime>

using namespace std;

int main(){

    int startNum = -50;
    int endNum = 50;
    int rand = 0;
    int result = 0;

    srand(time(0));
    rand = (rand % (endNum - startNum +1 ) +startNum);
    int i = 0;
    int largestNum = 0;

    while (i <9){
        result = rand; 
        if (result > largestNum){
            largestNum = rand;
                i++;
        }

    }
        cout << "the largest number is " << largestNum << endl; 

    system("pause");
    return 0;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
Stella Jin
  • 15
  • 4
  • 1
    move i++ one line down (after your close bracket), and generate your random number inside the while loop (it's always the same number now) – Angel Koh Sep 25 '19 at 04:27
  • 2
    Your numbers are not random. – Eugene Sep 25 '19 at 04:28
  • You need to call the random number function and assign the value to `rand`. You want something like `result = rand();` Note the parenthesis after `rand`, to indicate a function call. – Thomas Matthews Sep 25 '19 at 04:28
  • 2
    You do know C++ provides its own [std::uniform_int_distribution](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) for generating random numbers? – David C. Rankin Sep 25 '19 at 05:43

2 Answers2

1

rand() is considered bad, mostly because you can't really generate an uniform distribution. I strongly recommend C++ random lib. Also you are not using rand(). I don't know what you are doing there.

Anyway, here is an elegant solution:

#include <iostream>
#include <random>
#include <algorithm>
#include <limits>

int main()
{
    std::random_device rd{};
    std::mt19937 eng{rd()}; // or std::default_random_engine e{rd()};
    std::uniform_int_distribution<int> dist{-50, 50};

    int largestNum = std::numeric_limits<int>::min();

    for (int i = 0; i < 10; ++i)
    {
        largestNum = std::max(largestNum, dist(eng));
    }

    std::cout << "the largest number is " << largestNum  << std::endl;
}
bolov
  • 72,283
  • 15
  • 145
  • 224
0

Similar questions have been already aswered but here's a complete working example for you:

int main()
{
    int i=0;
    int startNum = -50;
    int endNum = 50;
    int maxNum = startNum;

    std::cout << "The 10 random numbers:\n";
    while (i < 10)
    {
        int num = (rand() % (endNum - startNum + 1)) + startNum; 
        if (num > maxNum)
            maxNum = num;
        std::cout << num << "\n";
        ++i;
    }
    std::cout << "The largest number is: " << maxNum << "\n";

    return 0;
}

Although I don't really recommend using rand with loops. That's not the c++ way to do it. Check out this answer.

Zoltán
  • 678
  • 4
  • 15
  • 1
    That will not be very random unless you seed the random number generator by calling `srand` before your first call to `rand`? – David C. Rankin Sep 25 '19 at 05:44
  • It does give you different numbers, even if you don't seed it. I think The the accent was on how to do it, not on the numbers itself. If you want to do it the C++ way (the right way in my opinion), you should do it as this answer: https://stackoverflow.com/a/26301629/2166946 – Zoltán Sep 25 '19 at 07:20
  • 1
    Yes, but it will give you the very same different numbers in the same order every time (without `srand`). Technically, you are correct. But at least with `srand` (even if you give it the same seed) it tends to give a much better distribution. – David C. Rankin Sep 25 '19 at 07:24
  • You are also right, if you need to use those numbers for something useful, you need to seed it. Preferably with various seeds, as you said (like current time), unless you want to generate the same "random" numbers over and over – Zoltán Sep 25 '19 at 07:30
  • Maybe you should point out that the OP aren't using `rand()` *at all*. – Bob__ Sep 25 '19 at 07:58