![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0
![]() |
Finding Random Min and Max
I need help finding a minimum and maximum of random numbers.
what my program does it asks for a number and it outputs that many random numbers between the range of 0-100. It also finds the number of values, sum and the average. what im having problem is i dont know how to output the maximum and minimum values of the generated random numbers. any ideas guys? #include <cstdlib>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
int main()
{
double n, sum=0;
cout << "Enter a number: " << endl;
cin >> n;
unsigned seed = time(NULL);
srand(seed);
int range = 101;
for (int i = 0; i < n; i++)
{
int myrand = rand()/100%range;
cout << myrand << " " << endl;
sum += myrand;
}
cout << "There are " << n << " values." << endl;
cout << "The sum is " << sum << endl;
cout << "The average is " << (sum/n) << endl;
return 0;
}Last edited by imagikricei; Jun 8th, 2006 at 8:13 PM. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
#1 Use code tags
#2 Explain what is wrong with your code |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2006
Posts: 137
Rep Power: 0
![]() |
He did explain, he wants to know how to do somthing, this is not a code error... And he did use code tags, hence the code being in the box....
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
#include <iostream>
#include <conio.h>
int main()
{
int i_numArray[5] = { 45, 12, 67, 34, 96 };
int i_min = 100, i_max = 0;
for ( int i = 0; i <= 4; i++ )
{
if ( i_numArray[i] != i_min && i_numArray[i] < i_min )
i_min = i_numArray[i];
else if ( i_numArray[i] != i_max && i_numArray[i] > i_max )
i_max = i_numArray[i];
}
std::cout << "Min: " << i_min << std::endl;
std::cout << "Max: " << i_max;
getch();
return 0;
}output: Min: 12 Max: 96 splinter9x: he could have edited his post?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4
![]() |
You need to use if statements and compare the numbers each time they are generated. Keep the lowest one in one variable and the largest number in another. Next simply compare each of the following numbers which were generated to those.
Just Like Kilo did ![]() |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0
![]() |
your numbers were already set. i need it to find max and min of random numbers.
i was told this function works fairly well with finding max and min. problem is i dont know how to put it into my code. can somebody help me out. #include <algorithm> ... int x1 = 10, x2 = 20, minval, maxval; minval = min(x1, x2); maxval = max(x1, x2); hey kilo, i tested your source and it only output the min Last edited by imagikricei; Jun 8th, 2006 at 8:56 PM. |
|
|
|
|
|
#7 |
|
Expert Programmer
|
here!
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
double n, sum=0;
std::vector<int> v_numArray(0x0);
int i_min = 100, i_max = 0;
cout << "Enter a number: " << endl;
cin >> n;
unsigned seed = time(NULL);
srand(seed);
int range = 101;
for (int i = 0; i < n; i++)
{
int myrand = rand()/100%range;
cout << myrand << " " << endl;
v_numArray.push_back(myrand);
sum += myrand;
}
cout << "There are " << n << " values." << endl;
cout << "The sum is " << sum << endl;
cout << "The average is " << (sum/n) << endl;
for ( int i = 0; i < v_numArray.size(); i++ )
{
if ( v_numArray[i] != i_min && v_numArray[i] < i_min )
i_min = v_numArray[i];
else if ( v_numArray[i] != i_max && v_numArray[i] > i_max )
i_max = v_numArray[i];
}
cout << "Min: " << i_min << endl;
cout << "Max: " << i_max << endl;
getch();
return 0;
}im not using algorithm.. so use my code or have someone else do it?
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0
![]() |
thanks for the help kilo. its just that i haven't learned how to use what you're using in your code yet. but thanks again for the help.
|
|
|
|
|
|
#9 |
|
Expert Programmer
|
does it work ok for you??
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0
![]() |
yeah it definately works i just dont understand the code.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|