C++代写:CS117 Palindrome

代写五个基础C++练习题,从Palindrome到Tic-Tac-Toe.

Exercise #1

A palindrome is any word or phrase which reads the same backwards as it does forward. For instance the word Bob is a palindrome. Napoleon is also attributed as saying the famous palindrome Able was I ere I saw Elba. As a counter example the phrase Computer Science is fun is NOT a palindrome.

For this exercise you will implement a program which asks the user for a string. The program will then output whether or not the phrase is a palindrome.

Hint: use an array of characters to determine if the string is a palindrome.

Sample output:

Enter a phrase: Rise to vote sir

"Rise to vote sir" IS A PALINDROME

Save your solution in a file named palindrome.cpp.

Exercise #2

In statistics, the standard deviation measures the amount of variation or dispersion from the average. It can be calculated using the following formula:

σ =√∑(x−μ)2 / n

Where σ is the standard deviation, x is each value in the data set, μ is the mean of all values in the data set, and n is the number of values in the data set. For your program you will ask the user for double values. Your program should stop when the user enters -99 when at this point your program will display the mean and the standard deviation from the data set.

Sample output:

Enter a value (or -99 to quit): 3
Enter a value (or -99 to quit): 5
Enter a value (or -99 to quit): 11
Enter a value (or -99 to quit): 14
Enter a value (or -99 to quit): 11
Enter a value (or -99 to quit): -99

The average is 8.8 with a standard deviation of 4.11.

Save your solution in a file named stddev.cpp.

Exercise #3

Consider the classic number guessing game we played in class, where one person thinks of a number and the second player attempts to guess that number given a number of attempts. For this exercise, you will write a program where the user will think of a number between 1 and 100, and the computer will attempt to guess it using the binary search algorithm.

Sample output:

Think of a number between 1 and 100.    Ready?    (Y/N)    Y
Is the number 50? (1=Higher, 2=Lower, 3=Exact) 2
Is the number 25? (1=Higher, 2=Lower, 3=Exact) 1
Is the number 37? (1=Higher, 2=Lower, 3=Exact) 3
Thanks for playing.

Save your solution in a file named guess.cpp.

Exercise #4

For this exercise you will read in student names and test scores into two arrays: an array of strings for the names and an array of integers for the scores. You will write a program which sorts the scores and displays the scores from highest to lowest and the student’s name who received the grade. Please use one of the sorting algorithms discussed in class. Do not use std::sort.

Implement the sort using either the Quick Sort algorithm, Merge Sort algorithm or with any advanced sorting algorithm with an average case runtime of Θ(nlog2n).

Sample output:

How many students took the exam?    3
Student #1 Name: Justin Verlander
Student #1 Score: 84
Student #2 Name: Jon Lester
Student #2 Score: 96
Student #3 Name: Clayton Kershaw
Student #3 Score: 51
RESULTS:
Jon Lester – 96
Justin Verlander – 84
Clayton Kershaw - 51

Save your solution in a file named reversal.cpp.

Exercise #5

The game TTT is a two player game where each player trades moves across a 3x3 game grid. One player designates their move with an X whereas the other player designates their move with an O. When one player has three of their marks placed in a line, that player has won the game. If all nine squares in the grid are filled and neither player has three symbols matching in a row, that game is considered tied.

Consider the following game scenario between a red and a blue opponent.

In the above figure, the blue player won the game since the bottom horizontal row has three of the same symbol in each square. Please note that there are eight matching rows where victory can be determined (three horizontal, three vertical and two diagonal.)

For this exercise you will implement the same game in C++. You may use either arrays or vectors to hold moves as the game board.

Points will be awarded for this exercise as follows:

  • generate a game board with three randomly placed symbols
  • output the game board with the placement of each symbol
  • A playable game of TTT, where the player trades moves with the computer opponent (players moves can be entered by asking the user for the row and column to be placed.)
  • All of the above requirements except the game should be implemented so that the player will never be able to defeat the computer opponent in a game of TTT (please note that the game can be completed to a tie.)

Save your solution in a file named ttt.cpp.