Skip to main content
Added a missing paren
Source Link
user1118321
  • 11.8k
  • 1
  • 19
  • 46
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code checks the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF) != 1) Handle_Error();
    
  6. Curious that code did not define f,g,h,k in alphabetically order.

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();  // why last?
    
  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  10. Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns

    // OP's
    scanf("%d", &inputK);
    
      if (inputF*inputH == a && ...
    
    // Auto formated
    scanf("%d", &inputK);
    
    if (inputF * inputH == a && ....
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code checks the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF != 1) Handle_Error();
    
  6. Curious that code did not define f,g,h,k in alphabetically order.

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();  // why last?
    
  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  10. Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns

    // OP's
    scanf("%d", &inputK);
    
      if (inputF*inputH == a && ...
    
    // Auto formated
    scanf("%d", &inputK);
    
    if (inputF * inputH == a && ....
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code checks the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF) != 1) Handle_Error();
    
  6. Curious that code did not define f,g,h,k in alphabetically order.

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();  // why last?
    
  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  10. Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns

    // OP's
    scanf("%d", &inputK);
    
      if (inputF*inputH == a && ...
    
    // Auto formated
    scanf("%d", &inputK);
    
    if (inputF * inputH == a && ....
    
added 292 characters in body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code checkchecks the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF != 1) Handle_Error();
    
  6. Curious that code did not define f,g,h,k in alphabetically order.

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();  // why last?
    
  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  10. Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns

    // OP's
    scanf("%d", &inputK);
    
      if (inputF*inputH == a && ...
    
    // Auto formated
    scanf("%d", &inputK);
    
    if (inputF * inputH == a && ....
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code check the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF!= 1) Handle_Error();
    
  6. Curious that code did define f,g,h,k in alphabetically order.

  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code checks the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF != 1) Handle_Error();
    
  6. Curious that code did not define f,g,h,k in alphabetically order.

    int f = nonZeroCoefficient();
    int h = nonZeroCoefficient();
    int k = nonZeroRand();
    int g = nonZeroRand();  // why last?
    
  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  10. Important tip for future coder efficiency (your efficiency). Do not manually format code as the below code implies. Use your coding environment's auto formatter (or get one that does that) that meets your group's coding style. Life is too short for such manual picayune tasks - like white-space concerns

    // OP's
    scanf("%d", &inputK);
    
      if (inputF*inputH == a && ...
    
    // Auto formated
    scanf("%d", &inputK);
    
    if (inputF * inputH == a && ....
    
added 292 characters in body
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code check the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF!= 1) Handle_Error();
    
  6. Curious that code did define f,g,h,k in alphabetically order.

  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code check the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF!= 1) Handle_Error();
    
  6. Curious that code did define f,g,h,k in alphabetically order.

  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  1. Standard C does not allow definition a function within another. Move definitions of functions like int ask() outside of main().

  2. The if ( n == 0 ){ n++; } favors the return value of 1- a bias. Alternative:

     // return [-level...-1, 1...level]
     int nonZeroCoefficient(void) {
       int n = rand()%(2*level);
       if (n % 2) {
         return n/2 + 1;
       }
       return -(n/2 + 1);
     }
    
  3. Likewise for nonZeroRand(). In this case, code could simply re-try to avoid the 1 bias.

    int nonZeroRand(void) {
      int n;
      do {
        int n=(rand()%randMax - randMax);
      } while (n == 0);
      return n;
    }
    
  4. stdout may not be buffered as expected. Use fflush() to insure output is seen before asking for input.

    printf("\nEnter f:");
    fflush(stdoout); // add
    scanf("%d", &inputF);
    
  5. Robust code check the return value of user input.

     // scanf("%d", &inputF);
     if (scanf("%d", &inputF!= 1) Handle_Error();
    
  6. Curious that code did define f,g,h,k in alphabetically order.

  7. Formating to the width of the review presentation is appreciated.

     }else{
       printf("\nSorry! your answer is not currect!\nthe currect answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \ntry again",f, g, h, k);
    
  8. ... versus (also check spelling)

     }else{
       printf("\nSorry! your answer is not correct!\n"
           "the correct answer is:\nf:%d\ng:%d\nh:%d\nk:%d\n \n"
           "try again",f, g, h, k);
    
  9. Consider declaring/defining variables when needed.

    // int a, b, c, level=1, randMax=level*5;
    // .... many lines later 
    // a = f*h;
    // b = (f*k + g*h);
    // c = g*k;
    
    int a = f*h;
    int b = (f*k + g*h);
    int c = g*k;
    
Source Link
chux
  • 33.6k
  • 2
  • 38
  • 92
Loading