0

Calculating averages of numbers with loops and arrays

How do I calculate the average of prompted numbers using loops and arrays?

14th Apr 2018, 12:04 AM
love
14 Réponses
+ 6
I got this to work, but it gives the wrong answer. I tried using a loop to input data, but it didn't work. I'll have to study it some more. https://code.sololearn.com/WV6zfFQm2qsV/?ref=app
14th Apr 2018, 1:30 AM
Eric Zatarack
Eric Zatarack - avatar
+ 6
another way might be to make a button in HTML and send the input to JS. I'm a bit of a noobie with this too.😞
14th Apr 2018, 1:38 AM
Eric Zatarack
Eric Zatarack - avatar
+ 4
//in C++ #include <iostream> using namespace std; int main(){ int array1[4]={1,3,5,7}; int average, sum=0; for (int i=0; i<4; i++) { sum += array1[i]; } average=sum/4; cout<<average; return 0; }
14th Apr 2018, 12:29 AM
Eric Zatarack
Eric Zatarack - avatar
+ 4
love, it's similar, but without #include, using and int main(). I'm gonna try one out in playground.
14th Apr 2018, 12:35 AM
Eric Zatarack
Eric Zatarack - avatar
14th Apr 2018, 12:46 AM
Eric Zatarack
Eric Zatarack - avatar
+ 4
I'm not an expert in JS, so I'd have to look it up. wait a few minutes...
14th Apr 2018, 1:03 AM
Eric Zatarack
Eric Zatarack - avatar
+ 3
I think you have to use a prompt and another "for" loop to input the data into the array.
14th Apr 2018, 12:59 AM
Eric Zatarack
Eric Zatarack - avatar
+ 3
// created by arif ansari in c++ #include<iostream> using namespace std; int main() { int a[10]={2,3,5,2,4,6,6,3,2,4} int s=0,av; for(int i=0;i<10;i++) { s+=a[i]; av=s/10; } cout<<"average ="<<av; return 0; } //i hope this might help...
14th Apr 2018, 6:40 AM
ARIF ANSARI
ARIF ANSARI - avatar
0
How bout in JavaScript
14th Apr 2018, 12:30 AM
love
0
Thanks. It works. What if you want to prompt the user for those numbers that need to be averaged?
14th Apr 2018, 12:56 AM
love
0
Thanks. It works. What if you want to prompt the user for those numbers that need to be averaged?
14th Apr 2018, 12:56 AM
love
0
How exactly should I do it. Cause the average I'm getting is not accurate
14th Apr 2018, 1:01 AM
love
0
Ok. Thank you so much
14th Apr 2018, 1:04 AM
love
0
Nice
14th Apr 2018, 3:41 AM
Cristhofer Martinez Garmendia
Cristhofer Martinez Garmendia - avatar