A2 Conditions - sheerazwalid/COMP-I GitHub Wiki
The source code files for this assignment should be placed in a folder named conditions in your Cloud9 workspace. The files should be named as ex1.cpp for exercise 1, ex2.cpp for exercise 2, and so on.
Write a console program that prompts the user for 2 double precision floating point values x and y. Have the program compute and display the sine of the average of x and y.
The sine function is declared in the cmath header so you'll need to include it as follows.
#include <cmath>
Write a C++ program that prompts the user to enter a number between 3 and 12, inclusive. If the user enters a number inside [3, 12], the program displays "good number", otherwise the program displays "bad number".
Hint: use an if statement.
Write a C++ program that prompts the user for an integer. The program reads the integer and displays "even number" if the number is even or "odd number" if the number is odd.
Hint: use the modulo operator (%)
Write a C++ program that prompts the user for a temperature value. The program should print "cool" when the temperature is greater than 40 and less than or equal to 60, should print "warm" when the temperature is greater than 60 and less than or equal to 85 and should print "don't go outside" for all other temperature values.
Hint: use an if-else statement
Write a C++ program that prompts the user for a temperature. If the user enters a number less than 50 or greater than 90 then print "Let's not go out today."
Write a program that prints a random integer.
The C++ standard library contains a pseudo random number generator that you can use to solve this problem. The function rand returns a pseudo random number. To use rand in your programs, you need to include the cstdlib header as follows.
#include <cstdlib>
The function rand will return the same sequence of values for a given seed. You can change the sequence of values it returns by changing the seed as follows.
srand(719); // Seed the random number generator with 719.
The following is an example of how you can use the return value of rand.
cout << rand();
You can vary the seed passed into srand by calling the time function. Calling the time function with 0 as an argument produces the number of seconds from a given epoch (January 1, 1970 by convention). The following code shows how to seed rand with this time.
srand(time(0));
You'll need to call srand with the time function in order to get a different random number each time you run the program.
Submit your work by sending the url of your Cloud9 workspace to the teaching assistant. The subject line of your email should be 201 Assignment: Conditions.
-
Any of the above problems.
-
Write code that prompts the user to enter a number between 3 and 12, inclusive. If the user enters a number inside [3, 12], the program displays "good number", otherwise the program displays "bad number".
-
Suppose that t is an integer variable. Write code that prints "cool" when t is greater than 40 and less than or equal to 60, and prints "warm" when t is greater than 60 and less than or equal to 85. For all other values for t, print "don't go outside".
-
When the following code runs, what does it print?
int n = -1;
int k = n++ * -3;
cout << ++n * 4 + k;
- When the following code runs, what does it print?
cout << 1 / 3 + 1 / 3.0;
- Write a C++ program that prints a random number between 75 and 125.
Hint: compute 75 plus random number in [0, 50]
- Write a C++ program that prompts the user for a temperature. If the user enters a number less than 50 or greater than 90 then print "Let's not go out today."