Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

SEHH2042 Computer Programming

Tutorial 8 – Arrays (1)

Q1.  Write a program called getInteger.cpp that asks the user to enter n integers (n is input by the user at the

beginning). The program stores the n integers in an array. After user’s input, the program prints out the integers input by the user, five in a row.

You can assume that the maximum value of n entered by the user is 20.

Sample result:

How  many  integers  to  enter?  7

?  10

?  15

?  20

?  5

?  12

?  8

?  27

The  input  integers  are:

10

8


15       20         5       12

27


Q2.  Modify Q1 above so that the program prints out “bar chart” with the input numbers, instead of printing

individual values. Each input number represents the number of asterisks ('*') in a bar.

You can assume that the maximum value of n entered by the user is 20.

Sample result:

How  many  integers  to  enter?  5

?  10

?  15

?  20

?  5

?  12

The  bar  chart  is:

**********

***************

********************

*****

************

 

Q3.  Write a program called randomInteger.cpp that finds the largest value of 20 randomly generated integers

and calculates the average of them. Values of these integers range from 1 to 100. In your program, you should use an array to store the integers. The program prints the random integers, five in a row, and prints the largest value and average among them. The seed value for the random number sequence should be set by the user at the beginning.

Sample result:

Please  enter  the  seed  value:  123

The  random  integers  are:

41      54       76         5      64

66      50       79       61      65

30      51       81       64      30

87      59       96       68      31

The  largest  number  is:  96

The  average  value  is:  57.9

Q4.  Write  a  program  called  maxAppear.cpp that  finds  the  most  displayed  number  among  100  randomly

generated integers range from 1 to 10. Similar to Q3, your program allows the user to set the seed value, and stores the integers in an array.

Sample result:

Please  enter  the  seed  value:  456

The  random  integers  are:

8

5

5

10

7

1

3

7

5

7

5  appears  the


9         7         8

7         7         2

5         9         1

8         5         8

5         3         3

4         5         1

5         4         8

8         7         3

9       10         1

5       10       10

most  with  14


7

3

4

10

3

7

4

2

3

6

times


10

6

4

2

5

3

9

7

5

4


9

2

1

9

4

10

9

10

6

1


6

6

5

6

3

2

4

6

7

1


4

4

7

5

6

4

2

3

7

1


Q5.  Modify all programs above by implementing appropriate function(s), and then use the function(s) in main().

The function prototypes of each question are given below:

Question

Function Prototype

Q1

void  printData(  int  list[],  int  size  );

Q2

void  printBar(  int  list[],  int  size  );

 

Q3

void  printData(  int  list[],  int  size  );

int  largest(  int  list[],  int  size  );

double  average(  int  list[],  int  size  );

 

Q4

void  printData(  int  list[],  int  size  );

void  maxAppear(  int  list[],  int  size,  int  &max,  int  &freq  );