Thread: Please help me I need help

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Please help me I need help

    Can you please make this programm work and make with C language not C++.
    Also,I want the main fuction to be int not of float not void.
    this is the programme:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <iomanip.h>
    #include <math.h>
    #include <fstream.h>
    
    
    
    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();
    
    
    void main()
    {
      for(;;)menu();
    }
    
    
    void menu()
    {
     char ch;
    
     clrscr();
    
     if(flowRateTable.name==NULL)
     strcpy(flowRateTable.name,"");
    
    
     cout<<"1. Create a flow rate table\n";
     cout<<"2. Load an existing flow rate table\n";
     cout<<"3. Display currently loaded flow rate table\n";
     cout<<"4. Estimate water depth using flow rate table\n";
     cout<<"5. Terminate\n";
     cout<<"Please, make a selection(1-5)\n\n\n";
    
    
     if(strcmp(flowRateTable.name,"")==0)
     {
     cout<<"No table loaded\n";
     }
     else cout<<"Table "<<flowRateTable.name<<" loaded\n";
    
     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;
    
     if(setFlowRateTableName()==0) return 0;
    
    
     cout<<"Enter the slope of the water:\n";
     flowRateTable.params.s= readValueGTzero();
     cout<<"Enter the slope of the channel sides:\n";
     flowRateTable.params.z= readValueGTzero();
     cout<<"Please give the coefficient of roughness:\n";
     do
     {
      cin>>flowRateTable.params.n;
      if(flowRateTable.params.n<0.001||flowRateTable.params.n>0.1)
      {
       cout<<"Coefficient of roughness must lie in the range of 0.001 and 0.1\n";
       cout<<"Please give the coefficient of roughness:\n";
      }
     }while(flowRateTable.params.n<0.001||flowRateTable.params.n>0.1);
    
     cout<<"Enter initial base width:\n";
     flowRateTable.baseWidth.initialValue = readValueGTzero();
     cout<<"Enter step size:\n";
     flowRateTable.baseWidth.step = readValueGTzero();
      cout<<"Enter initial water depth:\n";
     flowRateTable.waterDepth.initialValue = readValueGTzero();
     cout<<"Enter step size:\n";
     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();
    
     cout<<"Table created\n";
     getch();
     return 1;
    }
    
    double readValueGTzero()
    {
     double val=0;
     cin>>val;
    
     while(val<=0)
     {
      cout<<"The value have to be greater than zero. Try again please!\n";
      cin>>val;
     }
     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;
    
      strcpy(fname,flowRateTable.name);
      strcat(fname,ext);
    
       ofstream ostr(fname,ios::out);
    
       if(ostr)
       {
    
       ostr<<flowRateTable.name<<" "<<flowRateTable.params.s<<" ";
       ostr<<flowRateTable.params.z<<" "<<flowRateTable.params.n<<" ";
       ostr<<flowRateTable.baseWidth.initialValue<<" ";
       ostr<<flowRateTable.baseWidth.step<<" ";
       ostr<<flowRateTable.waterDepth.initialValue<<" ";
       ostr<<flowRateTable.waterDepth.step<<" ";
       for(i=0;i<N;i++)
       for(j=0;j<N;j++)
       {
       ostr<<flowRateTable.flowRates[i][j]<<" ";
       }
       }
    
       return 1;
    
    
    }
    
    
    int setFlowRateTableName()
    {
     char fname[20],tn[20],ch;
     char *ext = ".frt";
     FILE *stream;
    
     cout<<"Enter the filename(without file extension):\n";
     cin>>fname;
     strcpy(tn,fname);
     strcat(fname, ext);
    
    
    
     if ((stream = fopen(fname, "r"))!= NULL)
     {
       cout<<"Such file already exists. Would you like to overwrite it (y/n)? \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[7], *ext = ".frt",ch,p;
      int i,j,k;
    
      l:
      cout<<"Enter the filename(without file extension):\n";
      cin>>fname;
      strcat(fname,ext);
      ifstream istr(fname,ios::in);
      if(!istr)
      {
        cout<<"Error opening file or such file does not exist\n";
          cout<<"Would you like to try again(y/n)?\n";
          for(;;)
       {
       ch = getch();
       if(ch=='y'||ch=='n') break;
       }
    
       if(ch=='n') return 1;
       else goto l;
      }
    
       istr>>flowRateTable.name>>flowRateTable.params.s;
       istr>>flowRateTable.params.z>>flowRateTable.params.n;
       istr>>flowRateTable.baseWidth.initialValue;
       istr>>flowRateTable.baseWidth.step;
       istr>>flowRateTable.waterDepth.initialValue;
       istr>>flowRateTable.waterDepth.step;
       for(i=0;i<N;i++)
       for(j=0;j<N;j++)
       {
       istr>>flowRateTable.flowRates[i][j];
       }
    
       cout<<"Table successfully loaded\n";
       getch();
    
       return 0;
    }
    
    void outline(void)
    {
     for(int i=0;i<80;i++) cout<<"-";
    }
    
    void spaces(int total, int word, int pos)
    {
     int t,i,r,b;
     t=total-word;
    
    
    
    
     if(t%2==0) t/=2;
     else
     {
    
         if(pos==0) t/=2;
         else t=t/2+1;
     }
    
     for(i=0;i<t;i++) cout<<" ";
    
    }
    
    
    void displayFlowRateTable()
    {
     char s[20];
     int i,j;
    
     outline();
     cout<<"|";
     spaces(78,strlen(flowRateTable.name),0);
     cout<<flowRateTable.name;
     spaces(78,strlen(flowRateTable.name),1);
     cout<<"|";
     outline();
    
     cout<<"|";
     spaces(78,strlen("Params"),0);
     cout<<"Params";
     spaces(78,strlen("Params"),1);
     cout<<"|";
     outline();
    
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.params.s);
     spaces(25,strlen(s),0);
     cout<<s;
     spaces(25,strlen(s),1);
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.params.z);
     spaces(26,strlen(s),0);
     cout<<s;
     spaces(26,strlen(s),1);
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.params.n);
     spaces(25,strlen(s),0);
     cout<<s;
     spaces(25,strlen(s),1);
     cout<<"|";
     outline();
    
     cout<<"|";
     spaces(38,strlen("Base Width"),0);
     cout<<"Base Width";
     spaces(38,strlen("Base Width"),1);
     cout<<"|";
     spaces(39,strlen("Water Depth"),0);
     cout<<"Water Depth";
     spaces(39,strlen("Water Depth"),1);
     cout<<"|";
     outline();
    
     cout<<"|";
     spaces(19,strlen("Value"),0);
     cout<<"Value";
     spaces(19,strlen("Value"),1);
     cout<<"|";
     spaces(18,strlen("Step"),0);
     cout<<"Step";
     spaces(18,strlen("Step"),1);
     cout<<"|";
     spaces(19,strlen("Value"),0);
     cout<<"Value";
     spaces(19,strlen("Value"),1);
     cout<<"|";
     spaces(19,strlen("Step"),0);
     cout<<"Step";
     spaces(19,strlen("Step"),1);
     cout<<"|";
     outline();
    
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.baseWidth.initialValue);
     spaces(19,strlen(s),0);
     cout<<s;
     spaces(19,strlen(s),1);
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.baseWidth.step);
     spaces(18,strlen(s),0);
     cout<<s;
     spaces(18,strlen(s),1);
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.waterDepth.initialValue);
     spaces(19,strlen(s),0);
     cout<<s;
     spaces(19,strlen(s),1);
     cout<<"|";
     sprintf(s, "%.2f",flowRateTable.waterDepth.step);
     spaces(19,strlen(s),0);
     cout<<s;
     spaces(19,strlen(s),1);
     cout<<"|";
     outline();
    
     getch();
    
     cout<<"|";
     spaces(78,strlen("Flow Rates"),0);
     cout<<"Flow Rates";
     spaces(78,strlen("Flow Rates"),1);
     cout<<"|";
     outline();
    
     int c;
    
     for(i=0;i<N;i++)
     {
     c=0;
      for(j=0;j<N;j++)
      {
       sprintf(s, "%.1e ",flowRateTable.flowRates[i][j]);
       c+=strlen(s);
    
       cout<<s;
      }
    
      outline();
     }
    
    
     getch();
    }
    
    void estimateWaterDepth()
    {
     int cix,rix;
     double b, q, waterDepthEstimate, stepFraction;
    
     do
     {
     cout<<"Please enter a base width:\n";
     cin>>b;
     cix = getColIndex(b);
    
     if(cix==-1) cout<<"Invalid base value, must be in the table heading";
    
     }while(cix==-1);
    
     do
     {
     cout<<"Please enter a flow rate\n";
     cin>>q;
    
     rix = getRowIndex(q,cix);
    
     if(rix==-1) cout<<"Invalid value, must be in the range of the base width column";
    
     }while (rix==-1);
    
    
    
    
     if(rix==N-1)
     {
       waterDepthEstimate = flowRateTable.flowRates[N-1][cix];
     }
     else
     {
       stepFraction = flowRateTable.flowRates[rix][cix]/
    		  flowRateTable.flowRates[rix+1][cix]-
    		  flowRateTable.flowRates[rix][cix];
    
    
       waterDepthEstimate = flowRateTable.waterDepth.initialValue+
    			flowRateTable.waterDepth.step * rix +
    			stepFraction*flowRateTable.waterDepth.step;
     }
    
     cout<<"Water depth estimated at "<<waterDepthEstimate;
     getch();
    
    }
    
    double getColIndex(double bGiven)
    {
     double b = flowRateTable.baseWidth.initialValue;
     double tolerance = 0.001*flowRateTable.baseWidth.step;
     double retValue=-1;
     int i;
    
     for(i=0;i<N;i++)
     {
      if(fabs(b-bGiven)<tolerance) retValue=i;
    
      if(retValue!=-1) break;
    
      b+=flowRateTable.baseWidth.step;
    
     }
    
     return retValue;
    
    }
    
    
    double getRowIndex(double qGiven, int col)
    {
     double q;
     double tolerance;
     double retValue=-1;
     int i;
    
     for(i=0;i<N;i++)
     {
      q = flowRateTable.flowRates[i][col];
      tolerance = 0.01*flowRateTable.flowRates[i][col];
      if(fabs(q-qGiven)<tolerance) retValue=i;
    
      if(retValue!=-1) break;
    
     }
    
     return retValue;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    here you go:



    Code:
    
    
    
    
    
    
    SUCK IT!

Popular pages Recent additions subscribe to a feed