Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem #1
- Write a program that simulates flipping a coin for 1000 times and report the
- number of heads and tails you get. Use a for loop to control the random number
- generation and two variables to store the heads vs tails counts. NOTE: use the
- random number generator.
- Challenge: How could you cheat to make the heads have a 2/3 probability?
- /*** SKELETON CODE ***/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- srand(time(NULL));
- /** LOCAL DECLARATIONS GO HERE ***/
- /** FOR LOOP GOES HERE ***/
- //Print number of heads (Outside loop)
- //Print number of tails (Outside loop)
- return 0;
- }//end main
- Problem #2
- https://www.mathsisfun.com/numbers/factorial.html
- Write a program that will determine the factorial of a user supplied number using a
- for loop.
- Note: Using a function will make the code cleaner, but is not necessary.
- /*** SKELETON CODE ***/
- /*
- * Compose a program using a for loop that will determine the
- * factorial of a user supplied number.
- * BY: Larry Snedden
- */
- #include <stdio.h>
- int main(void)
- {
- /** LOCAL DECLARATIONS GO HERE **/
- //Local statements
- printf("Enter in a number for factorial: ");
- //SCANF STATEMENT GOES HERE
- /** FOR LOOP GOES HERE **/
- /** OUTPUT FACTORIAL RESULT HERE (Outside of Loop) **/
- return 0;
- }//end main
- Problem #3
- Write a program that prints numbers in descending order. Using a while loop,
- prompt the user for the amount of numbers (integers) to print to screen up to 100.
- Print the numbers counting down from the number entered all the way down to the
- number 0, like a countdown. Every 10 numbers printed to screen, print a newline so
- that there is a maximum of 10 numbers per line. Your while loop should use a test
- condition to determine when to stop.
- /** SKELETON CODE **/
- /* Simple while loop that prints numbers 10 per line.
- Written by:
- Date:
- */
- #include <stdio.h>
- int main (void)
- {
- // Local Declarations
- int num;
- int lineCount = 0;
- // Statements
- printf ("Enter an integer between 1 and 100: ");
- scanf ("%d", &num); // Initialization
- // Test number value here to make sure it is correct (Use if-else)
- /** WHILE LOOP GOES HERE **/
- return 0;
- } // main
- Problem #4
- Write a program using a do while loop that reads positive integers from the user.
- The user should be prompted to enter positive integer values or a 0 to quit. Data is
- entered one integer at a time. Zero is used to signify the end of a user’s input. The
- program will print the minimum, maximum and average integer of user’s input.
- Also, recall that division on integer values may not work well if you have a fractional
- average. How might you use casting here? Remember that the zero is not a member
- of the list, don’t use it to determine the average. Test your program with the
- following data:
- 24, 7, 31, 64, 57, 7, 63, 31, 15, 7, 2, 4, and 6.
- Your min should be 2, max should be 64 and average should be 24.46 with this test
- data.
- /** SKELETON CODE **/
- /*
- * This program demonstrates how to use a do while loop with a
- * quit condition predicated on user input.
- * Also demonstrated is the casting operator and if selection.
- * By: L Snedden
- */
- #include <stdio.h>
- int main(void)
- {
- // Local declarations
- int min = 100000; //Min value (Don't mind the large number, keep it for now)
- int max = 0; // Max value
- int numIn = 0; // Input number
- int ttlCnt = 0; // Total number of inputs
- int ttlIn = 0; // Total sum of numbers
- double avg = 0.0; // Average
- /** DO WHILE LOOP GOES HERE **/
- avg = (double)ttlIn / (double)ttlCnt ;
- printf("Min: %d Max: %d Avg: %.2lf\n", min, max, avg);
- return 0;
- }//end main
Add Comment
Please, Sign In to add comment