Task: Write C++ statements to perform the following tasks: 1. In the function ma
ID: 3753232 • Letter: T
Question
Task: Write C++ statements to perform the following tasks: 1. In the function main, declare an aray alpha of 10 elements of type int and initialize the first seven elements to 9, 14, 5, 24, 41, 78, 63, and the rest elements to 0 2. Write a function printArray to print all the elements of an array. The function printArray has two formal parameters, an int array 1ist and an int variable size representing the number of elements of the array. The function prototype of printArray is void printArray (int listl), int size)i Note, arrays are passed to a function by reference only, no need to add & Call the function printArray to print the elements of array alpha. Set the valuc of the cighth element of alpha to the sixth element minus nine times the third element. 3. 4. Print out the array alpha S. Set the last element of alpha to be the difference of the eighth element and the fith element. Print out the array alpha again 6. Write a function getSum that returns the sum of an array. The function prototype is: int getSun (int listl, int size In the function main, call the getSum function and output the result. Output the value of alpha [101. What do you see? Set alpha [10] to 999 and output its value again. What is wrong with your program? 7. 8. 9.Explanation / Answer
C++ code:
#include <iostream>
using namespace std;
// Question 2
void printArray(int list[], int size) {
cout << "Array elements are: ";
for(int i = 0; i < size; i++) {
cout << list[i] << " ";
}
cout << " ";
}
// Question 6
int getSum(int list[], int size) {
int su = 0; // initialize sum
// Iterate through all elements
// and add them to sum
for (int i = 0; i < size; i++) {
su += list[i];
}
return su;
}
int main() {
// Question 1
int alpha[] = {9, 14, 5, 24, 41, 78, 63, 0, 0, 0};
// Question 3
printArray(alpha, 10);
// Question 4
// 8th element means index is 7. Indices are 0 based. So for any index we subtract 1
alpha[7] = alpha[5] - 9 * alpha[2];
// Question 7
int calculatedSum = getSum(alpha, 10);
cout << "Calculated sum after updating 8th element is " << calculatedSum << " ";
// Question 8
cout << alpha[10] << " ";
// Question 9
alpha[10] = 999;
cout << alpha[10] << " ";
// The value printed previously is a garbage value since the index is 10 and we tried to get 11th element
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.