Stuck in 2 loops
-
#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