Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

const int N=10; struct PARAM { double s; double z; double n; }; struct HEADING {

ID: 3615570 • Letter: C

Question

const int N=10;

struct PARAM
{
double s;
double z;
double n;
};

struct HEADING
{
double initialValue;
double step;
};

struct FLOW_RATE_TABLE
{
char     name[20];
PARAM     params;
HEADING   baseWidth;
HEADING   waterDepth;
double    flowRates[N][N];
}flowRateTable;


void menu();
int addFlowRateTable();
double readValueGTzero();
double computeFlowRate(PARAM, double, double );
int writeFrt();
int setFlowRateTableName();
int loadFlowRateTable();
void displayFlowRateTable();
double getRowIndex(double,int);
double getColIndex(double);
void estimateWaterDepth();


int main()
{
for(;;)menu();
return 0;
}


void menu()
{
char ch;

clrscr();

if(flowRateTable.name==NULL)
strcpy(flowRateTable.name,"");


printf("1. Create a flow rate table ");
printf("2. Load an existing flow rate table ");
printf("3. Display currently loaded flow rate table ");
printf("4. Estimate water depth using flow rate table ");
printf("5. Terminate ");
printf("Please, make a selection(1-5) ");


if(strcmp(flowRateTable.name,"")==0)
{
printf("No table loaded ");
}
else printf("Table %s loaded ",flowRateTable.name);

ch=getch();

switch(ch)
{
case '1':addFlowRateTable();break;
case '2':loadFlowRateTable();break;
case '3':displayFlowRateTable();break;
case '4':estimateWaterDepth();break;
case '5':exit(0);
}


}


int addFlowRateTable()
{
FLOW_RATE_TABLE t;
int i,j;
double y,b;
char temp[10];

if(setFlowRateTableName()==0) return 0;


printf("Enter the slope of the water: ");
flowRateTable.params.s= readValueGTzero();
printf("Enter the slope of the channel sides: ");
flowRateTable.params.z= readValueGTzero();
printf("Please give the coefficient of roughness: ");
do
{
scanf("%s",&temp);
flowRateTable.params.n=atof(temp);


if(flowRateTable.params.n<0.001||flowRateTable.params.n>0.1)
{
   printf("Coefficient of roughness must lie in the range of 0.001 and 0.1 ");
   printf("Please give the coefficient of roughness: ");
}
}while(flowRateTable.params.n<0.001||flowRateTable.params.n>0.1);

printf("Enter initial base width: ");
flowRateTable.baseWidth.initialValue = readValueGTzero();
printf("Enter step size: ");
flowRateTable.baseWidth.step = readValueGTzero();
printf("Enter initial water depth: ");
flowRateTable.waterDepth.initialValue = readValueGTzero();
printf("Enter step size: ");
flowRateTable.waterDepth.step = readValueGTzero();

y=flowRateTable.waterDepth.initialValue;
b=flowRateTable.baseWidth.initialValue;


for(i=0; i<N; i++)
{
   for(j=0; j<N; j++)
   {
    flowRateTable.flowRates[i][j]=computeFlowRate(flowRateTable.params,b,y);
    b+=flowRateTable.baseWidth.step;
   }
   y+=flowRateTable.waterDepth.step;
}

writeFrt();

printf("Table created ");
getch();
return 1;
}

double readValueGTzero()
{
double val=0;
char temp[10];
scanf("%s",&temp);
val = atof(temp);

while(val<=0)
{
printf("The value have to be greater than zero. Try again please! ");
scanf("%s",&temp);
val = atof(temp);
}
return val;
}


double computeFlowRate(PARAM params, double b, double y)
{
double area,perimeter,rh,q;

area = y*b+y*2.0*params.z;
perimeter = b+y*(1.0+2.0*params.z);
rh=area/perimeter;

q = (1/params.n)*area*rh*2.0/3.0*params.s*0.5;

return q;

}


int writeFrt()
{

char fname[20],t[7], *ext = ".frt";
int i,j;
FILE *stream;

strcpy(fname,flowRateTable.name);
strcat(fname,ext);


if ((stream = fopen(fname, "w"))!= NULL)
{

   fwrite(&flowRateTable.name,sizeof(flowRateTable.name),1,stream);
   fwrite(&flowRateTable.params,sizeof(flowRateTable.params),1,stream);
   fwrite(&flowRateTable.baseWidth,sizeof(flowRateTable.baseWidth),1,stream);
   fwrite(&flowRateTable.waterDepth,sizeof(flowRateTable.waterDepth),1,stream);


   for(i=0;i<N;i++)
   for(j=0;j<N;j++)
   {
    fprintf(stream,"%.8e ",flowRateTable.flowRates[i][j]);

   }
   fclose(stream);
   }

   return 1;


}


int setFlowRateTableName()
{
char fname[20],tn[20],ch;
char *ext = ".frt";
FILE *stream;

printf("Enter the filename(without file extension): ");
scanf("%s",&fname);
strcpy(tn,fname);
strcat(fname, ext);

if ((stream = fopen(fname, "r"))!= NULL)
{
   printf("Such file already exists. Would you like to overwrite it (y/n)? ");

   for(;;)
   {
   ch = getch();
   if(ch=='y'||ch=='n') break;
   }


   if(ch=='n') return 0;

}
strcpy(flowRateTable.name,tn);
fclose(stream);
return 1;

}

int loadFlowRateTable()
{

char fname[20],t[14],tt[14], *ext = ".frt",ch,p;
int i,j,k;
FILE *stream;

l:
printf("Enter the filename(without file extension): ");
scanf("%s",&fname);
strcat(fname,ext);
//ifstream istr(fname,ios::in);
if ((stream = fopen(fname, "r"))== NULL)
{
    printf("Error opening file or such file does not exist ");
    printf("Would you like to try again(y/n)? ");
      for(;;)
   {
   ch = getch();
   if(ch=='y'||ch=='n') break;
 

Explanation / Answer

The only problem I had compiling it was the use of clrscr(), which doesn't work on all compilers (I use wxdevcpp). I replaced it with system("CLS") and it compiled just fine.