Write C++ statements to do the following: a. Declare an array alpha of 15 compon
ID: 3629564 • Letter: W
Question
Write C++ statements to do the following:a. Declare an array alpha of 15 components of type int.
b. Output the value of the 10th component of the alpha array.
c. Set the value of the 5th component of the alpha array to 35.
d. Set the value of the 9th component of the alpha array to the sum of the 6th and 13th components of the alpha array.
e. Set the value of the 4th component of the alpha array to three times the value of the 8th component minus 57.
f. Output alpha so that five components appear on each line.
Explanation / Answer
Since you're declaring the array but not initializing all the values yourself (except for the 5th component - you give it a 35) expect the system to come up with its own numbers, they're pretty random and usually big integer numbers too.
Also, there are many different ways to output alpha at the end so that we get 5 elements on each line, you can write them component by component but it's not practical.
Or you can loop through all the elements and decide where each component goes based on the loop counter (etween 0 and 5) then (5 - 10) then (10 - 15).
What I do here is simpler, 3 different loops, one starting at 0 and stopping at 5 to output the first row. The next one from 5 to 10. The last row from 10 to 15.
Here it goes.
#include <iostream.h>
#include <stdlib.h>
int main ()
{
int* alpha = new int[15]; // step a.
cout << "10th component: " << alpha[10] << endl; // step b.
alpha[5] = 35; // step c.
alpha[9] = alpha[6] + alpha[13]; // step d.
alpha[4] = 3*alpha[8] - 57; // step e.
// step f in 3 parts for each of the 3 lines 0-5, 5-10, 10-15
for (int i = 0; i < 5; i++)
{
cout << alpha[i] << " :: ";
}
cout << endl; // this marks the end of the 1st line
for (int i=5; i < 10; i++)
{
cout << alpha[i] << " :: ";
}
cout << endl; // this marks the end of the 2nd line
for (int i=10; i < 15; i++)
{
cout << alpha[i] << " :: ";
}
cout << endl; // this marks the end of the 1st line
return 0;
}
Hope this helps, good luck :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.