Thread: Need Help

  1. #1
    Registered User
    Join Date
    Nov 2010

    Exclamation Need Help

    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:

    #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 () {"/0";
    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 ( !="/0" ){
    		printf("Table %s Loaded",;
    		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", &;
    		strcat(, ".frt");
    		if (abletocreate==0){
    			file=fopen("", "w");
    			return TRUE;
    			if (file == NULL) {
    				printf("I couldn't open the file for writing.\n");
    		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("", "w");
    				return TRUE;
    				if (file == NULL) {
    					printf("I couldn't open the file for writing.\n");
    return FALSE;}
    void addflowratetable(){
    	if (setflowratetablename()==TRUE){
    		flowratetable.param.s= readvaluegtzero();
    		int 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);}
    		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){
    			while (cix< NUMCOLS){
    				flowratetable.flowrates[rix][cix]=computeflowrate(flowratetable.param, b, y);
    		char filename[50];
    		int flowname; *flowname;;
    		FILE *fileptr;
    		fileptr=fopen("", "w");
    		if (fileptr == NULL) {
    			printf("I couldn't open the file for writing.\n");
    			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){
    		printf("Invalid value\n");
    	return value;
    double computeflowrate(struct PARAM params, double b,double y){
    int exists(char *fname)
    	FILE *file;
    	if (file = fopen(fname, "r"))
    		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?

    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
    Last edited by Salem; 12-04-2010 at 11:53 PM. Reason: Folded long lines

  2. #2
    Registered User
    Join Date
    Nov 2010
    Create a software solution to support analysis of flow rate values (discharge) for trapezoidal open channels according to following guidelines (see the last two pages for background information about the problem):
    1. Provides the users with one of four functions: (1) create a flow rate matrix for a series of depth values and bottom width values with given values for Manning coefficient of roughness n, slope of water surface S, and slope of the trapezoid side z; the flow rate table that consist of the matrix and the values of all parameters used to create the matrix are stored in a file; (2) Load a flow rate table from an existing file (3) print the currently loaded table; and (5) using the loaded table estimate the water depth given a base width and flow rate. The following is an example of the initial menu presented:

    1. Create a flow rate table
    2. Load an existing flow rate table
    3. Display currently loaded flow rate table
    4. Estimate water depth using flow rate table
    5. Terminate

    Please make a selection (1-5):

    2. The following pseudo-code describes a structure to be used to contain the information about a selected table. Note that two structures are defined for creating members of the flow rate table structure. Use a global structure variable for storing flow rate table data.

    Define PARAM structure that contains:
    s (slope of the water, real value)
    z (slope of channel side, real value)
    n (coefficient of roughness, real value)
    Define HEADING structure that contains
    initialValue (initial value of the parameter)
    stepSize (step size for the parameter
    Define FLOW_RATE_TABLE structure that contains:
    name (string that contains the table name)
    params (a PARAM structure)
    baseWidth (a HEADING structure)
    waterDepth (a HEADING structure)
    flowRates is a matrix of real values of size NUMROWS by NUMCOLS
    (rows – contains q values for given depth)
    (columns – contains q values for given base width)
    Declare flowRateTable global structure variable as a FLOW_RATE_TABLE structure

    3. The flow rate matrix is a 10 X 10 matrix (i.e. NUMROWS and NUMCOLS are both equal to 10). The user is prompted for an initial base width value and step size (store in the baseWidth HEADING structure) and for an initial water depth value and step size (stored in the waterDepth HEADING structure). Thus the program computes 10 values for base width starting at the initial value and incrementing the base widths using the step size; e.g. with a base width of 1.0 and step size of 0.5, the 10 base width values used are 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5.

    4. When creating a flow rate table (i.e. updating the global flow rate table structure), prompt the user for a table name. Define a file name by adding the extension “.frt” to the flow rate table name. Before updating the flow rate table structure, check that the file can be created (in the current working directory). If the file already exists, prompt the user to replace the file and continue the creation process only if the user accepts to replace the current file. Use a data file (.dat) to store the contents of the flow rate table structure.

    5. Loading a flow rate table allows the user to select load a flow rate table from a flow rate table file. The contents of the corresponding file are loaded into the flow rate structure.

    6. In the case of estimating depth, the user must provide a base width that occurs in the table. The flow rate can fall between values and linear interpolation should be used to estimate the depth. If the base width is not part of the table or the flow rate falls outside the range of the table, an error message shall be printed.

    7. Demonstrate structured programming by using a number of functions, that is, break down the software into smaller problems to be solved by separate functions (note that algorithms should be provided for each function). It will be much easier to develop the software than trying to create large functions. For example:

    8. Do not use global variables other than the single structure variable described above.

    9. Use the metric system.

    10. In Step 2, when describing input/output, you will be required to provide a textual description since block diagrams will not be sufficient.

    11. Test input data as follows:
    1. The following values must be greater than zero: slope of water surface S, and slope of the trapezoid side z, initial base width value, initial water depth value, step sizes.
    2. The Maning coefficient must fall between the values of 0.001 and 0.1.
    3. Check for invalid selection values entered in response to the menus or other prompts (e.g. prompt to request for overwriting a file).

    12. Develop the pseudo code for the missing functions, and translate all into C code.

    Main program:
    Copy the empty string to

    Print “1) Create a flow rate table”
    Print “2) Load an existing flow rate table”
    Print “3) Display the currently loaded flow rate table”
    Print “4) Estimate water depth using flow rate table”
    Print “5) Terminate”
    If not empty string
    Print “(Table “,, “loaded)”
    Print “(No table loaded)”
    Print “Please make a selection (1-5): “
    Read value into selection
    If selection equals 1
    Else if selection equals 2
    Else if selection equals 3
    Else if selection equals 4
    Else if selection equals 5
    Print “Terminating”
    while selection less than 1 or greater than 5
    while selection not equal to 5

    if setFlowRateTableName() equals OK
    Assign readValueGTzero(“slope of the water”) to flowRateTable.params.s
    Assign readValueGTzero(“slope of the channel sides”) to flowRateTable.params.z

    Print “Please give the coefficient of roughness: “
    Read value into flowRateTable.params.n
    If flowRateTable.params.n less than N_MIN or greater than N_MAX
    Print “Coefficient of roughness must lie in the range of”, N_MIN, “ and “, N_MAX
    While flowRateTable.params.n less than N_MIN or greater than N_MAX

    Assign readValueGTzero(“initial base width”) to flowRateTable.baseWidth.initialValue
    Assign readValueGTzero(“step size”) to flowRateTable.baseWidth.stepSize
    Assign readValueGTzero(“initial water depth”) to flowRateTable.waterDepth.initialValue
    Assign readValueGTzero(“step size”) to flowRateTable.waterDepth.stepSize
    ( updates the matrix )
    Assign flowRatetable.waterDepth.initialValue to y
    Assign 0 to rix

    Repeat while rix is less than NUMROWS
    Assign flowRateTable.baseWidth.initialValue to b
    Assign 0 to cix
    Repeat while cix is less than NUMCOLS
    Assign computeFlowRate(flowRateTable.params, b, y) to flowRateTable.flowRates[rix][cix]
    Assign b+flowRateTable.baseWidth.stepSize to b
    Increment cix
    Assign y+flowRateTable.waterDepth.stepSize to y
    Increment rix
    (Store to the file)
    Append “.frt” to and assign to fileName
    If can open fileName for writing as filePtr
    Write flowRateTable into file filePtr
    Close file filePtr
    Print “Table “,, “ created”


    /* This function should prompt the user for the file name, checks if the file exists or not, and give the user the choice to overwrite it, and copy the table name into the name field of the flowRateTable structure. This function returns a Boolean value*/

    /* This function should prompt the user for a positive value, and keep prompting until the value is positive. This function returns that positive value */

    computeFlowRate(params, b, y)
    /* This function should compute the flow rate and returns its value */

    /* This function prompts the user for the table name, opens the file for reading, and copy the file contents into the FlowRateTable structure */

    /* This function displays the flowRateTable on the output screen in a tabular format */

    /* This function should prompt the user for the base width and the flow rate, validate the user input for both values, and compute the estimated water depth*/

    /* this function returns the column index with the closest value to the qGiven value*/

    getRowIndex(cix, qGiven)
    /* this function returns the row index with the closest value to the qGiven value*/

    Background Information
    1. Manning formula
    1.1.1.The following description is based on Wikipedia source for Manning Formula.
    The Manning formula, known also as the Gauckler-Strickler formula in Europe, is an empirical formula for open channel flow, or flow driven by gravity. It was developed by the Irish engineer Robert Manning. For more than a hundred years, this formula lacked a theoretical derivation. Recently this formula was derived theoretically ([1],[2]) using the phenomenological theory of turbulence.
    The Manning formula states:

    V is the cross-sectional average velocity (ft/s, m/s)
    k is a conversion constant equal to 1.486 for U.S. customary units or 1.0 for SI units
    n is the Manning coefficient of roughness (independent of units)
    Rh is the hydraulic radius (ft, m)
    S is the slope of the water surface or the linear hydraulic head loss (ft/ft, m/m) (S = hf / L)
    The discharge formula, Q = AV, can be used to manipulate Manning's equation by substitution for V. Solving for Q then allows the calculation of the volumetric flow rate (discharge in ft3/s or m3/s) without knowing the limiting or actual flow velocity. The Manning formula becomes:

    The Manning Formula is typically used to estimate flow in open channel situations where it is not practical to construct a weir or flume to measure flow with greater accuracy. Error rates of +/- 20% are common using the Manning Formula while error rates within +/- 5% are possible with properly constructed weirs or flumes.
    1.1.Manning coefficient of roughness
    The Manning coefficient of roughness, often denoted as n, is an empirically derived coefficient, which is dependent on many factors, including river-bottom roughness and sinuosity. Often the best method is to use photographs of river channels where n has been determined using Manning's formula.
    Values vary greatly in natural stream channels and will even vary in a given reach of channel with different stages of flow. Most research shows that N will decrease with stage, at least up to bank-full. Overbank N values for a given reach will vary greatly depending on the time of year and the velocity of flow. Summer vegetation will typically have a significantly higher N value due to leaves and seasonal vegetation. High velocity flows will cause some vegetation grasses and forbs) to lay flat, where a lower velocity of flow through the same vegetation will not.
    There are some typical values of streambeds for this coefficient:

    (source: Manning roughness coefficient)
    1.2.Hydraulic radius
    The hydraulic radius is defined as:

    Rh is the hydraulic radius (m)
    A is the cross sectional area of flow (m2)
    P is wetted perimeter (m)
    The hydraulic radius is not half the hydraulic diameter, despite what the similarity in the names may suggest. It is a function of the shape of the pipe, channel, or river in which the water is flowing. In wide rectangular channels, the hydraulic radius is approximated by the flow depth.
    1.3.Examples for Obtaining A and P (Source: )

    Trapezoid Channel

    Dimensions of the channel: Length of the base = b, Water depth = y, Slope of side = z
    Values required to compute Hydraulic Radius Rh:
    Cross-sectional area of flow: , Wetted perimeter:
    Circular Channel

    Dimensions of the channel: Angles α and θ as shown, Water depth = y, Diameter of circle = D
    Values required to compute Hydraulic Radius Rh:
    Cross-sectional area of flow: , Wetted perimeter:
    α = 2(π – θ)
    for y > D/2,
    for y ≤ D/2,

  3. #3
    Join Date
    Aug 2010
    Ontario Canada
    Ok, I'll take a pass at the disk file question...

    Writing structs, instead of text means opening the file in Binary mode. You can read and write sequentially like in "r" or "w" but binary mode doesn't add carriage returns which would cause positional displacement of your structs in the file. But binary files also offer you the chance to do random access by seeking to some multiple of the struct's size before reading or writing, this can be considerably faster than sequential I/O.

    For the question of passing the struct into a function... Just hand it over...

    // method 1 by value
    void MyFunction(MyStruct Test, Int x)
         // now use dots to access elements
         Test.Age += x;
    // call it as
    MyFunction(StructVar, q);
    // method 2 by pointer
    void MyFunction(MyStruct* Test , int x)
         // now use -> to access elements
        Test->Age += x;
    // call this one as
    MyFunction(&StructVar, q);
    I hope that helps you with a cople of the problems...

  4. #4
    Registered User
    Join Date
    Nov 2010
    ok thanks. do do i have to use the defstruct when declaring my structures? it is coming up for me as "error: conflicting types for 'cmputeflowrate'

  5. #5
    Join Date
    Aug 2010
    Ontario Canada
    Quote Originally Posted by fourseventwo View Post
    ok thanks. do do i have to use the defstruct when declaring my structures? it is coming up for me as "error: conflicting types for 'cmputeflowrate'
    It may not be necessary to typedef it but I would... It makes the syntax a bit simpler and there's no penalty in space or speed if you do.

    For the conflict with computeflowrate...
    //forward declaration...
    double computeflowrate(double params,double b,double y);
    // actual definition...
    double computeflowrate(struct PARAM params, double b,double y){
    // oops...
    This is one of the reasons I disfavour the programming style with declarations above main and everything else below it... too many chances for this kind of screw up. My best suggestion would be to use the more traditional style and put your functions above main... no forwards needed.

Popular pages Recent additions subscribe to a feed