943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 591
  • C++ RSS
Nov 12th, 2008
0

Need some help for a randomize code

Expand Post »
Hello DaniWeb members,

I need some help with a program I made that I couldn't figure out the logic to complete

the program should make 5 random numbers between "1 and 55" and those five numbers should:

*Have two or three even numbers (%66 even or %66 odd)

*Have two or three numbers higher than 28.

*the sum of all five numbers should be between 106 and 179

*Choose one two numbers from the same level "and not choose two levels "total of level chosen 4"

Level 1 1 : 9
Level 2 10: 19
Level 3 20: 29
Level 4 30: 39
Level 5 40: 49
Level 6 50: 55


I did make the program work where it should all the results, but couldn't figure how I could force the program to do the outputs I'm looking for.

here is a sample of the output I'm looking for
http://img511.imageshack.us/img511/7...eenshotoo6.png
any ideas?


thanks

Hassan







// PROJECT1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int q,r,s,t,u,v, w, x, y, z, pb;
int evens (int),highs (int);
int sum_evens=0, sum_highs=0,sumintegers=0, sum_level1=0,sum_level2=0,sum_level3=0,sum_level4=0,sum_level5=0,sum_level6=0;
int level1 (int), level2 (int), level3 (int), level4 (int), level5 (int), level6 (int);



int main() 
{ 

	// 1- Making Five Random Numbers
	int random_integer[5];
	srand((unsigned)time(NULL));
	for (int i = 0; i <5; i++)
	{
		random_integer[i] = 1+ rand() % 55;	
	}
		

	//2- Organizing the numbers from small to large
	for (int i = 1; i < 5; i++)
	{
		int currentElement = random_integer[i];
		int k;
		for (k= i-1; k >=0 && random_integer[k] > currentElement; k--)
		{
			random_integer[k+1] = random_integer[k];
		}
		random_integer[k+1]=currentElement;
	}

	
	//3- Having three or two even numbers
	for (int i = 0; i < 5; i++)
	{
		z= evens (random_integer[i]);
		sum_evens=sum_evens + z;
	}
	cout << "sum_evens : " <<  sum_evens << "\n"; // need to be forced to be two or three

					
	//4- Having two or three high/low numbers
		for (int i = 0; i < 5; i++)
		{
			w= highs (random_integer[i]);
			sum_highs=sum_highs + w;
		}
		cout << "sum_highs : " <<  sum_highs << "\n"; // need to be forced to be two or three
	

	//5- Having two numbers from the same level .. and skip two levels
		//level1
		for (int i = 0; i < 5; i++)
		{
			v= level1 (random_integer[i]);
			sum_level1=sum_level1 + v;
			u= level2 (random_integer[i]);
			sum_level2=sum_level2 + u;
			t= level3 (random_integer[i]);
			sum_level3=sum_level3 + t;	
			s= level4 (random_integer[i]);
			sum_level4=sum_level4 + s;
			r= level5 (random_integer[i]);
			sum_level5=sum_level5 + r;
			q= level6 (random_integer[i]);
			sum_level6=sum_level6 + q;
		}
		cout << "sum_level1 : " <<  sum_level1 << "\n"; // need to be forced to be two or three
		cout << "sum_level2 : " <<  sum_level2 << "\n";
		cout << "sum_level3 : " <<  sum_level3 << "\n";
		cout << "sum_level4 : " <<  sum_level4 << "\n";
		cout << "sum_level5 : " <<  sum_level5 << "\n";
		cout << "sum_level6 : " <<  sum_level6 << "\n";
		//6- Having the Sum of all integers


		for (int i = 0; i < 5; i++)
		{
			sumintegers= random_integer[i] + sumintegers;
		}
			cout << "sumintegers : " <<  sumintegers << "\n";

	
	//7-powerball number
	pb = 1+ rand() % 44;	 

	//8-Output the results
		for (int i = 0; i <5; i++)
	{
		cout << random_integer[i] << " - "; 
	}
		cout << "pb " << pb << "\n" ;
}
Last edited by Hassan87; Nov 12th, 2008 at 12:56 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hassan87 is offline Offline
2 posts
since Nov 2008
Nov 12th, 2008
0

Re: Need some help for a randomize code

Here are my functions

1- To count even numbers

#include "stdafx.h"
int evens (int x)
{

	if (x%2==0)
		{
			x=1;
		}
		else 
		{
			x=0;
		}
	return x;
}



2- To count numbers higher than 28

#include "stdafx.h"
int highs (int x)
{

	if (x>28)
		{
			x=1;
		}
		else 
		{
			x=0;
		}
	return x;
}


3- To count numbers from level1

#include "stdafx.h"
int level1(int x)
{

	if (x<10)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}


4- To count numbers from level2

#include "stdafx.h"
int level2(int x)
{

	if (x>9 && x<20)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}

5- To count numbers from level3
#include "stdafx.h"
int level3(int x)
{

	if (x>19 && x<30)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}
		


}


6- To count numbers from level4
#include "stdafx.h"
int level4(int x)
{

	if (x>29 && x<40)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}


7- To count numbers from level5
#include "stdafx.h"
int level5(int x)
{

	if (x>39 && x<50)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}



8- To count numbers from level5

#include "stdafx.h"
int level6(int x)
{
	if (x>49 && x<56)
	{
		x=1;
	}
	else
	{
		x=0;
	}
	return x;
}



and for the libraries I added those under "stdafx.h":

#include <iostream>
#include <ctime>
using namespace std;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Hassan87 is offline Offline
2 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: quantisation of image colours
Next Thread in C++ Forum Timeline: How do I Stop the program form listing empty variables?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC