Stuck in 2 loops
-
By "loops" do you mean "collections"? Or "arrays"? Or something like that? Can you do something like
if ( ( collection1.count == 0 ) && ( collection2.count == 0 ) ) ...
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:
- Voltage at hour 3 was 189.6 volts (difference of 23.6 volts).
- Voltage at hour 5 was 235.1 volts (difference of 21.9 volts).
- 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.
-
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:
- Voltage at hour 3 was 189.6 volts (difference of 23.6 volts).
- Voltage at hour 5 was 235.1 volts (difference of 21.9 volts).
- 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.
-
#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); }
}
}
-
#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); }
}
}
I did not see any issue in above code. But what condition you are checking to show the comment - No Issues in voltage?
-
I did not see any issue in above code. But what condition you are checking to show the comment - No Issues in voltage?
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?
-
#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); }
}
}
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");
} -
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");
}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
-
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
-
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?
-
And you have some code ?
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
-
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