I’m making a program to calculate flowrate but everytime you enter in the variables when the program is excecuted, it returns the wrong answer. I got the formula for calculating flow rate from this website “http://www.flowmeterdirectory.com/flowcalculator.php”, i’m using it as a a reference to check my program’s results but they never work.
#include <stdio.h>
#include <math.h>
#include "PI.h"
int flowRateFormula(int,int); //Finally, finds out the flow rate
int square(int);
int square(int x)
{
x=pow(x,2);
return x;
}
int flowRateFormula(int pipeDiameter,int velocity)
{
int integer3=0.25*(PI*square(pipeDiameter)*velocity);
return integer3;
}
int main()
{
int flowRate;
int velocity;
int pipeDiameter;
printf("Enter velocity of liquid(in./s):");
scanf("%d",&velocity);
printf("Velocity=%d in/s.\n",velocity);
printf("Enter pipe diameter(inches):");
scanf("%d",&pipeDiameter);
printf("Pipe Diameter=%d inches.\n",pipeDiameter);
printf("Applying formula for Flow Rate.........\n");
flowRate=flowRateFormula(pipeDiameter,velocity);
printf("Pipe Diameter=%d inches.\n",pipeDiameter);
printf("Velocity of liquid=%d in/s.\n",velocity);
printf("Thus, the flow rate=%d ft/s.\n",flowRate);
return 0;
}
It’s most likely because of the integer cast. Depending on your inputs, the results can be close and appear to have the same result because of rounding during the integer cast. Use double in your function instead.