array searching vs nested if else in c++ -
what array searching version of loop? same loop below without nested if else.
test t; int a; int b; int c; int d; int f; for(int = 0; < gradecount;i++){ if(t.getscore() <= 100 && t.getscore() >= 90) a++; else if(t.getscore() >= 80 && t.getscore() <= 89) b++; else if(t.getscore() >= 70 && t.getscore() < 79) c++; else if(t.getscore() >= 60 && t.getscore() < 69) d++; else if(t.getscore() <= 59) f++; }
this program self-explaining. hope helps.
#include <iostream> #include <string> using namespace std; int main() { int rank[5]={0}; //rank[0] grade f //rank[1] grade d //rank[2] grade c //rank[3] grade b //rank[4] grade int num=0; cout<< "enter number b/w 0-100, press -1 exit"<<endl; while(num != -1) { cin>>num; int onetenthofnum = num/10; int index = (onetenthofnum<=5)? 0: onetenthofnum%5; rank[index]++; } return 0; }
Comments
Post a Comment