Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2006, 7:55 PM   #1
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
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.
imagikricei is offline   Reply With Quote
Old Jun 8th, 2006, 8:03 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
#1 Use code tags
#2 Explain what is wrong with your code
Wizard1988 is offline   Reply With Quote
Old Jun 8th, 2006, 8:27 PM   #3
splinter9x
Hobbyist Programmer
 
splinter9x's Avatar
 
Join Date: Jun 2006
Posts: 137
Rep Power: 0 splinter9x is an unknown quantity at this point
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....
__________________
Visit my Blog
I support WINDOWS...
splinter9x is offline   Reply With Quote
Old Jun 8th, 2006, 8:29 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
#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
Kilo is offline   Reply With Quote
Old Jun 8th, 2006, 8:33 PM   #5
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 422
Rep Power: 4 Wizard1988 is on a distinguished road
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
Wizard1988 is offline   Reply With Quote
Old Jun 8th, 2006, 8:44 PM   #6
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
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.
imagikricei is offline   Reply With Quote
Old Jun 8th, 2006, 9:22 PM   #7
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Jun 8th, 2006, 10:13 PM   #8
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
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.
imagikricei is offline   Reply With Quote
Old Jun 8th, 2006, 10:14 PM   #9
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Jun 8th, 2006, 10:33 PM   #10
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
yeah it definately works i just dont understand the code.
imagikricei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC