-1

Can anyone tell me why my smallest value variable always comes out to 0 in the first set of random numbers? This program generates a random number between 2 and 15, then uses that number to determine how many random numbers will be in a set, those numbers with a range of 1-100.

Here is my code:

#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstdlib>

using namespace std;

const int SEED_VALUE=5;
const int MIN_SET_1=2;
const int MAX_SET_1=15;
const int MIN_SET_2=1;
const int MAX_SET_2=100;

int main()
{
int rand_num_1, rand_num_2, sum_of_squares=0, square_of_sum=0,sum=0;
float average=0, standard_deviation=0;
string again_value;

srand(SEED_VALUE);

do 
{
    cout<<"The random number are: ";
    int smallest_num=rand_num_2, largest_num=rand_num_2;

    rand_num_1=MIN_SET_1+(rand()% (MAX_SET_1-MIN_SET_1+1));

    for (int i=0; i<=rand_num_1-va1; i++)
    {
        rand_num_2=MIN_SET_2+(rand()% (MAX_SET_2-MIN_SET_2+1));
        cout<<rand_num_2<<"  ";
    
        sum+=rand_num_2;
        sum_of_squares+=(rand_num_2*rand_num_2);
    
        if (rand_num_2<smallest_num)
        {
            smallest_num=rand_num_2;
        }
    
        if (rand_num_2>largest_num)
        {
            largest_num=rand_num_2;
        }
    }

    square_of_sum=sum*sum;
    average=sum/rand_num_1;
    standard_deviation=sqrt((sum_of_squares-(square_of_sum/rand_num_1))/(rand_num_1-1));

    cout<<endl;
    cout<<"Number of values:"<<setw(12)<<rand_num_1<<endl;
    cout<<"Smallest:"<<setw(20)<<smallest_num<<endl;
    cout<<"Largest:"<<setw(21)<<largest_num<<endl;
    cout<<"Sum:"<<setw(25)<<sum<<endl;
    cout<<setprecision(2)<<fixed;
    cout<<"Average:"<<setw(21)<<average<<endl;
    cout<<"Standard deviation:"<<setw(10)<<standard_deviation<<endl<<endl;

    cout<<"Again (Y/N)? ";
    cin>>again_value;
    cout<<endl;

    sum=0;
    average=0;
    standard_deviation=0;
}
while (again_value=="Y"|| again_value=="y");

return 0;
}
Syd
  • 11
  • 1
    `int smallest_num=rand_num_2, largest_num=rand_num_2;` you set the value of `smallest_num` and `largest_num` before you set the value of `rand_num_2` this is undefined behavior to make use of a variable that has not been initialized. – drescherjm Oct 05 '22 at 23:17
  • what is the variable va1 supposed to represent? It is not initialized nor is it declared anywhere in the block that you have posted. Also, you should initialize the values of smallest_num and largest_num before running the do-while loop as @drescherjm suggested. – spencer Oct 05 '22 at 23:22
  • Can you explain what you understand to be what `srand()` does, and what results you can expect if it's always called with the same value, as the shown code does. – Sam Varshavchik Oct 05 '22 at 23:25
  • I moved the smallest_num and largest_num declarations to the for loop right after rand_num_2 is created, but now i get the errors 'smallest_num' was not declared in this scope, same for largest_num. – Syd Oct 05 '22 at 23:32
  • spencer- that was a typo I accidentally made when copying and pasting my code. – Syd Oct 05 '22 at 23:32
  • 1
    Sam, srand sets the seed value to 5, something that's required for my assignment. – Syd Oct 05 '22 at 23:33
  • Groovy, give the instructor what they asked for, and in this case what they've asked or makes a certain amount of sense. But what Sam's getting at is do you know what the effect of using a fixed value in `srand` is? He's highlighting it because it's a great place to make a mistake if you don't know what to expect. – user4581301 Oct 06 '22 at 00:01
  • From what I believe srand initializes the random number generator. However, I do not fully understand what the integer value does - in this case it is 5. – Syd Oct 06 '22 at 00:08
  • You don't really get random numbers, at least not from `rand`. Usually generating random numbers is expensive, so we use a little hack. We generate the numbers based on a seed value, and we usually spend the time to get a really random seed. The number you feed into `srand` is the seed. If you know the seed, five, and you know the algorithm, you know the numbers that will be generated. Your instructor wants to get the same sequence for all of the students so that they can get the same results for all correct programs. This makes the marking far easier. – user4581301 Oct 06 '22 at 01:16
  • Worth noting that `rand` is very weakly specified and not all library implementations use the same algorithm for `rand`, so if you're not using the same tools, you can't count on getting the same sequence. Assuming the instructor uses the same tool chain for all of their marking, they'll get the same sequence, but when you're compiling with your tools you could easily get a different sequence. Some implementations of `rand` are pretty much worthless. [Obligatory XKCD link](https://xkcd.com/221/) that isn't too far off the mark. – user4581301 Oct 06 '22 at 01:22
  • ahh, that makes a lot of sense. So what should I do to fix my program? Is it simply moving the smallest_num and largest_num initializations and if so, where should they go? – Syd Oct 06 '22 at 02:29
  • I'd start with the initialization, something like `int smallest_num = INT_MAX, largest_num = INT_MIN;` ought to work, and then step through the program with a debugger to see where else the program does stuff you don't expect like store the wrong value or take the wrong path. The unexpected is a mistake in the code or in your expectations. Either way you need to fix it. – user4581301 Oct 06 '22 at 18:30
  • Are you already aware of [what is the behavior of integer division?](https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division?) – Bob__ Oct 06 '22 at 23:09

1 Answers1

0

hey I am working on the same assignment. try setting smallest_num= to a number outside the range of 0-100 to get the right smallest number.