Programming
program c " game "

Question Description

I don’t know how to handle this Programming question and need guidance.

Hello, I upload all what you need, I want you to do what it in the filed.


thank you

Unformatted Attachment Preview

LAB 4: Guessing Game Flow Chart START Predefined Process main(); Display Options User Selection input Selection If option = 1 Predefined Process User Instructions PLAY GAME Else option = other digit Predefined Process Good Bye! Exit Program exit(0) END GAME If A>B PLAYER1 input: A Data Storage: A PLAYER2 input: B Clear Screen Process and Instructions PLAYER 2 WON! Compare Else A = B PLAYER 1 WON! main(); Else if A >= != Relational/Equality Operators Description Sample Usage Less than (eg. x < 10). bool result = (4 < 7) Less than or equal to (eg. x <= 10). bool result = (41.1 <= 42) Equivalence (eg. x = = 10) (important) bool result = (11 == 8) Greater than (eg. x > 10). bool result = (3.2 > 3.1) Greater than or equal to (eg. x >= 10). bool result = (41.1 >= 42) Not equivalent (eg. x != 10). bool result = (12 != 12) Logical Operators Operators Sample Usage Logical AND (&&) bool result = (0 && 1) Logical OR (||) bool result = (0 || 1) Logical not (!) bool result = (0 != 1) Result true false false true false false Result false true true LOGICAL NOT Page 1 of 18 Lecture 3 LOGICAL AND Cloudy and working lights Cloudy and busted lights Cloud && Bulbs = Lights ON Cloud && Bulbs = Lights OFF 1 && 1 = 1 1 && 0 = 0 Sunny and busted lights Sunny and working lights Cloud && Bulbs = Lights OFF Cloud && Bulbs = Lights OFF 0 && 0 = 0 0 && 1 = 0 Page 2 of 18 Lecture 3 LOGICAL OR Night OR Cloudy Cloudy || Night = Lights ON 1 || 0 = 1 Night OR Cloudy Cloudy || Night = Lights ON 0 || Night OR Cloudy Cloudy || Nights = Lights ON 1 || 1 = 1 1 = 1 Daytime OR Sunny Cloudy || Night = Lights OFF 0 || 0 = 0 Page 3 of 18 Lecture 3 Let see an example: /* A Program that determines if the two entered numbers are equal or not */ So in C where can we use these Boolean Expressions and Comparison Operators? Page 4 of 18 Lecture 3 How about Branching Mechanisms? Let's say you are writing software for a bank ATM (automated teller machine). A customer makes a request for a certain amount of cash, and your responsibility is to determine if they should be allowed to withdraw that amount. You could decide to use the following algorithm: "if the amount requested is less than the account balance, that amount should be withdrawn; otherwise, the customer should be notified and no money should be withdrawn." Makes sense, right? So, the next step is coming up with some pseudo-code. Once you have pseudo-code, writing the C code will be easy. Amount < Total Balance Take Cash! Amount > Total Balance NO CASH! Pseudo-code for the ATM problem might look like this if the amount requested < account balance then withdraw the amount requested otherwise withdraw nothing and notify the customer Now that we have pseudo-code, writing the C code is as simple as "translating" your pseudo-code into C. In this case, it's easy: C Code for the ATM problem if (amountRequested < accountBalance) { withdraw(amountRequested); } else { withdraw(0); notifyCustomer(); } Page 5 of 18 Lecture 3 You'll notice some new syntax in this example, but don't worry about it too much. Pay close attention to the very first line, which checks to make sure that the amount requested is less than the account balance. The way it works is, if the expression between parentheses (amountRequested < accountBalance) evaluates to true, then the first block of code will be read. That is, the code inside the first set of curly braces { withdraw(amountRequested); } will be executed. If the expression in parentheses evaluates to false, on the other hand, then the second block of code, the code following the word else will be read. In this case, the first block of code withdraws the amount requested by the customer, while the second block of code withdraws nothing, and notifies the customer. That wasn't so hard! All we did was take the original English description of how we would solve the problem, write some pseudo-code for the English description, and translate the pseudo-code into C. FLOW CHART FOR ATM PROGRAM start main Amount Request Amount < Balance Withdraw Cash! Sorry. NO Cash. stop main Page 6 of 18 Lecture 3 Pseudocode Notation and Flowchart Symbols Page 7 of 18 Lecture 3 If and else conditional statements The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user entered password, your program can decide whether a user is allowed access to the program. Without a conditional statement such as the if statement, programs would run almost the exact same way every time. The if statements allow the flow of the program to be changed, and so they allow algorithms and more interesting code. Before discussing the actual structure of the if statement, let us examine the meaning of TRUE and FALSE in computer terminology. A true statement is one that evaluates to a nonzero number. A false statement evaluates to zero. When you perform comparison with the relational operators, the operator will return 1 if the comparison is true, or 0 if the comparison is false. For example, the check 0 == 2 evaluates to 0. The check 2 == 2 evaluates to a 1. If this confuses you, try to use a cout statement to output the result of those various comparisons (for example cout<< ( 2 == 1 );) When programming, the aim of the program will often require the checking of one value stored by a variable against another value to determine whether one is larger, smaller, or equal to the other. There are a number of operators that allow these checks and they are called relation operators as discussed earlier. Here are the relational operators, as they are known, along with examples: > greater than < less than >= greater than or equal <= less than or equal == equal to != not equal to 5 > 4 is TRUE 4 < 5 is TRUE 4 >= 4 is TRUE 3 <= 4 is TRUE 5 == 5 is TRUE 5 != 4 is TRUE Now that you understand TRUE and FALSE in computer terminology as well as the comparison operators, let us look at the actual structure of if statements. Page 8 of 18 Lecture 3 Structure of an if statement The structure of an if statement is as follows: if ( TRUE ) //Execute the next statement To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of a function. Anything inside braces is called a compound statement, or a block. For example if ( TRUE ) { //Execute all statements inside the braces } There is also the else statement. The code after it (whether a single line or code between brackets) is executed if the if statement is FALSE. It can look like this: if ( TRUE ) { // Execute these statements if TRUE } else { // Execute these statements if FALSE } One use for else is if there are two conditional statements that may both evaluate to true, yet you wish only one of the two to have the code block following it to be executed. You can use an else if after the if statement; that way, if the first statement is true, the else if will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements. Page 9 of 18 Lecture 3 Let's look at a simple program for you to try out on your own. /*------------------------------------------------------If and else conditional statements program ---------------------------------------------------------*/ #include #include int main(void) { int age=0; // Most important part of the program! //Declare and initialize integer variable printf( "Please input your age: "); // Asks for age scanf("%i", &age); // Input is: put in age using keyboard if ( age < 100 ) { printf( "You are pretty young!\n"); } // If the age is less than 100 // Just to show you how it works... else if ( age == 100 ) { printf ("You are old\n"); } // I use else just to show an example // Just to show you how it works... else { printf( "You are really, really old\n"); } // Executed if no other condition applicable system("PAUSE"); return 0; } Nesting Statements There is one control statement that is inside of another control statement. This is known as nesting. The above is a nesting statements example. Page 10 of 18 Lecture 3 The switch statement A switch statement is used in place of many if statements. Let's consider the following case: You are writing a program that calculates the Highway toll based on the vehicle class. There are 5 different types of vehicle classes as follows: Vehicle Type or Class Passenger Car Motor Cycle Commercial Vehicle Small Truck 18 Wheeler Toll ($) 5.50 2.00 8.00 10.00 15.00 One way you can write this program is as follows: (assuming also that you have assigned numbers to each vehicle class starting with Passenger Car and ending with 18 Wheeler.) // declare a variable to keep track of the interest float Toll = 0.0; int Vehicle_Class = 0; // decide which interest rate to use. if (Vehicle_Class == 1){ Toll = 5.50; } else { if (Vehicle_Class == 2) { Toll = 2.00; } else { if (Vehicle_Class == 3){ Toll = 8.00; } else { if (Vehicle_Class == 4){ Toll = 10.00; } else { // Total Vehicle Classes are 5 Toll = 15.00; } } } } // Passenger Car // Motor Cycle // Commercial Vehicle // Small Truck // 18 wheeler Page 11 of 18 Lecture 3 # 1 2 3 4 5 Vehicle Type or Class Passenger Car Motor Cycle Commercial Vehicle Small Truck 18 Wheeler Toll ($) 5.50 2.00 8.00 10.00 15.00 The above code that uses if-else Statements is hard to read and hard to understand. There is an easier way to write this, using the switch statement. The preceding chunk of code could be written as follows: switch (Vehicle_Class){ case 1: Toll = 5.50; break; case 2: Toll = 2.00; break; case 3: Toll = 8.00; break; case 4: Toll = 10.00; break; default: Toll = 15.00; } // Passenger Car // Motor Cycle // Commercial Vehicle // Small Truck // 18 wheeler Let us examine the code on the next page: Page 12 of 18 Lecture 3 Page 13 of 18 Lecture 3 Structure of Switch Statement The switch statement allows a programmer to compound a group of if statements provided that the condition being tested is an integer or a character. The switch statement has the form: switch(integer_val){ case 1: // code to execute if integer_val is 1 break; ... case n: // code to execute if integer_val is the nth integer value break; default: // code to execute if integer_val is none of the above } The default clause is optional, but it is good programming practice to use it. The default clause is executed if none of the other clauses have been executed. For example, if my code looked like: switch(character_val){ case a: // code to execute if character_val is val_1 break; ... case z: // code to execute if character_val is the z character value break; default: // code to execute if character_val is none of the above } The break keyword means "jump out of the switch statement, and do not execute any more code." To show how this works, let us re-examine the Highway Toll program by removing the break statements. Page 14 of 18 Lecture 3 This feature of switch statements can sometimes be used to a programmers' advantage. In the example with the different types of bank accounts, say that the interest earned was a follows: # 1 2 3 4 5 Vehicle Type or Class Passenger Car Motor Cycle Commercial Vehicle Small Truck 18 Wheeler Toll ($) 8.00 2.00 8.00 10.00 15.00 Now, the code for this could be written as: switch (Vehicle_Class){ case 1: case 3: Toll = 8.00; // Commercial Vehicle and Passenger Cars Toll is same break; case 2: Toll = 2.00; break; break; case 4: Toll = 10.00; break; default: Toll = 15.00; // Motor Cycle // Small Truck // 18 wheeler } Page 15 of 18 Lecture 3 Switch Statement Flow Chart Page 16 of 18 Lecture 3 Operator precedence in C Operator precedence in C is incredibly easy! Don't let anyone tell you otherwise! Here's the trick: if you don't know the order of precedence, or you're not sure, add parentheses! Don't even bother looking it up. We can guarantee that it will be faster for you to add parentheses than to look it up in these notes or in a C book. Adding parentheses has another obvious benefit - it makes your code much easier to read. Chances are, if you are uncertain about the order of precedence, anyone reading your code will have the same uncertainty. That having been said, here's the order of operator precedence. In general, the order is what you would think it is - that is, you can safely say int x = 4 + 3; and it will correctly add 4 and 3 before assigning to x. Our advice is to read this table once and then never refer to it again. system("cls") and exit(0) functions for MS Windows • OS command line, "cls" is a command on some operating systems that clears the screen C syntax: system("cls" ); • exit(0) is a function call used to terminates the process and exit out of the program C syntax: exit(0); Page 17 of 18 Lecture 3 How to use system("cls") and exit(0) functions in MS Windows? Page 18 of 18 ...
Purchase answer to see full attachment
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

Final Answer

Here is...

criss53 (3153)
New York University

Anonymous
Top quality work from this tutor! I’ll be back!

Anonymous
Heard about Studypool for a while and finally tried it. Glad I did caus this was really helpful.

Anonymous
Thank you! Reasonably priced given the quality

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4
Similar Questions
Related Tags