2.1 Study the following descriptions and write only the statements to achieve the required objectives in both algorithm and C++ syntax.
2.1.1 The array called quantity has 20 elements. Write statements that will help in counting how many of these elements have a value of less than 50. Also determine the sum of the values that are more than or equal to 50. Display these 2 answers.
2.1.2 The array called itemPrice has 150 elements. Write the statements to determine and display the highest price. If there is more than one item with this price, the number of items must also be displayed.
2.1.3Three corresponding arrays of 250 elements each contain the following respectively: employee number, department and annual salary. Write only the statements to calculate the monthly salary for each and every employee. The following must be displayed the department and employee number of every employee who earns more than R100 000 per annum.
2.1.4 Three corresponding arrays of 250 elements each contain the following respectively: employee number, department and annual salary. The following must be displayed the department and employee number as well as the monthly salary of each employee that must be stored in a fourth parallel array called monSalay.
An array called number contains 20 integer values and must be filled with values in the following way:
Fill the array with consecutive even numbers starting from 10.
Start again at the first element and increase every fourth element with 3.
Replace the value of the first four elements each with its square.
Deduct 2 from the last 3 elements.
Swap the value of element 5 and element 15.
Add the value of the first element to that of the last element
Add consecutive integers (starting from 1) to all even elements.
Remember, the first element has a subscript of 0 and the last element (element 20) has a subscript of 19.
The following must be displayed:
All elements must be displayed each on a new line. Identify each element clearly.
Display the highest value in the array.
The lowest value in the array must be displayed.
Count and display how many even values and how many odd values the array contains.
Students in Programming 1 would like to understand the concept of Ohm’s law. You are required to write an application that will help students to calculate the voltage in an electric circuit using the formula below.
V = IR
Where V= volatge
I = Current
R- Resistance
Resistance
Voltage
4.0
8.5
6.0
7.35
9.0
15.3
3.0
5.4
2.9
4.8
Write a C++ application that will help the students to understand the concepts:
4.1 Create a C++ called Circuit and save it in a file called Circuit.cpp.
4.2 Declare all necessary variables and constants.
4.3 Declare two parallel arrays named current, resistance and initialise them using the information given above.
4.4 Declare another parallel array called voltage populate by calling using default values.
Current
0.62
14.89
13.21
16.55
18.62
9.47
6.58
18.32
12.15
3.98
Delicious Dish 3
6
4.5 Create a menu that will iterate until the user enters an invalid number. Create this menu by calling the function menus. There are five possible menu selections numbered from 1 to 7, if the user enters 8 or any other number the program must terminate.
NB: Make use of the switch statement for the menu selection.
Menu Option Description
1
Display all
Display all circuit details as shown in Figure 4.1.
2
Search volts: (See Figure 4.2)
The user must be prompted to enter the volts that he or she would like to search for.
The volts must then be searched in the corresponding array.
o If the value is found or not an appropriate message must be display as shown in Figure 4.2.
3
Search volts and indicate where it was found : (See Figure 4.2)
The user must be prompted to enter the volts that he or she would like to search for.
The volts must then be searched in the corresponding array.
o If the value is found it must display the value, the index at which it was found as well as the number of eminent for that particular volatge or an appropriate message must be display as shown in Figure 4.2.
4
Find the highest voltage (See Figure 4.3)
Display the highest voltage.
5
Find highest volatge with corresponding details: (See Figure 4.3)
Display the highest volts with their corresponding details
6
Display average volatge (See Figure 4.4).
Calculate and display average voltage.
7
Display number of measurements that are above the average volatge (See Figure 4.4).
8 or any other key
Exit : (See Figure 4.5)
Enter an operator (+, -, ×, /): +
Enter two operands: 5
8
5.0 + 8.0 = 13.0
An array called number contains 20 integer values and must be filled with values in the
following way:
Fill the array with consecutive even numbers starting from 10.
Start again at the first element and increase every fourth element with 3.
Replace the value of the first four elements each with its square.
Deduct 2 from the last 3 elements.
Swap the value of element 5 and element 15.
Add the value of the first element to that of the last element
Add consecutive integers (starting from 1) to all even elements.
Remember, the first element has a subscript of 0 and the last element (element 20)
has a subscript of 19.
The following must be displayed:
All elements must be displayed each on a new line. Identify each element clearly.
Display the highest value in the array.
The lowest value in the array must be displayed.
Count and display how many even values and how many odd values the array
contains.
Write the exact output of the following algorithms:
1.1 Study the following array in an algorithm called arr. Indicate what the values in arr
would be after the given code has been executed.
4 6 8 10 120 14 15 99 102 55
for x = 0 to 9
if arr(x) Mod 2 = 0 then
arr(x) = arr(x) + 5
endif
if x Mod 2 = 0 then
arr(x) = arr(x) + 1
endif
next x
x = x – 5
for y = 2 to 8 step 2
arr(y) = arr(x) + y
next y
for z = 1 to 6 step 3
arr(z) = z * y + x
next
Students in Programming 1 would like to understand the concept of Ohm’s law. You are
required to write an application that will help students to calculate the voltage in an electric
circuit using the formula below.
V = IR
Where V= volatge
I = Current
R- Resistance
Resistance Voltage
4.0 0
8.5 0
6.0 0
7.35 0
9.0 0
15.3 0
3.0 0
5.4 0
2.9 0
4.8 0
Write a C++ application that will help the students to understand the concepts:
4.1 Create a C++ called Circuit and save it in a file called Circuit.cpp.
4.2 Declare all necessary variables and constants.
4.3 Declare two parallel arrays named current, resistance and initialise them using the
information given above.
4.4 Declare another parallel array called voltage populate by calling using default values.
4.5 Create a menu that will iterate until the user enters an invalid number. Create this menu
by calling the function menus. There are five possible menu selections numbered from
1 to 7, if the user enters 8 or any other number the program must terminate.
NB: Make use of the switch statement for the menu selection.
Menu
Option
Description
1 Display all
Display all circuit details as shown in Figure 4.1.
2 Search volts: (See Figure 4.2)
The user must be prompted to enter the volts that he or she would like to
search for.
The volts must then be searched in the corresponding array.
o If the value is found or not an appropriate message must be
display as shown in Figure 4.2.
3 Search volts and indicate where it was found : (See Figure 4.2)
The user must be prompted to enter the volts that he or she would like
to search for.
The volts must then be searched in the corresponding array.
o If the value is found it must display the value, the index at which
it was found as well as the number of eminent for that particular
volatge or an appropriate message must be display as shown in
Figure 4.2.
4 Find the highest voltage (See Figure 4.3)
Display the highest voltage.
5 Find highest volatge with corresponding details: (See Figure 4.3)
Display the highest volts with their corresponding details
6 Display average volatge (See Figure 4.4).
Calculate and display average voltage.
7 Display number of measurements that are above the average volatge (See
Figure 4.4).
8 or any
other key
An array called number contains 20 integer values and must be filled with values in the
following way:
Fill the array with consecutive even numbers starting from 10.
Start again at the first element and increase every fourth element with 3.
Replace the value of the first four elements each with its square.
Deduct 2 from the last 3 elements.
Swap the value of element 5 and element 15.
Add the value of the first element to that of the last element
Add consecutive integers (starting from 1) to all even elements.
Remember, the first element has a subscript of 0 and the last element (element 20)
has a subscript of 19.
The following must be displayed:
All elements must be displayed each on a new line. Identify each element clearly.
Display the highest value in the array.
The lowest value in the array must be displayed.
Count and display how many even values and how many odd values the array
contains.
Matrix Rotations
You are given a square matrix A of dimensions NxN. You need to apply the below given 3 operations on the matrix A.
Rotation: It is represented as R S where S is an integer in {90, 180, 270, 360, 450, ...} which denotes the number of degrees to rotate. You need to rotate the matrix A by angle S in the clockwise direction. The angle of rotation(S) will always be in multiples of 90 degrees.
Update: It is represented as U X Y Z. In initial matrix A (as given in input), you need to update the element at row index X and column index Y with value Z.
After the update, all the previous rotation operations have to be applied to the updated initial matrix.
Querying: It is represented as Q K L. You need to print the value at row index K and column index L of the matrix A. Input
The first line contains a single integer N.
Next N lines contain N space-separated integers Aij (i - index of the row, j - index of the column).
Next lines contain various operations on the array. Each operation on each line (Beginning either with R, U or Q).
-1 will represent the end of input.Output
For each Query operation print the element present at row index K and colum index L of the matrix in its current state.Explanation
For Input:
2
1 2
3 4
R 90
Q 0 0
Q 0 1
R 90
Q 0 0
U 0 0 6
Q 1 1
-1
Initial Matrix
1 2
3 4
For R 90, clockwise rotation by 90 degrees, the matrix will become
3 1
4 2
For Q 0 0, print the element at row index 0 and column index 0 of A, which is 3.
For Q 0 1, print the element at row index 0 and column index 1 of A, which is 1.
Again for R 90, clockwise rotation by 90 degrees, the matrix will become
4 3
2 1
For Q 0 0, print the element at row index 0 and column index 0 of A, which is 4.
For U 0 0 6, update the value at row index 0 and column index 1 in the initial matrix to 6. So the updated matrix will be,
6 2
3 4
After updating, we need to rotate the matrix by sum of all rotation angles applied till now(i.e. R 90 and R 90 => 90 + 90 => 180 degrees in clockwise direction).
After rotation the matrix will now become
4 3
2 6
Next for Q 1 1, print the element at row index 1 and column index 1 of A, which is 6.
output
3
1
4
6
Sample Input 1
2
1 2
3 4
R 90
Q 0 0
Q 0 1
R 90
Q 0 0
U 0 0 6
Q 1 1
-1
Sample Output 1
3
1
4
6
Sample Input 2
2
5 6
7 8
R 90
Q 0 1
R 270
Q 1 1
R 180
U 0 0 4
Q 0 0
-1
Sample Output 2
5
8
8
Here is an array of 10 integers. 5 3 8 1 7 0 2 6 4. Draw this array after the first iteration of the large loop in a selection sort (Sorting from smallest to largest)