functions

i have this practical assignment due tomorow heres the question for the prac :

Write a C++ version of the game called BEAT THAT! In this game, a player will roll four dice. The
program must generate a random number between 1 and 6 for four different dice. Then the
program must sort these four numbers in such a manner that the largest possible number is formed.
This must happen until the user wishes to stop (you must ask the user if they wish to continue).
Check if the new number is greater than the previous number. For example:
* * * * * *
* * * *
* * * * * *
5 2 6 3 rearranged to make the highest number is 6532.
Your program must have at least two functions. One function called DICE_ROLL which generates and
returns a random number between one and six, and another called ARRANGE which takes four
parameters and returns the largest possible number.
For bonus marks – output the dice rolls in the ‘star style’ shown above every time the dice is rolled.
Each roll may be on a new line.



============================================

so far this is the code i got pls helpppp................

+++++++++++++++++++++++++++++++++++++++++++++++++++++





#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;



int DICE_ROLL(int RandNum );
void ARRANGE (int a , int b , int c , int d );



int main()

{
int w , x , y , z ;
char run;

while (run ='y')
{

w=DICE_ROLL(w);
x=DICE_ROLL(x);
y=DICE_ROLL(y);
z=DICE_ROLL(z);


ARRANGE (w,x,y,z);
cout<<w<<x<<y<<z<<endl;




cout<< "Press y to dice again or pree any other key to exsit"<<endl;
cin>>run;
if (run != 'y')
{
return 0;
}
}

return 0;


}

int DICE_ROLL(int RandNum )
{
RandNum = rand () % 6+1;


return RandNum;
}

void ARRANGE(int a , int b , int c , int d )
{
Last edited on
I am new to programming too.
This is how i sort the numbers

void arrange(int d1, int d2, int d3, int d4) {
for (int x = 6; x > 0; x--) { // each iteration reduce x by 1.
if (d1 == x)
cout << d1;
if (d2 == x)
cout << d2;
if (d3 == x)
cout << d3;
if (d4 == x)
cout << d4;
}

good luck
thanks so heres how the new code looks...
btw does any1 knw hw to check if the numbers generated are larger then the previous num generated

and to display a msg to say if larger or not...
==================================



#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;



int DICE_ROLL(int RandNum );
void ARRANGE(int d1, int d2, int d3, int d4) ;



int main()

{
int w , x , y , z ;
char run;


cout<<"<*****BEAT THAT****>"<<endl;
cout<<endl;

while (run ='y')
{;
w=DICE_ROLL(w);
x=DICE_ROLL(x);
y=DICE_ROLL(y);
z=DICE_ROLL(z);


ARRANGE (w,x,y,z);
cout<<endl;
cout<<endl;



cout<< "Press y to dice again or pree any other key to exsit"<<endl;
cin>>run;
if (run != 'y')
{
return 0;
}
}

return 0;


}

int DICE_ROLL(int RandNum )
{
RandNum = rand () % 6+1;


return RandNum;
}

void ARRANGE(int d1, int d2, int d3, int d4)
{
for (int x = 6; x > 0; x--)
{ // each iteration reduce x by 1.
if (d1 == x)
cout << d1;
if (d2 == x)
cout << d2;
if (d3 == x)
cout << d3;
if (d4 == x)
cout << d4;

}


}
Don't check the numbers AS they are generated there is no need to, sort them afterward. In other words don't roll a 1 then roll a 2 then check if 2 is bigger then 1 before rolling a 5 then checking if 5 is bigger then one, then checking if 5 is bigger then 2 before rolling... Get ALL of the numbers first then create a sorting function.

Why are we passing a variable to int DICE_ROLL only to reassign the value of that variable within the same function at the next line of code? Am I burning out again? It's possible I know I don't sleep enough and often make an @** of my self.

Also the rand() function is only a psuedo random number generator.

You have a spare ';' after while (run ='y')
Look up some sorting algos online, they'll be more reliable than that silly for loop... thing. Pseudocode will often be available. Bubble sort is nice and easy to code, it should be more than enough for this application.
Topic archived. No new replies allowed.