1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Elden [556K]
3 years ago
11

Write a program that lets the user guess the coin's head or tail. The program randomly generates an integer 0 or 1, which repres

ents the head or the tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect. include step by step commentary.
Computers and Technology
1 answer:
svlad2 [7]3 years ago
7 0

Answer:

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

int main()

{

   srand (time(NULL));  //initialize random seed

   int guess;

    int number = rand() % 2;  //generate random number 0 or 1

    cout<<"Enter the guess (0 or 1): ";

    cin>>guess;     //store in guess

    if(number == guess){     //check if number equal to guess

       cout<<"Guess is correct."<<endl;   //if true print correct message.

    }else{

        cout<<"Guess is incorrect."<<endl;  //otherwise print correct message.

    }

    return 0;

}

Explanation:

first include the three library iostream for input/output, stdlib,h for using the rand() function and time for using the time() function.

create the main function and initialize the random number generation seed, then generate the random number using the function rand() from 0 to 1.

after that, take input from user and then with the help of conditional statement if else check if random number is equal to guess number or not and print the appropriate message.

You might be interested in
Write a program that reads in your question #2 Python source code file and counts the occurrence of each keyword in the file. Yo
scoray [572]

Answer:

Here is the Python program:

import keyword  #module that contains list of keywords of python

filename = input("Enter Python source code filename: ") # prompts user to enter the filename of a source code

code = open(filename, "r") # opens the file in read mode

keywords = keyword.kwlist #extract list of all keywords of Python and stored it into keywords

dictionary = dict() #creates a dictionary to store each keyword and its number of occurrence in source code

for statement in code: # iterates through each line of the source code in the file

   statement = statement.strip() # removes the spaces in the statement of source code  

   words = statement.split(" ") #break each statement of the source code into a list of words by empty space separator

   for word in words:# iterates through each word/item of the words list  

       if word in keywords:#checks if word in the code is present in the keywords list of Python  

           if word in dictionary: #checks if word is already present in the dictionary

               dictionary[word] = dictionary[word] + 1 #if word is present in dictionary add one to the count of the existing word

           else: #if word is not already present in the dictionary  

               dictionary[word] = 1 #add the word to the dictionary and set the count of word to 1

for key in list(dictionary.keys()): #iterates through each word in the list of all keys in dictionary  

   print(key, ":", dictionary[key])# prints keyword: occurrences in key:value format of dict

Explanation:

The program is well explained in the comments attached with each line of the program.  

The program prompts the user to enter the name of the file that contains the Python source code. Then the file is opened in read mode using open() method.

Then the keyword.kwlist statement contains the list of all keywords of Python. These are stored in keywords.

Then a dictionary is created which is used to store the words from the source code that are the keywords along with their number of occurrences in the file.

Then source code is split into the lines (statements) and the first for loop iterates through each line and removes the spaces in the statement of source code .

Then the lines are split into a list of words using split() method. The second for loop is used to iterate through each word in the list of words of the source code. Now each word is matched with the list of keywords of Python that is stored in keywords. If a word in the source code of the file is present in the keywords then that word is added to the dictionary and the count of that word is set to 1. If the word is already present in the dictionary. For example if there are 3 "import" keywords in the source code and if 1 of the import keywords is already in the dictionary. So when the second import keyword is found, then the count of that keyword is increased by 1 so that becomes 2.

Then the last loop is used to print each word of the Python that is a keyword along with its number of occurrences in the file.

The program and its output is attached in a screenshot. I have used this program as source code file.

7 0
3 years ago
Write a recursive function to determine if a number is prime.
Darya [45]

Answer:

Follows are the code to find the prime number:

import java.util.*;//import package for user input  

public class Main//defining a class

{  

public static boolean Prime(int x, int j)//defining a method isPrime    

   {  

       if (x<= 2) //use if block to check x less than equal to 2

           return (x== 2) ? true : false; //use bitwise operator to return value  

       if (x%j == 0) //use if to check x%j equal to 0

           return false;//return false value  

       if (j*j > x)//defining if that check i square value greater than n  

           return true; //return true value

       return Prime(x, j+1); //callling recursive method

   }  

   public static void main(String[] as)//main method  

   {  

       int n; //defining integer variable

       Scanner oxc=new Scanner(System.in);//creating Scanner class object

       n=oxc.nextInt();//input value

       if (Prime(n, 2))  //use if block to call isPrime method

           System.out.println("Yes"); //print value  

       else //else block

           System.out.println("No");  //print value  

   }  

}  

Output:

5

Yes

Explanation:

In this code, a static boolean method "Prime" is declared, that accepts two integer variables in its parameter and defines the if block that checks the prime number condition by the recursive method and returns a value true or false. Inside, the main method an integer variable is declared that uses the Scanner class object for input value and passes into the method and prints its value.

6 0
3 years ago
A user of the system
iragen [17]

Answer:

ummm what system. have to be more specific ^^

Explanation:

8 0
3 years ago
_____ of a global information system (GIS) concentrates on medium-range activities that move an organization toward achieving lo
Dafna11 [192]

Answer: (A) Tactical support

Explanation:

 GIS is the global information system in which the data is delivered worldwide in the form of information system. The global information system achieve long term goals in an organization.

  • The tactical support is used in the global information system for concentrating the activities on the medium range in an organization.  
  • The tactical support is one of the effective way to producing the desirable result and also achieve the main strategic objective.

Therefore, Option (A) is correct.

8 0
3 years ago
What is a way that i can connect my Xbox360 to my computer?
Vladimir79 [104]
Its possible if you get the right cords like double ended usb cord and also it depends on what kind of model you have message me and ill tell you
8 0
3 years ago
Read 2 more answers
Other questions:
  • You discover memory is corrupted, what would be an indication of a software vs. a hardware issue?
    5·1 answer
  • TO COMPUTING IN BUSINE
    14·1 answer
  • _______ tags are tiny chips that can be embedded into almost anything and contain information used to track and locate lost pets
    14·1 answer
  • Alice just wrote a new app using Python. She tested her code and noticed some of her lines of code are out of order. Which princ
    8·1 answer
  • What general (system) recommendations might you make to the shop manager? (For example. if the system will be integrated, what m
    13·1 answer
  • 5. When resizing an object with the Select Tool, what keyboard key should you press to make it resize equally in all directions?
    11·1 answer
  • Methods are used to define an object's behavior. True False
    8·1 answer
  • What are building blocks? Select three options.
    14·1 answer
  • A form of segmentation that is based on user usage rate, user status, purchase occasion, and benefits sought is _________.
    13·1 answer
  • Which benefit does the Cloud provide to start-up companies without access to large funding?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!