0% found this document useful (0 votes)
21 views

Week7Lab S2 19

Uploaded by

Tiến Dũng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Week7Lab S2 19

Uploaded by

Tiến Dũng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SWE200024 – Technical Software Development

Lab 7 – Submit Task 7.3 and Task 7.4 as part of assignment 2.

Task 7.1 Find the error

Find the error in each of the following program segments and correct
the error:
a) #include <iostream>;

b) arraySize = 10; // arraySize was declared const

c) Assume that int b[ 10 ] = {};


for ( int i = 0; i <= 10; i++ )
b[ i ] = 1;

d) Assume that int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };


a[ 1, 1 ] = 5;

e)
double cube(float ); /*function prototype*/
...
cube(float number ) /*function prototype*/
{
return number*number *number;
}

f)
double y =123.45678;
int x;
x = y;
cout<<(double)x;

g)
double square(double number )
{
double number;

©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059


Week 7 Lab Page 1 of 4
SWE200024 – Technical Software Development

return number *number;


}
h) double f[ 3 ] = { 1.1, 10.01, 100.001, 1000.0001 };

Task 7.2 Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of
which is between 0 and 99, inclusive. As each number is read, print the number and indicate ‘duplicate
number’ if it is a duplicate number. After all 20 numbers have been entered; call a function to print out the
most frequent number in the array.
[Sample solution is available in Canavs]

Task 7.3 (Need to submit as a part of assignment 2)

Create a one-dimensional array to read 20 alphabetical letters (your program should be able to detect
and print out an error message if a non-alphabetical letter is entered). As each letter is entered, print a
message saying ‘duplicate letter’ if the letter is already in the array. Write a function that can sort the
array after all 20 letters have been entered. Write another function that print out the most frequent
letter and number of times it was entered. Prepare for the case where all 20 letters are different, or all
are the same. [Refer to sample solution given for Task 7.2]

Sample output:

©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059


Week 7 Lab Page 2 of 4
SWE200024 – Technical Software Development

©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059


Week 7 Lab Page 3 of 4
SWE200024 – Technical Software Development

Task 7.4 (Need to submit as a part of assignment 2)

Write a program that simulates the rolling of two dice. The program should use rand to roll the first and
second dice. The sum of the two values should then be calculated. [Note: Since each die can show an integer
value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum
and 2 and 12 the least frequent sums.]. There are 36 possible combinations of the two dice. Your program
should roll the two dice 50,000 times. Use a 2D array to tally the numbers of times each possible sum appears.
Print the results in a tabular format. The player wins if the total of the diagonal elements is greater than 8350,
otherwise the player loses.

Diagonal elements

Sample output: (The table formatting should look like the same as in the screen shots below)

©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059


Week 7 Lab Page 4 of 4

You might also like