Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROBLEM #1: Making Coordinates
- Construct a program that uses two nested for loops to produce 10 lines of 10
- integers that shows the row and column position (think like the game Battleship).
- The first loop represents the ROWS and the nested loop represents each COLUMN in a row.
- The first number is the row and the second is the column position.
- Your output should look something like this:
- [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
- [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
- [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
- [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
- [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
- [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
- [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
- [7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
- [8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
- [9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]
- NOTE: You could start looping at 1 instead of 0. People don’t usually refer to column 1 as
- column zero!
- PROBLEM #2: Printing Asterisks
- Create a program using nested loops that creates the output shown.
- That is 20 columns of the ‘*’ character and 4 rows. You may prompt the user to enter values for the
- height and width to create other shapes.
- SAMPLE OUTPUT:
- Enter in the number of rows/height you want: 4
- Enter in the number of columns/width you want: 20
- ********************
- ********************
- ********************
- ********************
- ADDITIONAL PROBLEM #2.5: Correcting an Incorrect Program
- The program below is supposed to print this output:
- *
- * *
- * * *
- * * * *
- * * * * *
- However, the program has a LOGIC error preventing it from printing correctly.
- Find and fix the errors. THERE IS ONLY 1 ERROR.
- BROKEN CODE:
- #include<stdio.h>
- int main()
- {
- // outer for loop to represent rows
- for(int i = 1; i <= 5; i++)
- {
- // inner for loop to represent columns
- for(int j = i; j <= i;j++)
- {
- printf("* ");
- }
- // new line
- printf("\n");
- }
- return 0;
- }
- PROBLEM #3: A Weird * * Problem
- Create a program using nested loops that creates the output shown. Hint: Make use of
- if/else statements and nested loops based on conditions. You are printing spaces, newlines and two
- different characters, so you may want to write down an algorithm before coding this.
- You may prompt the user to enter values for the height and width to create other shapes.
- SAMPLE OUTPUT:
- Enter in the number of rows you want: 7
- Enter in the number of columns you want: 13
- =============
- * *
- * *
- * *
- * *
- * *
- =============
- PROBLEM #4 Using Break & Continue
- THIS PROBLEM HAS NO NESTED LOOPS
- Using break and continue, create a program with an loop that will count up from 0.
- YOUR LOOP MUST BE while(1)
- The numerical value of each loop iteration should be checked to see if it is an even number.
- If it is odd, print to screen that is odd, and if it is even increment
- a counter to keep track of the number of even numbers. When the loop reaches it’s 50th
- iteration, the loop should stop using the break statement. You may not use a test case in the
- loop test to determine when to stop. You must use a counter in the body of the loop to
- keep track of the number of loops. NOTE: This is just an exercise in the use of break and
- continue.
- SAMPLE OUTPUT:
- 1 is odd.
- 3 is odd.
- 5 is odd.
- 7 is odd.
- 9 is odd.
- 11 is odd.
- 13 is odd.
- 15 is odd.
- 17 is odd.
- 19 is odd.
- 21 is odd.
- 23 is odd.
- 25 is odd.
- 27 is odd.
- 29 is odd.
- 31 is odd.
- 33 is odd.
- 35 is odd.
- 37 is odd.
- 39 is odd.
- 41 is odd.
- 43 is odd.
- 45 is odd.
- 47 is odd.
- 49 is odd.
- The number of evens in 50 numbers is 25
Add Comment
Please, Sign In to add comment