1. Write C++ statements to do the following: a. Declare an array alpha of 15 com
ID: 3659570 • Letter: 1
Question
1. 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.
2. What is stored in the list array after the following code executes? int list[5]; for(int j = 0; j < 5; j++) { list[j] = 2 * j + 5; if (j % 2 == 0) { list[j] = list[j] - 3; } }
3. What does array index out of bounds mean? What can happen if this occurs in your program?
4. Write C++ statements to define and initialize the following arrays using appropriate data types:
a. An array of heights has 6 components which have the following values: 5.5, 5.8, 6.3, 6.6, 4.9, 5.9.
b. An array of weights has 4 components which have the values: 140, 165, 190, 207.
c. An array of symbols which contains the following characters: '$', '%', '@', '!', '|', '&'.
d. An array of the seasons which contains "Spring", "Summer", "Fall", "Winter".
5. Given the following declaration, what is stored in the 8th element of the array? int list[10] = {1, 2, 3, 4, 5};
6. When an array is passed as an actual parameter to a function, what is really being passed in to the function? Why are arrays passed this way?
7. Write a for loop to initialize the following array (int data[10]) with the values 10, 9, 8
Explanation / Answer
answer-1.a) int alpha[15];
answer-1.b) cout<<alpha[9];
answer-1.c) alpha[5]=35;
answer-1.d) alpha[8]=alpha[5]+alpha[12];
answer-1.e) alpha[3]=(3*alpha[7])-57;
answer-1.f) for(int i=0;i<15;i++)
{
cout<<alpha[i]<<" ";
if((i+1)%5==0)
cout<<" ";
}
answer-2) 2 7 6 11 10
answer-3)array index out of bounds means, if you are trying to access data
of an array with undeclared index. It will give garbage value.
example: int array[10];
cout<<array[11];/*array index out of bounds*/
answer-4.a) double height[]= {5.5, 5.8, 6.3, 6.6, 4.9, 5.9};
answer-4.b) int weight[]={140, 165, 190, 207} ;
answer-4.c) char symbol[]={'$', '%', '@', '!', '|', '&'};
answer-4.d) string season[]={"Spring", "Summer", "Fall", "Winter"};
answer-5) 0
answer-6) when array is passed as actual parameter, an address of array is passed.
Because if we pass the value, then we have to make another copy of array.
answer-7) for(int i=10;i>=1;i--)
{
data[i-1]=i;
}
answer-8) for(int i=0;i<10;i++)
{
cin>>data[i];
}
answer-9) double sum=0.0;
for(int i=0;i<100;i++)
{
sum=sum+data[i];
}
cout<<" Sum:"<<sum;
answer-10) int min=data[0];
for(int i=1;i<100;i++)
{
if(data[i]<min)
min=data[i];
}
cout<<" Minimum:"<<min;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.