Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Stuck in 2 loops

Stuck in 2 loops

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    We cannot help unless you show the code that is causing the problems.

    U Offline
    U Offline
    User 12465479
    wrote on last edited by
    #5

    #include <stdio.h>
    #include <math.h>
    #include <string.h>

    int i;
    float volt[6];
    float avg, avg10, avg15, total, a, b;

    int main () {
    

    total= 0 ;
    avg = 0;
    printf("Enter 6 Volts of Machine\n");

    for ( i=0; i<6; i++) {
    printf("Type %d. volt", i+1);
    scanf("%f",&volt[i]);

    total = total + volt[i];
    }
    avg = total/6;
    avg10 = (avg * 10) / 100;
    avg15 = (avg * 15) / 100;
    printf("------------------------------------------\n");
    printf("The machine Avarage Voltage is %.2f\n", avg);
    printf("The Machine Avarage is%.2f\n", avg10);
    printf("The Machine 15 Avarage is%.2f\n\n\n", avg15);

     for (i=0;i<6;i++) {
      a = fabs(volt\[i\] - avg);
    
      if( a > avg10 ) {
     printf("\\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\\n\\n", i+1, volt\[i\], a);
       }
    

    }

     for (i=0; i<5; i++) {
    
       b = fabs(volt\[i+1\] - volt\[i\]);
       if( b > avg15) {
     printf("\\nVoltage change from hour %d to hour %d was %.2f\\n\\n", i+1, i+2, b);
       }
    

    }

    }

    P L 2 Replies Last reply
    0
    • U User 12465479

      #include <stdio.h>
      #include <math.h>
      #include <string.h>

      int i;
      float volt[6];
      float avg, avg10, avg15, total, a, b;

      int main () {
      

      total= 0 ;
      avg = 0;
      printf("Enter 6 Volts of Machine\n");

      for ( i=0; i<6; i++) {
      printf("Type %d. volt", i+1);
      scanf("%f",&volt[i]);

      total = total + volt[i];
      }
      avg = total/6;
      avg10 = (avg * 10) / 100;
      avg15 = (avg * 15) / 100;
      printf("------------------------------------------\n");
      printf("The machine Avarage Voltage is %.2f\n", avg);
      printf("The Machine Avarage is%.2f\n", avg10);
      printf("The Machine 15 Avarage is%.2f\n\n\n", avg15);

       for (i=0;i<6;i++) {
        a = fabs(volt\[i\] - avg);
      
        if( a > avg10 ) {
       printf("\\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\\n\\n", i+1, volt\[i\], a);
         }
      

      }

       for (i=0; i<5; i++) {
      
         b = fabs(volt\[i+1\] - volt\[i\]);
         if( b > avg15) {
       printf("\\nVoltage change from hour %d to hour %d was %.2f\\n\\n", i+1, i+2, b);
         }
      

      }

      }

      P Offline
      P Offline
      Parthiban Appuswamy
      wrote on last edited by
      #6

      I did not see any issue in above code. But what condition you are checking to show the comment - No Issues in voltage?

      U 1 Reply Last reply
      0
      • P Parthiban Appuswamy

        I did not see any issue in above code. But what condition you are checking to show the comment - No Issues in voltage?

        U Offline
        U Offline
        User 12465479
        wrote on last edited by
        #7

        I need if not any voltage problem to print "No problems were encountered. but just one result. How can I do? Code is right not any problem in code just I need information how can add this print?

        1 Reply Last reply
        0
        • U User 12465479

          #include <stdio.h>
          #include <math.h>
          #include <string.h>

          int i;
          float volt[6];
          float avg, avg10, avg15, total, a, b;

          int main () {
          

          total= 0 ;
          avg = 0;
          printf("Enter 6 Volts of Machine\n");

          for ( i=0; i<6; i++) {
          printf("Type %d. volt", i+1);
          scanf("%f",&volt[i]);

          total = total + volt[i];
          }
          avg = total/6;
          avg10 = (avg * 10) / 100;
          avg15 = (avg * 15) / 100;
          printf("------------------------------------------\n");
          printf("The machine Avarage Voltage is %.2f\n", avg);
          printf("The Machine Avarage is%.2f\n", avg10);
          printf("The Machine 15 Avarage is%.2f\n\n\n", avg15);

           for (i=0;i<6;i++) {
            a = fabs(volt\[i\] - avg);
          
            if( a > avg10 ) {
           printf("\\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\\n\\n", i+1, volt\[i\], a);
             }
          

          }

           for (i=0; i<5; i++) {
          
             b = fabs(volt\[i+1\] - volt\[i\]);
             if( b > avg15) {
           printf("\\nVoltage change from hour %d to hour %d was %.2f\\n\\n", i+1, i+2, b);
             }
          

          }

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          You just need to add a boolean value to indicate whether any problems were found in either of the loops. So your code would have the following additions:

          // add the following variable at the beginning of the program:
          int voltageProblem = 0;

          // add the following in each for loop, inside the test for voltage out of limits
          // loop1 code shown here:
          for (i=0;i<6;i++) {
          a = fabs(volt[i] - avg);
          if( a > avg10 ) {
          printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a);
          voltageProblem = 1;
          }
          }

          // after the two loops add the following:
          if (voltageProblem == 0) {
          printf("No problems were encountered.\n");
          }

          U 1 Reply Last reply
          0
          • L Lost User

            You just need to add a boolean value to indicate whether any problems were found in either of the loops. So your code would have the following additions:

            // add the following variable at the beginning of the program:
            int voltageProblem = 0;

            // add the following in each for loop, inside the test for voltage out of limits
            // loop1 code shown here:
            for (i=0;i<6;i++) {
            a = fabs(volt[i] - avg);
            if( a > avg10 ) {
            printf("\nVoltage at hour %d was %.2f volts (diffrence of %.2f volts)\n\n", i+1, volt[i], a);
            voltageProblem = 1;
            }
            }

            // after the two loops add the following:
            if (voltageProblem == 0) {
            printf("No problems were encountered.\n");
            }

            U Offline
            U Offline
            User 12465479
            wrote on last edited by
            #9

            Richard MacCutchan Thank you so much, you are amazing! I tried but wrong first under curly brackets and second I tried voltageproblem++ . . if (voltageproblem==0) . . anyway Just thank you so much. Happy coding :-D

            L 1 Reply Last reply
            0
            • U User 12465479

              Richard MacCutchan Thank you so much, you are amazing! I tried but wrong first under curly brackets and second I tried voltageproblem++ . . if (voltageproblem==0) . . anyway Just thank you so much. Happy coding :-D

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #10

              Happy to help. Good luck in the future.

              1 Reply Last reply
              0
              • U User 12465479

                Hey Guys, I have one Project which has inside 2 different loops if loops are not empty display results but both of them are empty must be display just one result like as "Not any Problem" How can I do? and if string how can i use?

                P Offline
                P Offline
                Patrice T
                wrote on last edited by
                #11

                And you have some code ?

                Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                L 1 Reply Last reply
                0
                • P Patrice T

                  And you have some code ?

                  Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #12

                  Read the thread messages.

                  P 1 Reply Last reply
                  0
                  • L Lost User

                    Read the thread messages.

                    P Offline
                    P Offline
                    Patrice T
                    wrote on last edited by
                    #13

                    you should have used "Edit" in first message. Everyone will read your first message, but it is unlikely that everyone will read discussion when not involved.

                    Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                    L 1 Reply Last reply
                    0
                    • P Patrice T

                      you should have used "Edit" in first message. Everyone will read your first message, but it is unlikely that everyone will read discussion when not involved.

                      Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #14

                      Reading the messages first helps to decide whether there is any point in posting a response. Something that I generally do, especially when it is obvious that the thread has a conversation going between OP and another CP member.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups