You are on page 1of 3

Computer Skills -2 (C++) (0206102)

Worksheet #5

1. Write C++ statements to do the following:

1.1 Declare an array list of 12 components of type int.


1.2 Initialize this array to the following values: 5, 6, 1, 4, 2, 3, 9.
1.3 Output the value of the tenth component of the array list.
1.4 Set the value of the fifth component of the array list to 35.
1.5 Set the value of the ninth component of the array list to the sum of the sixth
and thirteenth components of the array list.
1.6 Set the value of the fourth component of the array list to three times the value
of the eighth component minus 13.
1.7 Output list so that five components per line are printed.

2. Do the following after correcting the code:


2.1. Set the values of the array components to (12, 17, 200, 40, 90, 23, 89, 2, 11, 5)
using read statement (cin).
2.2. Output the elements of the array.

int myList(10);
for (int i = 1; i <= 10; i++)
cin >> myList;
for (int i = -1; i <= 10; i++)
cout << myList << " ";

3. Define a two-dimensional array named temp of three rows and four columns of type int
such that the first row is initialized to 6, 8, 12, 9; the second row is initialized to 17, 5, 10, 6;
and the third row is initialized to 14, 13, 16, 20.
3.1 Write C++ statements to accomplish the following:
a. Output the contents of the first row and first column element of temp.
b. Output the contents of the first row and last column element of temp.
c. Output the contents of the last row and first column element of temp.
d. Output the contents of the last row and last column element of temp.

1
Abdullah Shbtat
4. What is the output of the following program?

#include <iostream>
using namespace std;
int main()
{
int j;
int one[5];
int two[10];
for (j = 0; j < 5; j++)
one[j] = 5 * j + 3;
cout << "One contains: ";
for (j = 0; j < 5; j++)
cout << one[j] << " ";
cout << endl;
for (j = 0; j < 5; j++)
{
two[j] = 2 * one[j] - 1;
two[j + 5] = one[4 - j] + two [j];
}
cout << "Two contains: ";
for (j = 0; j < 10; j++)
cout << two[j] << " ";
cout << endl;
return 0;
}

5. Rewrite the previous program using (while and do while loop).

6. Write C++ program to sort an array of 10 components of type int (ascending).

7. Write C++ program to find the prime number in an array of 10 components.

8. Rewrite exercise 7 on an (two dimensional array 4×5).

9. Write a C++ program to find the maximum and minimum value of an (one dimensional array
and two dimensional array) of type double.

2
Abdullah Shbtat
10. Write a C++ program to test the equality of two arrays of type char.

11. Write a C++ program to check an array of 20 components of type int, whether the sum of
first ten components is greater than last ten components or not. Display on the screen (The
sum of first ten components and the sum of the last ten components).

12. Write a C++ program to find the number of even and odd numbers in an array of type int.
(using while, do while and for loop)

13. Write a C++ program to find the number of even and odd numbers in an (two dimensional
array 5×6) of type double, using while loop.

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

15. Write a C++ program that declares a (two dimensional array) matrix of 12 components of
type double.
15.1 Initialize the array so that the first row components are equal to the square of the
index variable.
15.2 The second row components are equal to three times the index variable.
15.3 The Third row components are equal to four times the index variable.
15.4 Output the array so that 4 elements per line are printed.

3
Abdullah Shbtat

You might also like