How to design a program using Array's

Pages: 12
Imagine you are a computer programmer for a plastics company called “Charlie’s plastics”. One day, your boss comes to you with a problem.

Here’s what he says:

As you likely know, dice have 6 sides. Each side has a different number (1-6). Statistically speaking, if you were to roll one, the odds of any number coming up with be 1 in 6 or 16.6%. This means that if you were to roll one die 30 times, you should end up with five 1s, five 2s, five 3s and so forth.

The problem is that our new, upgraded dice-making machine might not be making them right. We need to test a bunch of dice to make sure that there aren’t any issues.

He goes on to explain that he will hire some high schoolers to come in during the afternoon shift and roll the dice over and over again. He needs a program that will allow them to enter the number rolled every time. The program will use an array to keep track of all of the rolls and it needs to be able to keep track of 50 entries. Once all 50 numbers have been entered into the array, 6 variables will be used to keep track of how many of each number have been rolled.

The output will show the 6 numbers and how many of each were rolled. Then, it will display the entire array of numbers (all 50 of them)

Sample output:

Number of times 1 was rolled: 9
Number of times 2 was rolled: 6
Number of times 3 was rolled: 11
Number of times 4 was rolled: 10
Number of times 5 was rolled: 6
Number of times 6 was rolled: 8

Array of numbers
3
6
4
4
3
1
And so on…
Last edited on
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.
This classic homework assignment is meant to teach the student two main things:
1. The first element of an array is indexed at position zero
2. Arrays can be used in abstract ways

I would also like to point out that this assignment can also show the importance of data validation.

Dice Counter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstring>
#include <iostream>
#include <limits>
#include <string>

int getNumber(int minimum, int maximum);
void output(int* rolls, const int DICE_SIZE);
bool validateInput(int userChoice, int minimum, int maximum, std::string message);

int main()
{
    const int DICE_SIZE = 6;
    const int NUM_OF_ROLLS = 50;

    int rolls[DICE_SIZE] = { 0 };

    for(int i = 0; i < NUM_OF_ROLLS; i++)
    {
        std::cout << "What is your number? ";
        /* Notice:
            - The less obvious use of an array
            - The need to reduce the number by one to account for the index starting at zero
            Recall:
            - intVar++ increases the value of intVar by one (a.k.a: intVar = intVar + 1; )
        */
        rolls[getNumber(1, DICE_SIZE) - 1]++;
    }

    output(rolls, DICE_SIZE);
}


int getNumber(int minimum, int maximum)
{
    int choice;
    std::string msg = "Must be between " + std::to_string(minimum) + " and " + std::to_string(maximum);
    bool valid = false;

    do
    {
        std::cin >> choice;
        valid = validateInput(choice, minimum, maximum, msg);
    }
    while(!valid);


    return choice;
}

void output(int* rolls, const int DICE_SIZE)
{
    std::cout << std::endl << "Output:" << std::endl;
    for(int i = 0; i < DICE_SIZE; i++)
    {
        std::cout << "Number of times " << i + 1 << " was rolled: " << rolls[i] << std::endl;
    }
}

bool validateInput(int userChoice, int minimum, int maximum, std::string message)
{
    bool valid = true;

    if ((std::cin.fail()) || ((userChoice < minimum) || (userChoice > maximum)))
    {
        std::cout << message << std::endl;
        valid = false;
    }

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // #include <limits>

    return valid;
}


I hoped this helped and be sure to read the code carefully.
Last edited on
well can you please add me on facebook or email me and teach me how to do so
@BrandonStaab, please don't do other people's homework for people. Just help them.
I agree with @integralfx, don't post your homework here, but ask for help. If you didn't know how to use arrays, just ask how to use arrays. If you don't know how to use arrays go here: http://www.cplusplus.com/doc/tutorial/arrays/

I apologize if I was a little harsh,
VX
Last edited on
@UgoChi which lines would you like explained?
@VX0726 You was not harsh at all and thanks I will not make the same mistake in the future
@BrandonStaab

The formula of the dices roll how will I write that. I'm good at starting off the program and ending it. But when it comes to the meat and potatoes of the program I mess up
closed account (48T7M4Gy)
I'm good at starting off the program and ending it.

ROFL. A new dimension of the law of the excluded middle.
@UgoChi
The formula of the dices roll how will I write that.


I have already provided the answer to the homework assignment so there shouldn't be to much confusion on how the program operates unless the syntax is hard to understand(which i can provide better documentation if you would like).

The way the program works is as follows:
1. Ask for the number rolled
2. Increase the amount of rolls for that number
3. Repeat a predefined amount of times
4. Tell the user how many of each type of rolls, were rolled

I wouldn't give much headache to understanding the validateInput() function just know that it is essential to do when getting any information from an outside source such as a user, file, internet, ect.

If you still don't understand what is going on please provide line numbers or concepts that you would like to be explained.
Hello,

I'm a beginner at programming and I found your homework question to be quite interesting. Thus, I tried to create a programme of my own based on my knowledge to answer your question. I thought it might help you to understand more about programming too?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

int main()
{
    int diceEntry[50];
    cout << "Number of rolls --- Number rolled \n";
    for (int x = 0; x < 50; x++)
    {
        int y = x + 1;
        cout << "            " << y << " ----- ";
        cin >> diceEntry[x];
            while(diceEntry[x] != 1 && diceEntry[x] != 2 && diceEntry[x] != 3 && diceEntry[x] != 4 && diceEntry[x] != 5 && diceEntry[x] != 6)
            {
                cout << "Please type the dice's number( 1 - 6 )\n";
                cout << "            " << y << " ----- ";
                cin >> diceEntry[x];
            }
        cout << endl;
    }
int check1 = 0;
int check2 = 0;
int check3 = 0;
int check4 = 0;
int check5 = 0;
int check6 = 0;

    for (int x = 0; x < 50; x++)
    {
        if(diceEntry[x] == 1) {check1 += 1;}
        if(diceEntry[x] == 2) {check2 += 1;}
        if(diceEntry[x] == 3) {check3 += 1;}
        if(diceEntry[x] == 4) {check4 += 1;}
        if(diceEntry[x] == 5) {check5 += 1;}
        if(diceEntry[x] == 6) {check6 += 1;}

    }

cout << "Number of times 1 was rolled: " << check1 << endl;
cout << "Number of times 2 was rolled: " << check2 << endl;
cout << "Number of times 3 was rolled: " << check3 << endl;
cout << "Number of times 4 was rolled: " << check4 << endl;
cout << "Number of times 5 was rolled: " << check5 << endl;
cout << "Number of times 6 was rolled: " << check6 << endl;
}
closed account (48T7M4Gy)
Where you get lots of duplication you can use loops and arrays to advantage. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

int main()
{
    int limit = 10;// for testing convenience only
    
    int diceEntry[50] = {0};
    
    cout << "Number of rolls --- Number rolled \n";
    
    for (int x = 0; x < limit; x++)
    {
        int y = x + 1;
        cout << "            " << y << " ----- ";
        cin >> diceEntry[x];
        while(diceEntry[x] < 1 and diceEntry[x] > 6)
        {
            cout << "Please type the dice's number( 1 - 6 )\n";
            cout << "            " << y << " ----- ";
            cin >> diceEntry[x];
        }
        cout << endl;
    }
    
    int check[6] = {0};
    
    for (int x = 0; x < limit; x++)
    {
        check[diceEntry[x]]++;
    }
    
    for(int i = 1; i <= 6; i++)
    {
        cout << "Number of times " << i << " was rolled: " << check[i] << endl;
    }
    // Still need to add lines to print out all 50 trials
    return 0;
}
Last edited on
Since everybody has already posted answers to your homework assignment, I guess I will too. I'll try to make it as simple as possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
    int times, diceRolls[] = {0,0,0,0,0,0};
    
    for (int i = 0; i < 6; i++)
    {
        cout << "How many times was " << i + 1 << " rolled? " << endl;
        cin >> times;
        
        diceRolls[i] += times;
    }
    
    for (int i = 0; i < 6; i++)
        cout << "Number of times " << i + 1 << " was rolled: "
             << diceRolls[i] << endl;
}


That's about as simple as it gets. If you don't understand a part of it, ask me!

-VX
@VX0726's answer is by far the best if you don't care about validation
Haha, thanks. I had a lot of validation, but I realized if I had too much, @UgoChi might not understand.
@VX0726 Let’s imagine that you have been contacted by a local restaurant. It seems they are having some trouble with consistency and they need a program to help them determine exactly how long a chicken should cook. Your task is to design a program that would ask the user to input the weight of the chicken and then determine the appropriate cook time using a formula that will be given below. The oven temperature will remain constant so you don’t have to worry about that. For the purposes of this program, the cook time will be 20 minutes per pound plus an additional 15 minutes.

At the end of the program, you will display the “Cook Time Minutes” and the “Total Cook Time”.

This program design will use Functions for each of the key elements. Make sure to name each function appropriately.

THE PROGRAM SPECIFICS:

Function: The program should ask the user to input the weight of the bird rounded to the nearest pound.
The weight should be assigned to an appropriate named variable

For the purposes of this program, the cook time will be 20 minutes per pound plus an additional 15 minutes.

Function: The “Cook Time Minutes” should be calculated using the following mathematical formula
Cook Time Minutes = Weight * 20 + 15
The above formula calculates the total minutes that the chicken would be cooking.
Create a temporary variable called “intMinutes” and assign “Cook Time Minutes” to it.
If the “intMinutes” variable is more than 60, create a loop that will subtract 60 while adding 1 to the “intHours” variable. This loop should continue until the “intMinutes” is less than 60.

Function: Once calculated, display the cook time in total minutes (using “Cook Time Minutes”).
Also display the total cook time in hours and minutes (using “intHours” and “intMinutes”).
Yes I'm struggling in this course do to not being able to disect the basics and even present the code to form a program. Since Week 1 I've been struggling even with a basic assignment. I've watched videos from class discussions and Youtube videos as well
hey, I tried doing it but im stuck, heres my failure code...

edit :
deleted mah answer

edit2 :
here is the code in pascal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var weight, intHours : integer ;
     intMinutes : extended;
function cooktime ( w, m : integer ) : integer ;
begin
        cooktime := (w*m) + 15;
end;

begin 
        writeln('the weight is ');
        readln(weight);
        intMinutes := cooktime(weight, 20);
        while (intMinutes > 60) do
        begin
                intMinutes := intMinutes / 60;
                intHours := intHours + 1;
         end;
         if (intHours > 0) do
         begin
                 writeln('the time needed to cook is ', intHours, ' hours ', intMinutes, ' minutes');
         end
         else
         begin
                 writeln('the time needed to cook is ', intMinutes, ' minutes');
         end;
end.


Last edited on
@UgoChi Let me see what you have so far, and I'll try to help you from there.

-VX
All right, here's a function to get you started:

1
2
3
4
void calcCookTime (int weight)
{
    return (weight * 20 + 15);
}
Pages: 12