Hey guys. i need some help with my code so i am required to
1- Write a program that performs the following:
a- Declares and initializes an array a of 10 integers.
b- Finds the sum of the array elements
c- Finds the largest and smallest array element
d- Declares an array b of 10 integers. Copy the content of a into b, in reverse order.
e- Merges a and b into an array c of size 20 by alternating the elements
#include <iostream>
usingnamespace std;
int main() {
int a[10]={12, 54 , 22, 55 , 77 ,445, 62 , 1 , 2 ,3 };
int b[10];
int c[20];
int max = a[0];
int min = a[0];
int sum=0;
for (int i=0; i < 10 ; i++){
sum += a[i];
if( max <= a[i] )
max = a[i];
if( min >= a[i] )
min = a[i];
}
cout << "smallest array element :" << min << endl;
cout << "largest array element :" << max << endl;
for (int i = 9; i <=0 ; i++ ){
b[i] = a[i];
}
system ("pause");
return 0;
}
You can alternate the elements using a bool and doing
x= !x, or you can use a modulus operator... whatever floats your boat. I bet there are other ways to do it as well.
#include <iostream>
usingnamespace std;
int main()
{
int a[10]={12, 54 , 22, 55 , 77 ,445, 62 , 1 , 2 ,3 };
int b[10];
int c[20];
int max = a[0];
int min = a[0];
int sum=0;
for (int i=0; i < 10 ; i++){
sum += a[i];
if( max <= a[i] )
max = a[i];
if( min >= a[i] )
min = a[i];
}
cout << "smallest array element :" << min << endl;
cout << "largest array element :" << max << endl;
int x=9;
for (int i=0; i <=9 ; i++ )
{
b[i] = a[x];
x--;
cout<<b[i]<<endl;
}
system ("pause");
return 0;
}