Unit # 4: Arrays

 

 

 

Problem #1: Your program will take ten integer inputs in [0, 9]. The output of your program must describe the numbers in [0, 9] that were missing in the input. Further, it should print the input values that appeared more than once.

 

 

Examples:

 

Run#1:

The program will take ten input values between 0(inclusive) and 9 (inclusive)

Input Value 1

3

Input Value 2

4

Input Value 3

2

Input Value 4

1

Input Value 5

1

Input Value 6

8

Input Value 7

7

Input Value 8

4

Input Value 9

5

Input Value 10

9

 

 

RESULTS:

0 was missing

1 repeats more than once

4 repeats more than once

6 was missing

 

Run#2:

The program will take ten input values between 0(inclusive) and 9 (inclusive)

Input Value 1

6

Input Value 2

5

Input Value 3

2

Input Value 4

1

Input Value 5

9

Input Value 6

2

Input Value 7

9

Input Value 8

0

Input Value 9

5

Input Value 10

9

 

 

RESULTS:

2 repeats more than once

3 was missing

4 was missing

5 repeats more than once

7 was missing

8 was missing

9 repeats more than once

 

 

Problem#2: Your program will ask the user for the size of the input array. The program will then define the integer array of the input size. It will then take integer input values into the array. The values to the input array will be distinct. Your program must output the second largest value in the array.

 

Program Output:

Input Array Size

5

Input#1

45

Input#2

23

Input#3

90

Input#4

123

Input#5

55

 

Second Largest = 90

 

 

Problem #3: The mode of a list of numbers is defined as the number having the largest number of occurrences in the list. Write a program which finds all the modes in an input array of integers.

 

Input Array: 1 2 3 4 3 4 5 5

 

Modes in the array:

3  4  5

 

 

Problem #4: Your program should declare an integer array of input size and take distinct integer input values into it. Finally, the program should output the contents of the array in random order.

 

Program Output:

 

Run#1:

Input the size of the array:

7

 

Input Array: 1 2 3 4 5 6 7

Random Output: 7 2 3 6 1 5 4

 

Run#2:

Input the size of the array:

7

 

Input Array: 1 2 3 4 5 6 7

Random Output: 1 5 6 3 7 4 2

 

 

 

Problem #5: In this program, you will simulate the rolling of two dice. Use the random number generator to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Your program should roll the dice 10000 times. Use an integer array of the appropriate size to tally the number of times each possible sum appears. Display the results.

 

Example run:

Dice Roll Simulation

2 was rolled 263 times

3 was rolled 592 times

4 was rolled 817 times

5 was rolled 1120 times

6 was rolled 1389 times

7 was rolled 1597 times

8 was rolled 1363 times

9 was rolled 1103 times

10 was rolled 824 times

11 was rolled 619 times

12 was rolled 313 times

 

 

 

 

 

Problem #6:

 

Write a program to play a single game of Craps. The rules are as below:

 

You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated.

·       If the sum is 7 or 11 on the first throw, you win.

·       If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e., the “house” wins).

·       If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point.” To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making the point

 

 

Run#1:

Game of Craps

Roll#1 = 10

Your point is = 10

Roll#2 = 5

Roll#3 = 8

Roll#4 = 7

You lose!

 

Run#2:

Game of Craps

Roll#1 = 10

Your point is = 10

Roll#2 = 8

Roll#3 = 10

You Win!

 

Run#3:

Game of Craps

Roll#1 = 3

You lose!

 

Run#4:

Game of Craps

Roll#1 = 11

You Win!

 

Now, write a program that runs 10000 games of craps and answers the following questions:

 

a) How many games are won on the first roll, second roll, …, twentieth roll and after the twentieth roll?

 

b) How many games are lost on the first roll, second roll, …, twentieth roll and after the twentieth roll?

 

c) What are the chances of winning at craps?

 

d) What is the average length of a game of craps?

 

 

NOTE: The output of your program will vary slightly.

 

Program Output:

2242 games were won on the 1 rolls

1116 games were lost on the 1 rolls

 

763 games were won on the 2 rolls

1117 games were lost on the 2 rolls

 

524 games were won on the 3 rolls

800 games were lost on the 3 rolls

 

399 games were won on the 4 rolls

574 games were lost on the 4 rolls

 

294 games were won on the 5 rolls

417 games were lost on the 5 rolls

 

208 games were won on the 6 rolls

298 games were lost on the 6 rolls

 

143 games were won on the 7 rolls

181 games were lost on the 7 rolls

 

112 games were won on the 8 rolls

145 games were lost on the 8 rolls

 

75 games were won on the 9 rolls

102 games were lost on the 9 rolls

 

54 games were won on the 10 rolls

85 games were lost on the 10 rolls

 

45 games were won on the 11 rolls

63 games were lost on the 11 rolls

 

24 games were won on the 12 rolls

36 games were lost on the 12 rolls

 

21 games were won on the 13 rolls

26 games were lost on the 13 rolls

 

12 games were won on the 14 rolls

21 games were lost on the 14 rolls

 

9 games were won on the 15 rolls

20 games were lost on the 15 rolls

 

5 games were won on the 16 rolls

17 games were lost on the 16 rolls

 

3 games were won on the 17 rolls

9 games were lost on the 17 rolls

 

4 games were won on the 18 rolls

8 games were lost on the 18 rolls

 

3 games were won on the 19 rolls

6 games were lost on the 19 rolls

 

5 games were won on the 20 rolls

13 games were lost on the 20 rolls

 

 

Average length of each game = 3.3632

 

Chance of Winning = 0.4945

 

 

 

 

Problem #7: Imagine that N knights have decided to elect a leader by sitting at a round table and eliminating every Mth person around the circle. Let N =9 and let an array of size 9 (with values 1, 2...9) represent these knights in a circle. You will take an integer input for M (> 0). When eliminating every Mth person, if the count goes beyond the 9th cell of the array, your program must wrap around to the 1st cell of the array. Upon elimination, you can insert a zero in the Mth cell. When you have eliminated all but one of the knights, output the value of that knight to be the leader.

 

Example:

Start:   1 2 3 4 5 6 7 8 9

For M = 5:

1 2 3 4 0 6 7 8 9

0 2 3 4 0 6 7 8 9

0 2 3 4 0 6 0 8 9

0 2 3 0 0 6 0 8 9

0 2 0 0 0 6 0 8 9

0 2 0 0 0 0 0 8 9

0 2 0 0 0 0 0 8 0

0 0 0 0 0 0 0 8 0

Hence, 8 is the leader.

 

 

Problem #8: The int variable is unable to store numbers beyond 11 digits. Use two one-dimensional arrays of size= 20 to store the twenty digits of the two numbers. You can assume that the input (or randomized) values in each cell will be a digit in the range of 0, 1, 2….9.

 

Write programs that implement the following:

a)   Find the greater of the two input numbers and output the greater followed by the smaller.

b)   Subtract the smaller from the greater and output the result.

 

Examples:

1)

Input 1:

4 1 6 1 6 3 3 0 9 4 7 9 3 6 6 2 2 8 3 1

Input 2:

9 5 7 5 6 1 5 0 8 2 7 4 8 6 5 6 8 2 2 8

Greater:

9 5 7 5 6 1 5 0 8 2 7 4 8 6 5 6 8 2 2 8

Smaller:

4 1 6 1 6 3 3 0 9 4 7 9 3 6 6 2 2 8 3 1

Difference:

5 4 1 3 9 8 1 9 8 7 9 5 4 9 9 4 5 3 9 7

 

2)

Input 1:

5 3 4 0 9 8 6 8 4 6 8 8 3 5 0 3 9 6 8 7

Input 2:

5 9 2 9 9 6 5 1 9 7 3 5 3 4 4 7 3 9 8 6

Greater:

5 9 2 9 9 6 5 1 9 7 3 5 3 4 4 7 3 9 8 6

Smaller:

5 3 4 0 9 8 6 8 4 6 8 8 3 5 0 3 9 6 8 7

Difference:

0 5 8 8 9 7 8 3 5 0 4 6 9 9 4 3 4 2 9 9

 

 

Problem #9: For the above problem, take two 20-digit numbers as input values (as shown above) and compute their product.

 

Examples:

Input 1:

0  8  8  8  9  6  4  2  7  6  6  7  0  1  4  5  3  1  5  4 

 

Input 2:

8  3  0  5  6  3  0  6  3  9  6  5  5  1  1  0  5  2  5  6

 

Product:

7  3  8  3  4  0  8  9  3  3  8  7  0  4  0  1  6  8  2  6  9  7  5  6  1  2  5  6  0  0  0  0  7  1  7  7  4  2  4 

 

 

 

Problem #10:

Your program will accept a sequence of nine numbers as input. The numbers in the sequence will be a mix of 0’s and numbers in the range of 1-9.

 

An example would be:

          0 0 1 0 0 2 0 0 3

 

Your program should perform the following:

          Retain the non-zero numbers in their current position.

          Insert the missing numbers in the cells whose current value is 0.

          Create a second sequence of numbers in the range of 1-9 such that no two numbers at identical positions in the two sequences have the same value.

 

Example:

Input Sequence:                                     0   0   1   0   0   2   0   0   3

Completed Input Sequence:               4   5   1   6   7   2   8   9   3  

Completed Second Sequence:            1   6   9   3   5   7   2   8   4