0

count + and - numbers

How can write a program that reads an unspecified number of integers and determine the + and - that have been read and compute the average of the values

25th Aug 2017, 6:04 PM
abejide femi
3 Answers
+ 7
Providing a code would ruin the fun for you, no? :> I'm not proficient in JS, so I'll just write a C++ sample as to how I would do it. int count, temp, pos_counter, neg_counter; float sum; pos_counter = neg_counter = 0; cout << "How many numbers do you wish to input? : "; cin >> count; for (int i = 0; i < count; i++) { cout << "Input number : "; cin >> temp; sum += temp; if (temp > 0) pos_counter++; if (temp < 0) neg_counter++; } cout << "You have " << pos_counter << " positive integers and " << neg_counter << " negative integers." << endl; cout << "The mean of all input integers is " << sum/count;
26th Aug 2017, 4:25 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Query how many numbers to be input. Use a for loop to receive the input. Use conditional statement to evaluate the input and increment the counter for both + and - values. Store each of the value to a sum variable and divide by the number of inputs to obtain average.
25th Aug 2017, 7:13 PM
Hatsy Rei
Hatsy Rei - avatar
0
Thanks But code will be clearer
25th Aug 2017, 7:17 PM
abejide femi