Hey all, i know the board has a policy against posting course work but i am not asking for u guys to write my whole program for me. im just having trouble in a few areas. I will post the assignment write up in a message below just to give some background info tho.
Anyways here is what i have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define NUMROWS 10
#define NUMCOLS 10
#define TRUE 1
#define FALSE 0
#define N_MIN 0
#define N_MAX 1
struct PARAM{
float s; //slope of water
float z; //slope of channel side
float n; // coefficient of roughness
};
struct HEADING{
float initialvalue; //initial value of parameter
float stepsize; //step size for parameter
};
struct FLOW_RATE_TABLE{
char name[50]; //table name
struct PARAM param;
struct HEADING basewidth;
struct HEADING waterdepth;
float flowrates[NUMROWS][NUMCOLS];
};
struct FLOW_RATE_TABLE flowratetable;
void userinterface();
void addflowratetable();
bool setflowratetablename();
double computeflowrate(double params,double b,double y);
double readvaluegtzero();
int exists(char *fname);
int main () {
flowratetable.name=="/0";
userinterface();
}
void userinterface(){
int quit=0;
int option=0;
while (quit==0){
printf("\t1. Create a flow rate table\n"
"\t2. Load an existing flow rate table\n"
"\t3. Display currenty loaded flow rate table\n"
"\t4. Estimate water depth using flow rate table\n"
"\t5. Terminate\n\n");
if (flowratetable.name !="/0" ){
printf("Table %s Loaded", flowratetable.name);
}
else{
printf("No Table Loaded");
while (option!=5){
scanf("%i\n", &option);
if (option==1){
printf("Please enter an initial base width followed by the step size:");
scanf ("%f\n", &flowratetable.basewidth.initialvalue);
scanf ("%f\n", &flowratetable.basewidth.stepsize);
printf("Please enter an initial water depth, followed by the step size:");
scanf ("%f\n", &flowratetable.waterdepth.initialvalue);
scanf ("%f\n", &flowratetable.waterdepth.stepsize);
//for (i==0;i<=NUMCOLS; ++i){
// flowratetable.flowrates[i]
}
}
}}}
bool setflowratetablename(){
int abletocreate;
FILE *file;
int leaveloop=0;
while (leaveloop==0){
printf("Please enter a table name:\t");
scanf("%s\n", &flowratetable.name);
strcat(flowratetable.name, ".frt");
abletocreate=exists(flowratetable.name);
if (abletocreate==0){
file=fopen("flowratetable.name.dat", "w");
fclose(file);
return TRUE;
if (file == NULL) {
printf("I couldn't open the file for writing.\n");
exit(0);}
leaveloop=1;
}
if (abletocreate==1){
int overwrite;
printf("The file already exists. Would you like to overwrite it? (Yes=1/No=2)\n");
scanf("%i", &overwrite);
if (overwrite==1){
file=fopen("flowratetable.name.dat", "w");
fclose(file);
return TRUE;
if (file == NULL) {
printf("I couldn't open the file for writing.\n");
exit(0);}
leaveloop=1;
}
else{
leaveloop=0;}
}}
return FALSE;}
void addflowratetable(){
if (setflowratetablename()==TRUE){
flowratetable.param.s= readvaluegtzero();
flowratetable.param.z=readvaluegtzero();
int q=0;
while(q==0){
printf("Please give the coefficient of roughness:\n");
scanf("%d", &flowratetable.param.n);
if (flowratetable.param.n< N_MIN || flowratetable.param.n>N_MAX){
printf("Coefficient of roughness must lie within the range of %d and %d", N_MAX, N_MIN);}
else{
q=1;}
}
flowratetable.basewidth.initialvalue= readvaluegtzero();
flowratetable.basewidth.stepsize= readvaluegtzero();
flowratetable.waterdepth.initialvalue= readvaluegtzero();
flowratetable.waterdepth.stepsize= readvaluegtzero();
double y = flowratetable.waterdepth.initialvalue;
double b=0;
int rix=0, cix=0;
while (rix<NUMROWS){
cix=0;
b=flowratetable.basewidth.initialvalue;
while (cix< NUMCOLS){
flowratetable.flowrates[rix][cix]=computeflowrate(flowratetable.param, b, y);
b=b+flowratetable.basewidth.stepsize;
++cix;}
y=y+flowratetable.waterdepth.stepsize;
++rix;}
strcat(flowratetable.name,".frt");
char filename[50];
int flowname;
flowratetable.name *flowname;
filename=flowratetable.name;
FILE *fileptr;
fileptr=fopen("flowratetable.name.dat", "w");
if (fileptr == NULL) {
printf("I couldn't open the file for writing.\n");
exit(0);}
else{
fwrite(<#const void * #>, <#size_t #>, <#size_t #>, <#FILE * #>)
}
}}
double readvaluegtzero(){
int x=0;
double value=0;
while (x==0){
printf("Please enter a positive value:\t");
scanf("%d\n", &value);
if (value>0){
x=1;}
else{
printf("Invalid value\n");
x=0;
}
}
return value;
}
double computeflowrate(struct PARAM params, double b,double y){
}
int exists(char *fname)
{
FILE *file;
if (file = fopen(fname, "r"))
{
fclose(file);
return 1;
}
return 0;
}
I just had some questions about areas in which i could not get the program to work. NOTEL not all my functions are completed yet so they cause some errors but that will be solved when it is finished.
I am having difficulty figuring out how to write my structure flowratetable to a file. does any one know how to do this? and also how would i reload said file?
And for the function computeflowrate, where i will be doing the actual calculations, do u guys know how i would pass my flowrate.param structure to the fiunction?
thanks.
o and any other errors that u could help with will be appreciated. And i hope this isnt to long or in violation of anything. just looking for some help because my TA sucks/is unavailable and my partner wont work on the code at all.
thanks a lot everyone