Single Dimensional Array – Assignment-1
Q1.Write C++ statements to do the following:
Declare and initialize an array of scores of 20 student of type int.
- Display the 10th component of the array scores.
- Display the 5th component of the array scores
- Set the value of the 8th component of the array scores to 35.
- Set the value of the 6th component of the array scores to the sum of the 9th and 12th components of the array scores.
- Set the value of the 14th component of the array scores to 2 times the value of the 7th component minus 25.
- Display the array scores so that 4 components per line are printed
2. a. Given declaration, answer the questions.
int a[5] ={3, 7, 4, 9, 6};
(i). Give the value of a[4].
(ii).Give the value of a[5].
b. Given declaration, answer the questions.
int a[5] = {3, 7, 4, 9, 6};
(i) What is a[1] + a[4]?
(ii) What is a[0] – a[2]?
(iii) What is a[0] + 2?
(iv) What is a[1 + a[0]]?
c. Given declaration, answer the questions
int a[5] = {3, 7, 4, 9, 6};
for(i = 0; i <= 4; i = i + 1}
cout<< a[4 – i];
(i) When i is 0, What is the value of a[4 – 0]
(ii) When i is 2, What is the value of a[4 – 2]
(iii) When i is 4, what is printed?
(iv)What is the final output of this code?
d.Write a code to print the array in reverse order.
3. Give output (Please show the memory diagram/dry run)
(i) #include <iostream.h>
void main()
{
int count = 0, a[5] = {3, 7, 4, 9, 6}, i, sum=0;
for(i = 0; i <= 4; i = i + 1)
if(a[i] > 5)
sum = sum + a[i];
cout<< sum;
}
(ii) #include <iostream.h>
void main()
{
int count = 0, a[5] = {3, 7, 4, 9, 6}, i;
for(i = 0; i <= 4; i = i + 1)
if(a[i] > 5)
{
count = count + 1;
cout<<a[i];
}
cout<< count;
}
(iii)#include <iostream.h>
void main()
{
int a[5] = {3, 7, 4, 9, 6}, i, sum=0, count=0;
for(i = 0; i <= 4; i = i + 1)
{
if( a[i] > 5 )
sum = sum + a[i];
if( a[i] <= 5 )
count = count + 1;
}
cout<< sum;
cout<< count;
}