Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 22, 2015

Change multiple if to a loop?

Change multiple if to a loop?


I've just learned how to use loops, but I cant figure out how to translate this code:

#define _CRT_SECURE_NO_WARNINGS  #include   #include   #include     int main()  {      float manyTest, gr1, gr2, gr3, gr4, gr5, gr6, gr7, gr8, gr9, gr10, gr11, gr12, gr13, gr14, gr15, gr16, gr17, gr18, gr19, gr20, gr21, gr22, gr23, gr24, gr25;      manyTest = gr1 = gr2 = gr3 = gr4 = gr5 = gr6 = gr7 = gr8 = gr9 = gr10 = gr11 = gr12 = gr13 = gr14 = gr15 = gr16 = gr17 = gr18 = gr19 = gr20 = gr21 = gr22 = gr23 = gr24 = gr25 = 0;        printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");      scanf(" %f", &manyTest);      if (manyTest <= 0) {          printf("The Minimum is 1!\n");        }      else if (manyTest > 25){          printf("The Maximum is 25!\n");        }      else {          if (manyTest == 1) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);          }          else if (manyTest == 2) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);          }          else if (manyTest == 3) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);            }          else if (manyTest == 4) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);            }          else if (manyTest == 5) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);          }          else if (manyTest == 6) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);            }          else if (manyTest == 7) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);          }          else if (manyTest == 8) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr2);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);          }          else if (manyTest == 9) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);          }          else if (manyTest == 10) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);          }          else if (manyTest == 11) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);          }          else if (manyTest == 12) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);          }          else if (manyTest == 13) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);          }          else if (manyTest == 14) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);          }          else if (manyTest == 15) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);          }          else if (manyTest == 16) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);          }          else if (manyTest == 17) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);          }          else if (manyTest == 18) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);          }          else if (manyTest == 19) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);          }          else if (manyTest == 20) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);          }          else if (manyTest == 21) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);              scanf(" %f", &gr21);          }          else if (manyTest == 22) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);              scanf(" %f", &gr21);              scanf(" %f", &gr22);          }          else if (manyTest == 23) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);              scanf(" %f", &gr21);              scanf(" %f", &gr22);              scanf(" %f", &gr23);          }          else if (manyTest == 24) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);              scanf(" %f", &gr21);              scanf(" %f", &gr22);              scanf(" %f", &gr23);              scanf(" %f", &gr24);          }          else if (manyTest == 25) {              printf("Write down your grades on those tests:\n");              scanf(" %f", &gr1);              scanf(" %f", &gr2);              scanf(" %f", &gr3);              scanf(" %f", &gr4);              scanf(" %f", &gr5);              scanf(" %f", &gr6);              scanf(" %f", &gr7);              scanf(" %f", &gr8);              scanf(" %f", &gr9);              scanf(" %f", &gr10);              scanf(" %f", &gr11);              scanf(" %f", &gr12);              scanf(" %f", &gr13);              scanf(" %f", &gr14);              scanf(" %f", &gr15);              scanf(" %f", &gr16);              scanf(" %f", &gr17);              scanf(" %f", &gr18);              scanf(" %f", &gr19);              scanf(" %f", &gr20);              scanf(" %f", &gr21);              scanf(" %f", &gr22);              scanf(" %f", &gr23);              scanf(" %f", &gr24);              scanf(" %f", &gr25);          }          float avg = (gr1 + gr2 + gr3 + gr4 + gr5 + gr6 + gr7 + gr8 + gr9 + gr10 + gr11 + gr12 + gr13 + gr14 + gr15 + gr16 + gr17 + gr18 + gr19 + gr20 + gr21 + gr22 + gr23 + gr24 + gr25) / manyTest;          printf("Your average grade is: %.2f\n", avg);        }        system("pause");      return 0;    }  

from all of those if...if...if...if....if to some single loops. As you can see, my code is VERY long, and I made it just before I learned how to use loops. So, now after I've learned I have no idea how to do it.

Answer by Sourav Ghosh for Change multiple if to a loop?


To answer the first part, regarding reducing the if...else condition checks you cannot remove all your if..else condition from your code, however, you can reduce your else part for sure. You need to use a switch statement for that purpose. Something like

if (manyTest <= 0) {      printf("The Minimum is 1!\n");    }  else if (manyTest > 25){      printf("The Maximum is 25!\n");    }  else {      switch(manyTest)      {           case 1: //.... break;         case 2: //..... break;      }  

can be of your help.

Then, to reduce the number of variable used in your code, you can make use of an array, and use a for loop to iterate certain number of times to fill the required number of inputs.

Answer by ameyCU for Change multiple if to a loop?


You can use an array , and iterate over to take inputs .

Some thing like this could be done -

int n;                          // number of tests  scanf("%d",&n);                 // take input number of tests   int grades[n];                  // declare your array   for(int i=0;i

Answer by Sarz for Change multiple if to a loop?


You can use simple array for your for grade input and for average, Its just one step ahead for your

int main()  {      int manyTest = 0;      float gr[25];        printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");      scanf(" %f", &manyTest);      if (manyTest <= 0) {          printf("The Minimum is 1!\n");        }      else if (manyTest > 25){          printf("The Maximum is 25!\n");        }      for(int i=0;i

Answer by Kerrek SB for Change multiple if to a loop?


Here's a relatively robust and complete example that doesn't restrict the total amount of input. Rather, you send EOF to end the input stream:

#include   #include     int main() {    int total = 0;    double acc = 0, val;      for (;;) {      fputs("Value: ", stdout);      fflush(stdout);        errno = 0;      int res = fscanf(stdin, "%lf", &val);        if (res == EOF) {        // Handle successful end of input or read error.        if (errno == 0) {          fprintf(stdout, "Done! You entered %d values averaging %f.\n",                  total, acc / total);        } else {          fputs("There was an error, aborting!\n", stdout);        }        break;      } else if (res == 0) {        // Handle parse error.        fputs("Sorry, I did not understand. Try again.\n", stdout);        clearerr(stdin);        for (int r = 0; r != EOF && r != '\n'; r = fgetc(stdin)) {}      } else {        // Handle successful input.        acc += val;        ++total;      }    }  }  

Demo

Answer by Roddy for Change multiple if to a loop?


There's a way to calculate the average of a set of numbers without storing each one individually, either in an array, or in individually names variable (yuk.)

Just keep a running total, and divide it at the end.

Answer by Ian for Change multiple if to a loop?


What you need is simply a loop for your manyTest value, you do not need array at all!

#define _CRT_SECURE_NO_WARNINGS  #include   #include   #include     int main()  {      float manyTest = 0, gr = 0, avg = 0; //firstly, replace many gr with single gr, avg for averaging      int i; //just indexer        printf("How many tests you wanna average? (Minimum 1 Maximum 25)\n");      scanf(" %f", &manyTest);      if (manyTest <= 0) {          printf("The Minimum is 1!\n");      }      else if (manyTest > 25){          printf("The Maximum is 25!\n");      }      else {          printf("Write down your grades on those tests:\n");          for (i = 0; i < manyTest; ++i) {              scanf(" %f", &gr); //use gr every time              avg += gr; //then immediately add to avg!          }          printf("Your average grade is: %.2f\n", avg / manyTest); //notice the division by manyTest      }        system("pause");      return 0;  }  

Done!

Sample result:

enter image description here


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

  1 comment:

Popular Posts

Powered by Blogger.