in c++ Write a program to: 1. Declare an integer array a of size 10 2. Assign it
ID: 3680074 • Letter: I
Question
in c++
Write a program to:
1. Declare an integer array a of size 10
2. Assign it values using the following formula:
a[i] = i*2+1
Print the contents of array a.
3. Copy all elements divisible by 3 into another array (say b). Print array b.
4. Create a new array (say c) with the first half of c as the second half of a and the second half of c as
the first half of a. Print array c.
5. Create a new array(say d) that contains all elements of array a in reverse. Print array d.
Contents of each array must be printed on a new line and array elements in each line should be separated
by a space.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{ int a[10] ; int i; int j=0; int n=10;
int b[10] ; int c[10] , d[10] ;
for ( i = 0 ; i< 10 ; i++ )
{ a[i] = i*2+1 ;
cout << " a[" << i << "]=" << a[i] ;
}
cout << endl ;
for ( i = 0 ; i< 10 ; i++ )
{
if ( (a[i] % 3 ) == 0 )
{ b[j] = a[i] ;
cout << " b[" << j << "]=" << b[j] ;
j++ ;
}
} cout << endl ;
int m = 5 ;
for ( i = 0 ; i< m ; i++ ){
c[i] = a[ m+i ] ;
c[m+i] = a[ i ] ;
}
for ( i = 0 ; i<n ; i++ ){
cout << " c[" << i << "]= " << c[i] ;
} cout << endl ;
for ( i = 0 ; i< n; i++ ){
d[i] = a[ 9-i ] ;
cout << " d[" << i << "]=" << d[i] ;
} cout << endl ;
return 0;
}
output;
a[0]=1 a[1]=3 a[2]=5 a[3]=7 a[4]=9 a[5]=11 a[6]=13 a[7]=15 a[8]=17 a[9]=19
b[0]=3 b[1]=9 b[2]=15
c[0]= 11 c[1]= 13 c[2]= 15 c[3]= 17 c[4]= 19 c[5]= 1 c[6]= 3 c[7]= 5 c[8]= 7 c[9]= 9
d[0]=19 d[1]=17 d[2]=15 d[3]=13 d[4]=11 d[5]=9 d[6]=7 d[7]=5 d[8]=3 d[9]=1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.