You are on page 1of 3

CPT111: Principles of Programming

Lab 9: Array Basics

Instructions: Use comments in your program to enhance readability. For each question,
your lab submission must contain the following items in a Word document:

Answers or explanations where required


C++ source code
Screenshot of program output

Also submit a zip file containing all your C++ source codes (.cpp files).

Question 1
Trace the program manually to answer the questions below.
1 #include <iostream>
2
3 using namespace std;
4
5 //global function prototypes
6 void ShowScope (int& x, int y);
7
8 int a, b;
9
10 int main ()
11 {
12 a = 10;
13 b = 20;
14 cout << "Before activating ShowScope a = " << a
15 << " and b = " << b << endl;
16 ShowScope (a, b);
17 cout << "\nAfter ShowScope a = " << a << " b = " << b << endl;
18
19 return 0;
20 } //end of main
21
22 //Function: ShowScope()
23 void ShowScope (int& x, int y)
24 {
25 int a, c;
26
27 a = 100;
28 c = 200;
29 x = 300;
30 y = 400;
31 cout << "Inside ShowScope, a = " << a << " b = " << b << endl;
32 cout << "x = " << x << " and y = " << y << endl;
33 }

a) Identify all global variables.

b) Identify all local variables in ShowScope().

c) Identify all local variables in main().

d) Identify all formal parameters in ShowScope(). Indicate if each parameter is a value


or reference parameter.

1
CPT111: Principles of Programming
Lab 9: Array Basics

e) Identify all actual parameters in the function call to ShowScope().

f) Show the output generated by statements 14 and 15 in the function main().

g) Show the output generated by statements 31 and 32 in the function ShowScope().

h) Show the output generated by statement 17 when control is returned to function


main().

Question 2
Write a C++ program that does the following:
a) Declare and array beta of 20 components of type double.

b) Initialize each component of the array beta to 0.

c) Output the value of the fifth component of the array beta.

d) Set the value of the ninth component of the array beta to 70.50.

e) Set the value of the twelfth component of beta to four times the value of the eighth
component of beta minus 15.

f) Use a for loop to output the value of a component of beta if its index is a multiple of
3.

g) Output the value of the last component of beta.

h) Output the value of beta so that ten components per line are printed.

Question 3
Write a program that asks the user to type 10 integers of an array. The program must compute
and write how many integers are greater than or equal to 10.

Question 4
Write a C++ program that declares an array alpha of 50 components of type double. Initialize
the array so that the first 25 components are equal to the square of the index variable, and the
last 25 components are equal to three times the index variable. Output the array so that 10
elements per line are printed.

2
CPT111: Principles of Programming
Lab 9: Array Basics

Question 5
A car dealer has 10 salespersons. Each salesperson keeps track of the number of cars sold
each month and reports it to the management at the end of the month. The management
keeps the data in a file (cars.dat) and assigns a number, 1 to 10, to each salesperson.
Declare an array named cars of 10 components of type int to store the number of cars sold
by each salesperson. Write the code to store the number of cars sold by each salesperson in
the array cars, output the total numbers of cars sold at the end of each month, output the
maximum number of cars sold, and output the salesperson(s) number selling the maximum
number of cars.

Sample input data in cars.dat:


14
5
6
20
4
8
3
20
1
12

Sample output:
The total number of cars sold = 100
The maximum number of cars sold = 20
The salesperson(s) selling the maximum number of cars: #4 #8

You might also like