
/* Program that requests from the user characteristics of the channel and display in table form
how the average velocity of the water varies with its depth in the channel */

#include<stdio.h>
#include<math.h>
#define max 25 //symbolic for 25


/*defining the structure channel that contains variables*/
struct channel
{
    char name[50]; //for storing name
    double n,slope,width,maxDepth; //for storing other variables
};

double getPositiveValue() //to get a positive value for variables
{
    double a;
    scanf("%lf",&a); //taking input from user
    while(a<0.0) //taking the input until a>=0
        {
            printf("Enter a positive value: ");
        }
    return a; //returning this value
}

/*for computing velocity*/
void computeVelocity(double depth[max],double velocity[max],struct channel c)
{
    int i; //loop variable
    double con; //for storing the constant part of equation (sqrt(S)/n)
    con=sqrt(c.slope)/c.n; //calculating con
    for(i=0;i<25;i++)
        {
            velocity[i]=(c.width/depth[i])/(c.width+2*depth[i]); //(B/H)/(B+2H)
            velocity[i]=pow(velocity[i],0.66667); //raising this to the power 2/3
            velocity[i]*=con; //multiplying the constant part
        }
}

/*for displaying the data and velocities*/
void displayTable(struct channel c)
{ //displaying the data
    printf("\nChannel data for %s",c.name);
    printf(" Coefficient for roughness: %0.4lf\n",c.n);
    printf(" Slope: %0.5lf\n",c.slope);
    printf(" Width: %0.2lf\n",c.width);
    printf(" Maximum depth: %0.2lf\n",c.maxDepth);
    printf("\tDepth\t\tAverage velocity\n");
    printf("-------------------------------------------\n");
    double depth[max],velocity[max]; //for storing various depths and their velocities
    int i; //loop variable
    for(i=1;i<=25;i++) //calculating the various depths
        {
            depth[i-1]=(i*c.maxDepth)/max;
        }
    computeVelocity(depth,velocity,c); //computing the various velocities
    for(i=0;i<25;i++) //displaying the depth and their corresponding velocities
        {
            printf("\t%0.2lf\t\t%0.4lf\n",depth[i],velocity[i]);
        }
    }

int main()
{
    struct channel c; //declaring a channel type variable
    printf("Give the name of the channel: ");
    fgets(c.name,50,stdin); //getting the name from user
    printf("Give the coefficient of roughness: ");
    c.n=getPositiveValue(); //getting coefficient
    printf("Give the slope: ");
    c.slope=getPositiveValue(); //getting slope
    printf("Give the channel width: ");
    c.width=getPositiveValue(); //getting width
    printf("Give the maximum depth to the channel: ");
    c.maxDepth=getPositiveValue(); //getting maxDepth
    displayTable(c); //displaying the table
    return 0;
}
