Programing Assignment Arrays
1. True/False
a. True
b. True
c. False (Arrays are passed by pointer, not by value or reference directly)
d. False
e. True
f. False (No aggregate operations allowed)
g. False (Updates the sixth component)
h. False
i. True
j. False (Array size is 16)
k. False (Invalid declaration)
l. False
2. Array Declaration
a. salary
b. 10
c. double
d. 0 to 9
3. Errors in Declarations
a. Valid
b. Error: constint → const int; SIZE undefined
c. Error: Use int numList[10];
d. Valid (if using namespace std;)
e. Error: Syntax → double scores[50];
4. Valid Declarations
a. Invalid (Not an array)
b. Invalid (Size not constant)
c. Invalid (Negative size)
d. Invalid (Non-integer size)
5. Valid Index Range
0 to 49
6. C++ Statements
a. int alpha[15];
b. cout << alpha[9];
c. alpha[4] = 35;
d. alpha[8] = alpha[5] + alpha[12];
e. alpha[3] = 3 * alpha[7] - 57;
f.
cpp
Copy
Download
for (int i = 0; i < 15; i++) {
cout << alpha[i] << " ";
if ((i + 1) % 5 == 0) cout << endl;
7. Program Output
Copy
Download
-3 -1 1 3 5
5 -1 8 3 -1
8. Final Array
[2, 7, 6, 11, 10]
9. Final Array
[5, 6, 9, 19, 23, 37]
10. Corrected Code
cpp
Copy
Download
int myList[10];
for (int i = 0; i < 10; i++) cin >> myList[i];
for (int i = 0; i < 10; i++) cout << myList[i] << " ";
11. Array Index Out of Bounds
Accessing an invalid index; C++ does not check bounds.
12. Corrected Code
cpp
Copy
Download
for (int i = 0; i < 9; i++)
if (scores[i] > scores[i + 1])
cout << i << " and " << (i + 1) << " are out of order." << endl;
13. Array Initializations
a. double heights[10] = {5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0};
b. int weights[7] = {120, 125, 137, 140, 150, 180, 210};
c. char specialSymbols[] = {'$', '#', '%', '@', '&', '!', '^'};
d. string seasons[4] = {"fall", "winter", "spring", "summer"};
14. Valid Declarations
a. Valid
b. Valid
c. Invalid (Missing values)
d. Valid
e. Invalid (Insufficient size for null terminator)
15. Array Contents
[8, 9, 15, 12, 80, 0, 0, 0, 0, 0]
16. Output
Copy
Download
0 -2
16
2 -12
31
4 13
17. Valid Function Calls
a, b, d
18. Valid Function Calls
a, b
19. Output
Copy
Download
1 25000.00 750.00
2 36500.00 1095.00
3 85000.00 2550.00
4 62500.00 1875.00
5 97000.00 2910.00
20. Car Sales Code
cpp
Copy
Download
int cars[10], total = 0, maxCars = 0, maxIndex = 0;
for (int i = 0; i < 10; i++) {
inFile >> cars[i];
total += cars[i];
if (cars[i] > maxCars) {
maxCars = cars[i];
maxIndex = i + 1;
}
}
cout << "Total: " << total << "\nSalesperson " << maxIndex << " sold " << maxCars << endl;
21. Output
11 16 21 26 30
22. Output
5 15 25 35 45 28 33 38 43 48
23. Output
Each line shows cylinder number, radius, height, and lateral surface area.
24. Passed Parameter
Base address (pointer).
25. Pass by Value
No, unless wrapped.
26. Valid Statements
a, d, e, g
27. Valid Statements
d
28. Outputs Shelly
a, b, c
29. C++ Statements
a. strcpy(str1, "Sunny Day");
b. int length = strlen(str1);
c. strcpy(str2, name);
d.
cpp
Copy
Download
if (strcmp(str1, str2) <= 0)
cout << str1;
else
cout << str2;
30. Valid Statements
a, b, c, f, h
31. 2D Array
cpp
Copy
Download
int temp[3][4] = {{6,8,12,9}, {17,5,10,6}, {14,13,16,20}};
32. Access Elements
a. cout << temp[0][0];
b. cout << temp[0][3];
c. cout << temp[2][0];
d. cout << temp[2][3];
33. Sales Array
a. 30
b. 5
c. 6
d. Row-wise summation
e. Column-wise summation
34. C++ Statements
a. int alpha[10][20];
b. Initialize to 0:
cpp
Copy
Download
for (int i = 0; i < 10; i++)
for (int j = 0; j < 20; j++)
alpha[i][j] = 0;
c. Set first row to 1, others to 2:
cpp
Copy
Download
for (int j = 0; j < 20; j++) alpha[0][j] = 1;
for (int i = 1; i < 10; i++)
for (int j = 0; j < 20; j++)
alpha[i][j] = 2;
d. Columns as multiples:
cpp
Copy
Download
for (int i = 0; i < 10; i++) {
alpha[i][0] = 5;
for (int j = 1; j < 20; j++)
alpha[i][j] = 2 * alpha[i][j-1];
e. Print row-wise:
cpp
Copy
Download
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 20; j++)
cout << alpha[i][j] << " ";
cout << endl;
f. Print column-wise:
cpp
Copy
Download
for (int j = 0; j < 20; j++) {
for (int i = 0; i < 10; i++)
cout << alpha[i][j] << " ";
cout << endl;
35. beta Array Values
a. All zeros.
b. [[0,1,2], [1,2,3], [2,3,4]]
c. [[0,0,0], [0,1,2], [0,2,4]]
d. [[0,2,0], [2,0,2], [0,2,0]]
36. Function and Calls
a. Function:
cpp
Copy
Download
void print(int arr[][7], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 7; j++)
cout << arr[i][j] << " ";
cout << endl;
b. Calls:
cpp
Copy
Download
print(times, 30);
print(speed, 15);
print(trees, 100);
print(students, 50);