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;
}