+ 1

How to make a average program without using if statement?

5th Jan 2017, 4:34 AM
jerick
6 Answers
+ 6
You may need to elaborate on what you mean by an 'average' program.
5th Jan 2017, 4:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Let's say you don't have a predetermined number of quizzes. #include <iostream> using namespace std; int main (){ int quiz_num; cout << "Please input the number of quizzes to be evaluated" << endl; cin >> quiz_num; int score[quiz_num]; int sum = 0; int average; for (int i = 0; i < quiz_num; i++) { cout << "Please input score for quiz " << i + 1 << endl; cin >> score[i]; sum += score[i]; } average = sum / quiz_num; cout << "The average score is " << average << endl; return 0; } My example displays the use of arrays. I hope it is comprehensible.
5th Jan 2017, 4:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
If you are saying to make a program of calculating average without if statement, then there is no need of if statement in it. If you are saying that you want to create a program at an intermediate level, not an easy but average program 😅 then you can use ternary operator instead of if else statement. eg. #include <iostream> using namespace std; int main() { /*Use ternary operator ?: instead of if. Both works same.*/ int i=2; if(i%2==0) cout<<1; else cout<<0; /*above can also be written as*/ cout<<(i%2==0)?1:0; return 0; } output is- 11
5th Jan 2017, 4:49 AM
Sachin Artani
Sachin Artani - avatar
+ 1
can you please make me a sample program , its my first time to learn c++, thank you.
5th Jan 2017, 4:55 AM
jerick
+ 1
#include <iostream> using namespace std; //this is comment /* I'm gonna calculate the average of three numbers*/ int main(){ float num1,num2,num3,avg; cout<<"please enter your three scores."; cin>>num1; cin>>num2; cin>>num3; avg=(num1+num2+num3)/3; /*maybe you want it this way. an example: avg=(num1*2+num2*4+num3*2)/(8); */ cout<<"the average of your scoresare:"; cout<<avg; return 0; } you can add variables as many as you want and calculate their average.
5th Jan 2017, 7:10 AM
Mahdi Afshar
Mahdi Afshar - avatar
0
computing average of quizes
5th Jan 2017, 4:42 AM
jerick