Question:

I can't get right answer when second time looping?

by  |  earlier

0 LIKES UnLike

#include <stdio.h>

#include <math.h>

#define PI 3.1416

void main(void)

{

int choice,tanknum,hosenum,hose,q;

float area,volume,height,radius,length,width,t...

float totaltime2=0;

printf("\nFinding volume of different shapes of tanks and ");

printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~...

printf("\ntime taken to fill the tank with water\n");

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...

printf("\n\n How many tanks do you want to find? :");

scanf("%d",&tanknum);

for (q=1;q<=tanknum;q++)

{

printf("\n\nMenu of Choices\n");

printf("===============\n");

printf("1.Cylinder\n");

printf("2.Square tank\n");

printf("3.Rectangle tank\n");

printf("4.Quit\n\n");

printf(" Enter choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1:printf("\nEnter the radius and height of the cylinder in metres:");

scanf("%f %f",&radius,&height);

area=PI*pow(radius,2);

volume=PI*pow(radius,2)*height;

printf("\nArea of cylinder is %.2f square metres.\n",area);

printf("Volume of cylinder is %.2f cubic metres.\n",volume);

printf("\nHow many hoses do you want to use(must be more than 1):");

scanf("%d",&hosenum);

printf("\nEnter the 1 Hose time taken(in minutes):");

scanf("%f",&time1);

totaltime1=1.0/time1;

for(hose=2;hose<=hosenum;hose++)

{

printf("Enter the %d Hose time taken(in minutes):",hose);

scanf("%f",&time2);

totaltime2=totaltime2+(1.0/time2)...

}//end for

break;

case 2: printf("\nEnter the length and height in metres:");

scanf("%f %f",&length,&height);

area=length*length;

volume=pow(length,2)*height;

printf("\nArea of square tank is %.2f square metres.\n",area);

printf("Volume of square tank is %.2f cubic metres.\n\n",volume);

printf("\nHow many hoses do you want to use(must be more than 1):");

scanf("%d",&hosenum);

printf("\nEnter the 1 Hose time taken(in minutes):");

scanf("%f",&time1);

totaltime1=1.0/time1;

for(hose=2;hose<=hosenum;hose++)

{

printf("Enter the %d Hose time taken(in minutes):",hose);

scanf("%f",&time2);

totaltime2=totaltime2+(1.0/time2)...

}//end for

break;

case 3:printf("\nEnter length, width and height of rectanglular tank in m:");

scanf("%f %f %f",&length,&width,&height);

area=length*width;

volume=length*width*height;

printf("\nArea of rectangular tank is %.2f square metres.\n",area);

printf("Volume of rectangular tank is %.2f cubic metres.\n\n",volume);

printf("\nHow many hoses do you want to use(must be more than 1):");

scanf("%d",&hosenum);

printf("\nEnter the 1 Hose time taken(in minutes):");

scanf("%f",&time1);

totaltime1=1.0/time1;

for(hose=2;hose<=hosenum;hose++)

{

printf("Enter the %d Hose time taken(in minutes):",hose);

scanf("%f",&time2);

totaltime2=totaltime2+(1.0/time2)...

}//end for

break;

case 4:printf("\nBye-bye\n");

break;

default:printf("\nInvalid choice!!\n");

}//end switch

if(time1<=0 || time2<=0)

{

printf("\nTime taken cannot be negative or 0.\n");

}//end if

else

{

total=1.0/(totaltime1+totaltime2)...

printf("\nTime taken to fill a tank is %.2f minutes.\n",total);

}//end else

}//end for

}//end main

 Tags:

   Report

1 ANSWERS


  1. you initialize totaltime2 to 0 at the beginning of main() but not in the loop, you keep adding to it, so for the second and subsequent tanks, you will be adding the times from the previous tanks.

    you might try something like:

    totaltime1=1.0/time1;

    /* next line added */

    totaltime2 = 0;

    for(hose=2;hose&lt;=hosenum;hose+...

    {

    printf(&quot;Enter the %d Hose time taken(in minutes):&quot;,hose);

    scanf(&quot;%f&quot;,&amp;time2);

    totaltime2=totaltime2+(1.0/tim...

    }//end for

    BTW, if you include some white space Y!A won&#039;t truncate lines, it may wrap them, but at least you wont get lines like this:

    totaltime2=totaltime2+(1.0/tim...

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions