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.
  • U User 12465479

    Hey thank you for answer, I shared question originally and also how can identify your collection1.count and 2. yes should be like this. Thank you

    Voltage readings are obtained from an electrical substation once every hour for six hours (so there are six
    readings). Write a C program to perform the following checks on the substation:
    a) display all voltages that differ from the average by more than 10% of the average.
    b) display all pairs of consecutive hours where the change from the voltage at one hour
    to the next is greater than 15% of the average.

    Example 1

    Enter 6 voltages: 210.1 223.2 189.6 206.2 235.1 215.0
    The average is 213.2 volts.
    10% = 21.3 volts.
    15% = 32.0 volts.

    The following problems occurred:

    1. Voltage at hour 3 was 189.6 volts (difference of 23.6 volts).
    2. Voltage at hour 5 was 235.1 volts (difference of 21.9 volts).
    3. Voltage change from hour 2 to hour 3 was 33.6 volts.

    Example 2

    Enter 6 voltages: 233.1 201.0 221.5 240.2 222.7 208.1
    The average is 221.1 volts.
    10% = 22.1 volts.
    15% = 33.2 volts.

    No problems were encountered.

    I created project but when I put any think in for loops coming 11 "No problems were encountered" or put under for loops this result coming sometimes with voltage results.

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

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

    U 1 Reply Last reply
    0
    • 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