Page 1 of 2 12 LastLast
Results 1 to 15 of 17
Like Tree2Thanks

Math Help - C++ programming.

  1. #1
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    C++ programming.

    Write a program that simulates tossing of a die.
    Write a program that lets the user enter the results of 20 tosses. The output should be a summary of how many ones; twos and so on have appeared.


    this is the question but i dint even know how to start and what does the tossing of die mean?
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    Hi again Riana!

    Tossing a die means throwing a 6-sided die and seeing which number comes up.

    In C++ you can simulate it with:
    Code:
    int toss = rand() % 6 + 1;
    Your job is to count the number of times the toss is 1.
    Or 2, and so on.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    ok it says rand was not declared?....and if i write this after this am i suppose to ask the user to enter any number...and if yes so dp i use switch?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    #include <iostream>
    using namespace std;
    int main () {
    int counter =0;
    int x;
    int total1,total2,total3,total4,total5,total6=0;
    cout<<"Please input your results: ";
    cin>>x;

    while(counter<20)
    {
    switch(x)
    {
    case 1:
    total1++;
    break;
    case 2:
    total2++;
    break;
    case 3:
    total3++;
    break;
    case 4:
    total4++;
    break;
    case 5:
    total5++;
    break;
    case 6:
    total6++;
    break;
    }

    counter++;

    }
    return 0;
    }




    this is my code so far ....this was wat i was able to sort out.
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    You can get rand() by including #include <cstdlib>.
    But perhaps you're not supposed to use rand(), but simply ask the user for 20 numbers (between 1 and 6).

    And yes, if you have the toss results, you can use a switch to count each toss result.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    If believe you should put the "cin >> x" inside the while-loop.

    And I guess you should print the totals at the end.
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    #include <iostream>
    using namespace std;
    int main () {
    int counter =0;
    int x;
    int total1,total2,total3,total4,total5,total6=0;
    int total=0;


    while(counter<6)
    {
    cout<<"Please input your results: ";
    cin>>x;
    switch(x)
    {
    case 1:
    total1++;
    break;
    case 2:
    total2++;
    break;
    case 3:
    total3++;
    break;
    case 4:
    total4++;
    break;
    case 5:
    total5++;
    break;
    case 6:
    total6++;
    break;
    }

    counter++;

    }total=total+total1+total2+total3+total4+total5+to tal6;

    cout<<"the total is "<<total<<endl;
    return 0;
    }




    i did this....but the total have a negative sign in front of it.why?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    When you declared
    Code:
    int total1,total2,total3,total4,total5,total6=0;
    it was only the last one that was initialized to zero.
    They should all be initialized to zero, otherwise they will contain an unpredictable value (possibly negative).

    Btw, you output should consist of each total separately.
    The output should look something like:
    Code:
    Summary after 20 tosses.
    Number of ones: 3
    Number of twos: 4
    ...
    Number of sixes: 2
    Follow Math Help Forum on Facebook and Google+

  9. #9
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    how can i find these ones and twos?
    Follow Math Help Forum on Facebook and Google+

  10. #10
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    You already have them.
    The number of ones is total1.
    Thanks from Riana
    Follow Math Help Forum on Facebook and Google+

  11. #11
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    cout<<"Summary after 6 tosses "<<total<<endl;
    cout<<"The number of one's "<<total1<<endl;
    cout<<"The number of two's "<<total2<<endl;
    cout<<"The number of three's "<<total3<<endl;
    cout<<"The number of four's "<<total4<<endl;
    cout<<"The number of five's "<<total5<<endl;
    cout<<"The number of six's "<<total6<<endl;



    like this?
    Follow Math Help Forum on Facebook and Google+

  12. #12
    Newbie
    Joined
    Feb 2013
    From
    Australia
    Posts
    2

    Re: C++ programming.

    Are you all doing you java programs, do you get the idea of how to write a java progra,. cause when i was in my old school class during the computer exams i used to by heart my java programs.and also in my class no one had the idea of writting a java program cause it is so tough and i just hate when i dont know anything.whoever knows the java program they might be really lucky and a good student.
    Follow Math Help Forum on Facebook and Google+

  13. #13
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    Quote Originally Posted by Riana View Post
    cout<<"Summary after 6 tosses "<<total<<endl;
    cout<<"The number of one's "<<total1<<endl;
    cout<<"The number of two's "<<total2<<endl;
    cout<<"The number of three's "<<total3<<endl;
    cout<<"The number of four's "<<total4<<endl;
    cout<<"The number of five's "<<total5<<endl;
    cout<<"The number of six's "<<total6<<endl;



    like this?
    Yep!

    Although I'd replace the first line by
    Code:
    cout << "Summary after " << total << " tosses" << endl;
    Thanks from Riana
    Follow Math Help Forum on Facebook and Google+

  14. #14
    Junior Member
    Joined
    Feb 2013
    From
    Canada
    Posts
    31

    Re: C++ programming.

    opss my mistake ....thanks
    btw whats your name?
    Follow Math Help Forum on Facebook and Google+

  15. #15
    Super Member ILikeSerena's Avatar
    Joined
    Dec 2011
    Posts
    733
    Thanks
    121

    Re: C++ programming.

    Quote Originally Posted by Riana View Post
    opss my mistake ....thanks
    btw whats your name?
    Ah, I like to keep that a secret on such a public forum.
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. Replies: 9
    Last Post: March 3rd 2013, 04:15 PM
  2. C++ programming. i need help !
    Posted in the New Users Forum
    Replies: 3
    Last Post: February 22nd 2013, 07:32 PM
  3. Programming For HP 48G+
    Posted in the Calculators Forum
    Replies: 0
    Last Post: February 3rd 2009, 04:09 AM
  4. Replies: 1
    Last Post: November 17th 2008, 04:18 AM
  5. Please help on R programming
    Posted in the Math Software Forum
    Replies: 0
    Last Post: August 16th 2008, 03:51 AM

Search Tags


/mathhelpforum @mathhelpforum