+ 1

Who can answer ???

Allow a user to input 6 integer numbers into array Y. Then, by using pointer, go through the array elements to find the sum and the average of values divisible by 5 in the array contents. Finally, print on the screen the array contents, the sum and the average. By using #incloud<iostream> . And this my wrong try ... #include <iostream> using namespace std; int main() { int y[30], i; cout<<" enter the numbers of array "; for (i=0 ; i<6 ; i++) cin>>y[i] ; int *ptr ; ptr=&y[0]; int sum=0.0 ; for(i=0 ; i<6 ; i++) {if (i%5==0) sum=sum+(*(ptr+i)); float avg=sum/6 ; cout<<"the sum = "<<sum<<"\n"<<"the average = "<<avg;} return 0; }

10th May 2020, 1:28 AM
Mohamed Raafat Elsayed
Mohamed Raafat Elsayed - avatar
4 Answers
+ 1
u r welcome 😊
10th May 2020, 4:59 PM
Ahmed Mostafa
Ahmed Mostafa - avatar
+ 1
first in this loop you should check on the values not position so you should write if((*(ptr+i))%5==0) then to know the sum and the average, you don't need them into the loop so you write avg and printing operation out of loop and you need counter to know how many values divisible by 5 to calculate the average *Don't forget to check if this counter equal 0 or not to avoid dividing by 0 the solution is int count=0; for(i=0;i<6;i++){ if((*(ptr+i))%5==0){ sum=sum+(*(ptr+i)); count++; } } float avg; if(count==0) avg=0; else avg=sum/count; cout<<"sum="<<sum<<"\naverage="<<avg;
10th May 2020, 4:45 PM
Ahmed Mostafa
Ahmed Mostafa - avatar
+ 1
thank you very much 👏
10th May 2020, 4:55 PM
Mohamed Raafat Elsayed
Mohamed Raafat Elsayed - avatar
0
check the messages please 🙏
10th May 2020, 4:59 PM
Mohamed Raafat Elsayed
Mohamed Raafat Elsayed - avatar