Question: Practice 1 Write a stern program that asks the user to enter a number within some range. If the user tries to joke around and

Practice 1
Write a stern program that asks the user to enter a number within some range. If the user tries to joke around and input something incorrect, ask again, and again, and again, until they learn to get it right. Use input validation in a loop until the user enters correct input.
Example Output
Please enter a number (1-10): -1
Please enter a number (1-10): 0
Please enter a number (1-10): 11
Please enter a number (1-10): ldkfjdlj
Please enter a number (1-10): true
Please enter a number (1-10): 3.8
Please enter a number (1-10): 7
You entered: 7(finally)
Practice 2
Revisit any program in a previous lab and update it to include input validation. We recommend completely re-writing the program from scratch, instead of simply copying it and inserting input validation. This speeds up the process of learning a new programming language.
Example Output
Quotes on Demand!
--------------------------
1 Mark Twain
2 Jiddu Krishnamurti
3 Graham Priest
--------------------------
Choose wisely: klsdfj
Choose wisely: 0
Choose wisely: 4
Choose wisely: Mark Twain
Choose wisely: 2.9
Choose wisely: 3
"Scientific advances are often triggered by taking oddities seriously."
- Graham Priest
Practice 3
Use a for loop to print the multiples of 10 between 1 and 100. Do this by looping 100 times and using a continue statement to skip loop iterations which are not multiples of 10.
Example Output
Multiples of 10:
102030405060708090100
Practice 4
Use a for loop to print the multiples of 10 between 1 and 100. Do this by looping only 10 times, perhaps by changing the update expression of the loop.
Example Output
Multiples of 10:
102030405060708090100
Practice 5
Rewrite the program for Gentle Jerry's Generic Goods to be even better! This time add input validation, a loop to allow the user to buy more goodies, and an option to quit buying.
Example Output
Welcome to Gentle Jerry's Generic Goods!
1) Sword -5 gold
2) Shield 8 gold
3) Potion -3 gold/ounce
4) Nothing, quit.
What would you like to buy? Sword
What would you like to buy? 1
How many? 3
What would you like to buy? Kdjfld
What would you like to buy? 0
What would you like to buy? 3
How much? 10
What would you like to buy? Your prices are getting damn expensive, Jerry!
What would you like to buy? I HATE YOU!!!!
What would you like to buy? 4
3 Swords 15 gold
10 Potion 30 gold
------------------------------
Subtotal 45 gold
5% sales tax 2.25 gold
------------------------------
Total 47 gold, 25 silver
Practice 6
Create a Guess the Number! game. Initialize an unsigned integer to store a random number in the range 1->100 and declare another unsigned integer to store the user's guess. Ask the user for their guess. Then write a loop to keep running until the user guesses the number. If the user guesses the correct number, stop the loop and display a win message. Within the loop tell them if they are too high or too low and ask for the next guess. Finally, if the user enters 0, display the random number and stop the loop (essentially allowing them to quit the game).
Example Output
Lets play Guess The Number (1-100)
Whats your guess? 75
Too high!
Whats your guess? 20
Too low!
Whats your guess? 45
Too high!
Whats your guess? 31
That's right!
Practice 7
Use a loop and an array to generate a list of 10 random numbers between the ranges of 1 and 100, inclusive. Then loop through the array again and print out all the numbers.
Example Output
Your 10 random numbers are:
32528849748868132744
Practice 8
String types actually contain an array of char types inside of them. You can access this array of chars by using the array subscript operator (the brackets []) on the string itself. Now that you know this, ask the user to enter a message and read it into a string variable. Then loop through the characters of the string backwards to print the message out in reverse.
Example Output
Please enter a message to reverse:
Hello, World!
Here is your message reversed:
!dlroW ,olleH
Practice 9
Define an array of strings, initializing it with some names of your choice. Ask the user to specify which name they would like to search for. Next, search your array for the name the user specified, by iterating through the array and comparing their search term with each element of the array. Then let the user know if the name was found in the array or not. Finally, print out the array of strings so the user can verify.
Example Output
Which player would you like to look for: Luigi
Yes, Luigi is in the list!
Look for yourself:
Mario, Luigi, Peach, Toad,
Practice 10
Define and initialize an array of 10 random sales figures (floating point numbers). Then iterate through the array of numbers and compute the following:
total sales
average sale
highest sale
lowest sale
Example Output
Sales: 2.62,4.13,3.68,4.44,0.52,8.28,1.86,5.64,8.86,6.59,
Total: 46.62
Average: 4.662
Highest: 8.86
Lowest: 0.52
Practice 11
Time to play the Lotto! Define two arrays of 5 unsigned short integers. One array is for the user's guesses and the other is for the lotto numbers. Using a for loop, randomize the five lotto numbers to be in the range: 1->100. Using another for loop, ask the user for their five lotto picks. Then display the random lotto numbers to the user. To calculate the user's winnings, use a for loop and check if each element in the user's array is equal to a corresponding element in the lotto array (user[i]== lotto[i]). Increase a winnings variable by 10 for each matching number.
Example Output
Enter your pick for lotto ball #1: 4
Enter your pick for lotto ball #2: 8
Enter your pick for lotto ball #3: 15
Enter your pick for lotto ball #4: 16
Enter your pick for lotto ball #5: 23
Lotto Numbers are: 2068652327
You win $0!
Practice 12
Lets modify the previous lab to improve our chances of winning. Instead of checking for a matching number in the corresponding position, we could check if a user's pick matches any of the lotto numbers. So to calculate the winnings, write a for loop to iterate through the user's five numbers, and within that loop, write a nested for loop to iterate through the five lotto numbers (NOTE: You must use different names for the loop counters. Both counters shouldnt be called i!) Increase the winnings variable by 10 for each matching number (or make the increase exponential if you want more money ).
Example Output
Enter your pick for lotto ball #1: 4
Enter your pick for lotto ball #2: 8
Enter your pick for lotto ball #3: 15
Enter your pick for lotto ball #4: 16
Enter your pick for lotto ball #5: 23
Lotto Numbers are: 769648844
You win $10!
Practice 13
Write an extreme version of the Hello World program. Write the following function, which prints Hello, World! to the screen once:
static void PrintGreeting()
Now, create an indefinite loop that simply calls your function over and over again each loop iteration. Note that you will have to hard kill this program since there is no stopping condition to your loop.
Example Output
Hello, World!
Hello, World!
Hello, World!
...
Practice 14
Write a power function which computes one value raised to the power of another and then returns the result (i.e. f(base, exp)= base exp ). The function header should look like this:
static int Power(int x, int exp)
Now, outside the function, perhaps in Main, ask the user for both a base and an exponent, with input validation, and call the function passing those inputs as arguments. Catch the return value of the function and display it on the screen.
Example Output
Base: ksdjf
Base: 10
Exponent: 3
10 raised to the 3 power is 1000
Practice 15
Write a function to convert degrees to radians. This function is useful because most System.Math library functions take radians as arguments, not degrees. Use the following conversion formula:
radians = degrees */180.0
Once written, ask the user for some degrees, call your function to convert, and then display the result.
Example Output
Enter degrees: kdjfd
Enter degrees: 180
Radians: 3.14159265358979
Practice 16
Create a function to calculate a factorial. First, use input validation to ask the user for an integer, ensuring the user does not enter a negative value, floating-point value, or non-numeric text. After the input validation, pass the integer to your function and within the function write a for loop to start the counter at the user's input, run as long as the counter is greater than or equal to 1, decreasing the counter by 1 each time. Within the loop, multiply the counter variable into a product variable that starts at 1(product *= i). Have the function return the factorial product. Main should display the final answer.
Example Output
What number would you like a factorial of? five
What number would you like a factorial of?-5
What number would you like a factorial of?5
Calculating 5!...
5 x 4 x 3 x 2 x 1
120
Practice 17
Write a program to display the multiples of a number up to another number (thus the second number should be greater than or equal to the first number). The function itself should start at the first number and loop until reaching the second number. The function header should look like this:
static void Multiples(int number, int max)
Outside the function, ask the user for two numbers, storing them into integer variables. Then call your function sending the user's numbers as inputs.
Example Output
Enter a number: 7
Enter a max: 100
The multiples of 7 up to 100 are: 714212835424956637077849198
Practice 18
Write a program to detect if a number is prime or not. Create a function that takes a number in as input, detects if it is prime, and then returns a boolean indicating if the number is prime or not. There are various ways to check if a number is prime, though the easiest way (to understand) is to simply loop through all values from 2(number /2), inclusive, and see if any of those values divide evenly into the number. If none of those values divide evenly into the number, then the number is prime.
Example Output
Enter a number to detect primality: 7
7 is prime!
Enter a number to detect primality: 38
38 is not prime!
Practice 19
Pic

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!