Dice c++

Hello! I have a c++ assignment, im supposed to make a program that will throw 6 dices then find out the total sum and find and point out the highest and lowest number. I can't figure out how to find the lowest and highest number and would like some help! :)

Here is the code i currently have!

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int dice();
int getResult();
int main()
{
getResult();
}
int getResult()
{
srand(time(0));
int myarray[1];
for (int i =0; i<1; i++)
{
int number = dice();
myarray[i] = number;
cout << number << endl;

}
}
int dice()
{

int dice1 = (int)(1+(rand()%6));
int dice2 = (int)(1+(rand()%6));
int dice3 = (int)(1+(rand()%6));
int dice4 = (int)(1+(rand()%6));
int dice5 = (int)(1+(rand()%6));
int dice6 = (int)(1+(rand()%6));


cout << dice1 <<endl;
cout << dice2 <<endl;
cout << dice3 <<endl;
cout << dice4 <<endl;
cout << dice5 <<endl;
cout << dice6 <<endl;


int diceSum = dice1 + dice2 + dice3 + dice4 + dice5 + dice6;
return diceSum;
}




Are you supposed to be using arrays for this assignment? I see you have an array called 'myarray', but it only has a size of 1, so you aren't using it to its potential.

You don't actually need an array to keep track of the sum, min, and max. You just need a loop.

declare variable for "lowest number so far", set it to an impossibly high value to begin with, like 7
declare variable for "highest number so far", set it to an impossibly low value to begin with, like -1
declare variable for sum, set it to 0
do 6 times:
    get random number [1, 6]

    (1) if random number is less than current known lowest number
           set current known lowest number to this number

    (2) if random number is greater than current known greatest number
           set current known greatest number to this number

    (3) add number to sum

Last edited on
Topic archived. No new replies allowed.