实验 11.28. lab6_ex1 description of the problem write a program that simulates the rolling of two...

24
实实 11.28

Upload: bathsheba-bryant

Post on 16-Dec-2015

245 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

实验 11.28

Page 2: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Lab6_Ex1

• Description of the Problem• Write a program that simulates the rolling of two dice. • The program should call rand to roll the first die, and sho

uld call rand again to roll the second die. The sum of the two values should then be calculated.

• Roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each sum appears. Print the results in a tabular format.

Page 3: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 4: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 5: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Modify the program to use a two-dimensional array similar to the diagram in Figure L 7.1. Now, rather than counting the number of times each sum appears, increment the correct cell in the array. Print this array with the number of times each dice combination occurred. A sample output may look like the following:

Page 6: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 7: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Lab6_Dubugging• The following program in this section does not run properly. Fix

all the compilation errors so that the program will compile successfully. Once the program compiles, compare the output with the sample output, and eliminate any logic errors that may exist. The sample output demonstrates what the program’s output should be once the program’s code has been corrected.

Page 8: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 9: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 10: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 11: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 12: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Lab6_hom1• 1. Use a one-dimensional array to solve the following problem. A company p

ays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):

• a) $200–299• b) $300–399• c) $400–499• d) $500–599• e) $600–699• f) $700–799• g) $800–899• h) $900–999• i) $1000 and over

Page 13: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 14: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 15: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 16: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

• 2. Use a one-dimensional array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

Page 17: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 18: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,
Page 19: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Lab7_hom1(7.32)• 递归 /回文

• 例如: rahar

Page 20: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

bool testPalindrome( const char [], int, int );int main(){ const int SIZE = 80; // maximum size for string char c; // used to temporarily hold keyboard input char string[ SIZE ],copy[ SIZE ]; int count = 0; int copyCount; int i; // used for loop control in string copying cout << "Enter a sentence:\n"; while ( ( c = cin.get() ) != '\n' && count < SIZE ) string[ count++ ] = c; string[ count ] = '\0'; // terminate cstring for ( copyCount = 0, i = 0; string[ i ] != '\0'; i++ ){ if ( string[ i ] != ' ' ) copy[ copyCount++ ] = string[ i ]; } // end for // Print whether or not sentence is palindrome if ( testPalindrome( copy, 0, copyCount - 1 ) ) cout << '\"' << string << "\" is a palindrome" << endl; else cout << '\"' << string << "\" is not a palindrome" << endl;

return 0; // indicates successful termination} // end main

Page 21: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

// function to see if sentence is a palindrome

bool testPalindrome( const char array[], int left, int right )

{

if ( left == right || left > right )

return true;

else if ( array[ left ] != array[ right ] )

return false;

else

return testPalindrome( array, left + 1, right - 1 );

} // end function testPalindrome

Page 22: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

Lab7_home2(7.37)

• 递归、查找数组中的最小值

Page 23: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

const int MAXRANGE = 1000;int recursiveMinimum( const int [], int, int );int main() { const int SIZE = 10; int array[ SIZE ], int smallest;

srand( time( 0 ) ); // initialize elements of array to random numbers for ( int loop = 0; loop < SIZE; loop++ ) array[ loop ] = 1 + rand() % MAXRANGE;

cout << "Array members are:\n"; // display array

for ( int k = 0; k < SIZE; k++ ) cout << setw( 5 ) << array[ k ];

cout << '\n'; smallest = recursiveMinimum( array, 0, SIZE - 1 ); cout << "\nSmallest element is: " << smallest << endl; return 0; // indicates successful termination} // end main

Page 24: 实验 11.28. Lab6_Ex1 Description of the Problem Write a program that simulates the rolling of two dice. The program should call rand to roll the first die,

// function to recursively find minimum array elementint recursiveMinimum( const int array[], int low, int high ){ static int smallest = MAXRANGE;

// if first element of array is smallest so far // set smallest equal to that element if ( array[ low ] < smallest ) smallest = array[ low ];

// if only one element in array, return smallest // else recursively call recursiveMinimum with new subarray return low == high ? smallest : recursiveMinimum( array, low + 1, high );} // end function recursiveMinimum