Can someone please help me with Step 9. I\'m not sure exactly how to write the s
ID: 3778287 • Letter: C
Question
Can someone please help me with Step 9. I'm not sure exactly how to write the statement, because what I am doing now is wrong. Below will be a copy of what I have so far. Thanks a lot!
#include<iostream>
using namespace std;
int main()
{
int index, even=0;
//Step 3:
double qArray[]={3.7,12.3,4.5,10.6,2.3,5.4,9.8,15.2,7.7,19.8};
//Step 4:
qArray[2]=75.6;
//Step 5:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 6:
for(int index=0;index<10;index++)
if(qArray[index]<10.5)
{
qArray[index]=7.6;
}
//Step 7:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 8:
int sum=0;
for(int index=0;index<10;index++)
sum=sum+qArray[index];
cout<<"Sum: "<<sum<<endl;
//Step 9:
for(int index=0;index<10;index++)
{
if(qArray[index]%2==0)
{
cout<<"The even numbers are : "<<qArray[index]<<endl;
}
}
//Step 10:
for(int index=0;index<10;index++)
{
cout<<endl<<"Enter a number :"<<endl; // Step 10 completed.
cin>>qArray[index];
}
//Step: 11
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
system ("pause");
return 0;
}
Explanation / Answer
Even and odd concept can be applied to only integers.You cant do on double.Hence you should use int array not double array.
Still you want use double then use fmod for division by 2.I have given code 1 for double and code 2 for integers.
=====================================================================================
code 1:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int index, even=0;
//Step 3:
double qArray[]={3.7,12.3,4.5,10.6,2.3,5.4,9.8,15.2,7.7,18.8};
//Step 4:
qArray[2]=75.6;
//Step 5:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 6:
for(int index=0;index<10;index++)
if(qArray[index]<10.5)
{
qArray[index]=7.6;
}
//Step 7:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 8:
int sum=0;
for(int index=0;index<10;index++)
sum=sum+qArray[index];
cout<<"Sum: "<<sum<<endl;
//Step 9:
for(int index=0;index<10;index++)
{
if(fmod(qArray[index],2.0)==0.0)
{
cout<<"The even numbers are : "<<qArray[index]<<endl;
}
}
//Step 10:
for(int index=0;index<10;index++)
{
cout<<endl<<"Enter a number :"<<endl; // Step 10 completed.
cin>>qArray[index];
}
//Step: 11
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
return 0;
}
output:
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
3.7 12.3 75.6 10.6 2.3 5.4 9.8 15.2 7.7 19.8
7.6 12.3 75.6 10.6 7.6 7.6 7.6 15.2 7.6 19.8
Sum: 166
Enter a number :
1
Enter a number :
2
Enter a number :
3
Enter a number :
4
Enter a number :
5
Enter a number :
6
Enter a number :
7
Enter a number :
8
Enter a number :
9
Enter a number :
10
1 2 3 4 5 6 7 8 9 10
===============================================================
code 2:(integer)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int index, even=0;
//Step 3:
int qArray[]={3.7,12.3,4.5,10.6,2.3,5.4,9.8,15.2,7.7,18.8};
//Step 4:
qArray[2]=75.6;
//Step 5:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 6:
for(int index=0;index<10;index++)
if(qArray[index]<10.5)
{
qArray[index]=7.6;
}
//Step 7:
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
cout<<endl;
cout<<endl;
//Step 8:
int sum=0;
for(int index=0;index<10;index++)
sum=sum+qArray[index];
cout<<"Sum: "<<sum<<endl;
//Step 9:
for(int index=0;index<10;index++)
{
if(fmod(qArray[index],2.0)==0.0)
{
cout<<"The even numbers are : "<<qArray[index]<<endl;
}
}
//Step 10:
for(int index=0;index<10;index++)
{
cout<<endl<<"Enter a number :"<<endl; // Step 10 completed.
cin>>qArray[index];
}
//Step: 11
for(int index=0;index<10;index++)
cout<<qArray[index]<<" ";
return 0;
}
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ ans.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
3 12 75 10 2 5 9 15 7 18
7 12 75 7 7 7 7 15 7 18
Sum: 162
The even numbers are : 12
The even numbers are : 18
Enter a number :
1
Enter a number :
2
Enter a number :
3
Enter a number :
4
Enter a number :
5
Enter a number :
6
Enter a number :
7
Enter a number :
8
Enter a number :
9
Enter a number :
10
1 2 3 4 5 6 7 8 9 10
==================================================
Ans to step 9:
when even numbers compred to uneven number they always give remainder 0 when divide by 2