Question

C++ Programz

Program 0 (50%): In many computer science courses, when you study sorting, you also study “runtime” - and it can be confusing to understand when you first see it.  For example, they say that BubbleSort “runs in O(n2)”  time (pronounced “Big-Oh of n-squared") - but what does that mean?  For simplicity, it means if you have n elements, the worst it could run would be n2 low-level computer operations (like comparisons, assignment statements and so on).  For example, if we had an array of 10 elements, then n == 10 and the worst-case scenario would be 100 operations (and we’re skipping some of the math behind “Big-Oh”).  Note: runtime doesn’t mean clock time!
 
Let’s look at BubbleSort for an array of 10.  In the first pass through the array, the most we could have would be ~9 comparisons, at which point the largest element is now at the end of the array.  The second pass would be ~8 comparisons (since we don’t have to look at the largest element – it's already in the correct spot).  The third pass would be ~7 operations and so on.  So the total operations would be 9+8+...+3+2+1 == 45.  But wait! 45 doesn’t equal 100, so what gives?  Remember, the 100 is a worst-case scenario, not the actual.  Mathematically, we can sum up the numbers between 1 to n using n*(n-1)/2 (plug in 10 for n and you get 45).  Interestingly, that equation still gives us n2 if we remove any constants.
 
For this assignment, you’re going to write a visualization that helps you understand the concept above (because employers like asking sorting questions).  Declare an array of 5 random numbers between 0-99 and run BubbleSort on it.  After each pass of BubbleSort (the inner loop), you should print the array as well as the total number of comparisons and the total number of swaps. You should initialize the array to random values.  Your output should be formatted like the output below.  However, because your random numbers are different, your results will vary.  Hint: start with a function that prints an array.  It will help with debugging. 
 
Sample run 1:  |79|12|72|87|17| ============ |12|79|72|87|17| Comps:1 Swaps:1   |12|72|79|87|17| Comps:2 Swaps:2   |12|72|79|87|17| Comps:3 Swaps:2   |12|72|79|17|87| Comps:4 Swaps:3   |12|72|79|17|87| Comps:5 Swaps:3   |12|72|79|17|87| Comps:6 Swaps:3   |12|72|17|79|87| Comps:7 Swaps:4   |12|72|17|79|87| Comps:8 Swaps:4
Page 2 of 4
  |12|17|72|79|87| Comps:9 Swaps:5   |12|17|72|79|87| Comps:10 Swaps:5


Program 1 (50%): For this program, you’re going to extend the “Find the Wabbit” game to 2 dimensions to create the game Battleship.  For simplicity, imagine you have an 8x8 board of holes and two “ships” that have pegs on the bottom that go into those holes.  When you play, you place your ships on your board and your opponent does as well.  However, you can’t see each other’s boards.  So, you call out coordinates – guessing where your opponent’s ships are and vice versa.  If you guess a correct coordinate, your opponent says “Hit”.  Otherwise, they say “Miss”.  For this program, you’re going to play against the computer, even though you know where two ships are ☺   I hard-coded this example to the following, where S means “Ship”:
 
| | | | | | | | | | |S| | | | | | | | |S| | | | | | | | |S| | | | | | | | |S| | | | | | | | |S| | | | | | | | | | | |S|S|S| | | | | | | | | | |
 
Write the program that prints out the board, but does not show where the two ships are (similar to Find the Wabbit).  For each round, print the board and ask the player for an X and a Y coordinate.  If there’s a hit, the ‘S’ should be replaced by a ‘!’.  If it’s a miss, the ‘ ‘ should be replaced by ‘X’.  The game is over when the number of hits == 8 (two ships of size 5 and 3).  You must use at least two meaningful functions that take in a 2D array. Note, when printing, you may need to print [y][x] instead of [x][y].
 
Sample run:
 
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Enter x: 0 Enter y: 4 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Enter x: 1 Enter y: 1 | | | | | | | | | | |!| | | | | | | | | | | | | | | | | | | | | | | | | |X| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
 
Page 4 of 4
[CUT]
 
Enter x: 4 Enter y: 6 | | | | | | | | | | |!| | | | |X| | | |!|X| | | | | | | |!| | | | | | | |X|!| |X| | | | | | |!| | | | |X| | | |X| | |!|!|!|X| | | | | | | | | | 64m3 0v3r

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 7 more requests to produce the answer.

3 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
C++ Programz
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • C++ This program will compare sorting algorithms by examining how the number of swaps/exchanges differs between...

    C++ This program will compare sorting algorithms by examining how the number of swaps/exchanges differs between the two algorithms, and demonstrate the exchanges visually. This program will repeat until the user elects to quit. Ask the user how large of an array they'd like to create and fill with random numbers. The range of array elements they can select is between 5 and 20 inclusive. Use input validation on this 5-20 range. The range of random numbers is 0-99 inclusive....

  • In C++ program use the new style od C++ not the old one. Simple Battleship You...

    In C++ program use the new style od C++ not the old one. Simple Battleship You will make a game similar to the classic board game Battleship. You will set up a 5 x 5, 2 dimensional array. In that array, you will use a random number generator to select 5 elements that will act as the placeholders for your "battleships". Your user will get 10 guesses to "seek and destroy" the battleships. After their 10 guesses, you will tell...

  • Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to...

    Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....

  • Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

    Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won. The board will be read in as 3x3 array of characters (x, o or . for a blank spot). You are required to write the following functions to help solve the problem: // input prompts the user for a board and inputs it // into the given array void input (char board [3][3]); // print prints...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a...

    C++ ONLY THANKS! USE VISUAL STUDIO COMPILER ONLY! Part 1: Exercise 1: Problem description Write a three function program (your main function and two other functions). The main function of the program should create an array that can store 10 integers. The main function will then pass the array to a second function that gets the integers from the user and stores them in the array. Then your main will call a third function that displays the elements in the...

  • In C++ 1. Write a program that allows a user to enter 10 integer values from...

    In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT