Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.

stuck in a c++ problem?

The question is:

Write a program that simulates coin tossing. For each toss of the coin, the program should print Heads or Tails. Let the program toss the coin 100 times and count the number of times each side of the coin appears. The program should call a function flip that takes no arguments and returns 0 for tails and 1 for heads.

Here is my work:

#include<iostream>

using std::cout;

using std::endl;

using std::cin;

#include<cstdlib>

int main()

{

int flip;

srand(time(0));

for (int i=1, i<=100, i++)

flip=rand() %2 + 1;

if (flip==1)

cout<<"Heads"<<endl;

else

cout<<"Tails"<<endl;

system("pause");

return 0;

}

2 Antworten

Relevanz
  • vor 9 Jahren

    Make a function called flip()

    int flip()

    {

    return rand() % 2; // return either 0 or 1

    }

    then in your main function

    for (int i = 0; i < 100; i++)

    {

    int x = flip();

    if (flip == 0)

    cout << i << ": Heads" << endl; // for instance, "3: Heads"

    else

    cout << i << ": " << ": Tails" << endl; // for instance, "12: Tails"

    }

    Quelle(n): I rock out with my C++ out
Haben Sie noch Fragen? Jetzt beantworten lassen.