Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Sultan Qaboos University

College of Science, Department of Computer Science


COMP2002, Spring 2011
Week 12 Lab Exercises (Arrays-Part1)

Exercise 1:

Write a C++ program which allows the user to manipulate two vectors (arrays) A and B of real numbers with the
following menu options:

1. Add A and B into a new vector C (i.e. C=A+B) and display the vector C

2. Display both vectors (A and B) on the screen

Q. Quit

The program must first allow the user to enter the size of the vectors (n) from the keyboard, generate the elements
of the 1st vector (say A) randomly and finally read the elements of the 2nd vector (say B) from an input file. Once
the above preparation has been done, the program should keep displaying the menu options and perform the
corresponding task of each option until the user selects the option Q (i.e. Quit).

Note:
a. Assume that the maximum vector size is 20 (but can be smaller)
b. Implement and test the menu options one at a time
c. You may or may not use functions to perform each menu option

Exercise 2:
Modify the above program to add and implement the following menu option:

3. Perform the Dot Product of A and B. The dot product (i.e. A.B) can be calculated using the following
formula:

where ai’s and bi’s are the elements of A and B respectively

Submit this solution (Lab12_99999.cpp) on Moodle.

Exercise 3: Additional Practice Exercise (not to be submitted):

1. Write a C++ program which uses two functions each is accepting a string of any length and perform the
following tasks:
a. The first function convert the string’s character to UPPER cases. The function has the following
prototype: string upper(string tname)
b. The second function convert the string’s character to LOWER cases.

(Hint: Use the C++ built-in functions toupper() and tolower() to convert each character of the string)

2. Solve questions 1 to 13 from the textbook on pages 296 and 297.

You might also like