Thread: arrays- two dimensional

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    alright, i pretty much got it now, i believe i got the right answer now, imma double check by using pen and paper =] .........looky here:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void elements(float*,float*);
    int Finish();
    float equation(double p, double v, double a, double b, double n);
    
    int main()
    {
      do
       {
        FILE *output = NULL;
    
             float p,                  /*Pressure*/
                   v,                  /*Volume of Container*/
                   n,                  /*Number of Moles*/
                   a,                 /*Inputted by user defined element*/
                   b,                 /*Inputted by user defined element*/
                   eqfinal;           /*Van der Waals equation*/
    
        if((output=fopen("c:answer.txt","w")) == NULL)
           printf("Error opening data file.\n");
        else
        {
             printf("Van der Waals Equation:\n\n");
             printf("     an^2\n"
                    "(P + -----)(V-nb)=nRT\n"
                    "      V^2\n\n");
    
             elements(&a,&b);
    
             printf("\nElement has a and b defined as:"
                    "\na=%f b=%f\n\n",a,b);
    
             printf("Please enter the amount of pressure (atm), volume (L), and "
                    "moles (mol):\n(Separated by a Space)\n\n");
             scanf("%f %f %f",&p,&v,&n);
    
             printf("\nPressure=%.3f Volume=%.3f Moles=%.3f\n\n",p,v,n);
    
    
    
             printf("Temperature is equal to %.3f Kelvin\n\n",equation(p,v,a,b,n));
    
             fprintf(output,"AC\nKA\n\n"
                          "Since a=%f and b=%f\n"
                          "Pressure=%f atm Volume=%f L and Moles=%f mol\n\n"
                          "Temperature is equal to %f Kelvin\n\n",a,b,
                          p,v,n,eqfinal);
             fclose(output);
        }
    
      }while ( Finish() == 0 );
    
      system("PAUSE");
      return 0;
    }
    
    void elements(float *a, float *b)
    {
      int user_element;
    
      float constants[6][2]={
                             {3.640, .04267},
                             {.03457, .0237},
                             {3.716, .04081},
                             {1.378, .03183},
                             {6.803, .05636},
                             {5.536, .03049}
                            };
    
      printf("Which element do you want to solve for T?\n\n");
      printf("1-Carbon Dioxide\n2-Helium\n3-Hydrogen Chloride\n"
             "4-Oxygen\n5-Sulfur Dioxide\n6-Water\n\n");
      scanf("%d",&user_element);
    
      if(user_element<1 || user_element>6)
       {
          printf("\nError, Rerunning Choices!\n\n");
          elements(a,b);
       }
      else
       {
          switch(user_element)
          {
             case 1:
                   *a=constants[0][0];
                   *b=constants[0][1];
                   break;
             case 2:
                   *a=constants[1][0];
                   *b=constants[1][1];
                   break;
             case 3:
                   *a=constants[2][0];
                   *b=constants[2][1];
                   break;
             case 4:
                   *a=constants[3][0];
                   *b=constants[3][1];
                   break;
             case 5:
                   *a=constants[4][0];
                   *b=constants[4][1];
                   break;
             case 6:
                   *a=constants[5][0];
                   *b=constants[5][1];
                   break;
          }
       }
    }
    
    float equation(double p, double v, double a, double b, double n)
    {
      float eqfinal,
            eq1,
            eq2;
    
      const float R=8.314472;
    
      eq1=p+a*((n*n)/(v*v));
      eq2=v-n*b;
      eqfinal=(eq1*eq2)/(n*R);
    
      printf("Testing a=%f, b=%f\n", a, b);
    
      return eqfinal;
    }
    int Finish()
    {
      char cAnswer = 0;
      while (cAnswer != 'Y' && cAnswer != 'N')
      {
          printf("Want to quit ([Y]es/[N]o)?\n");
          scanf("%c", &cAnswer);
          cAnswer = toupper(cAnswer);
      }
      return (cAnswer == 'Y');
    }

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope. Same problems.
    Code:
             fprintf(output,"AC\nKA\n\n"
                          "Since a=&#37;f and b=%f\n"
                          "Pressure=%f atm Volume=%f L and Moles=%f mol\n\n"
                          "Temperature is equal to %f Kelvin\n\n",a,b,
                          p,v,n,eqfinal);
    eqfinal is uninitialized.
    You're mixing doubles & floats.

    Btw, I'm not sure if you missed this, but to use toupper, you need to include #include <ctype.h>

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    yea i fixed that, i believe everything is working correctly now, all i have to do now is just make it look a lil nicer and add a few more printf statements, add some extra elements and im good to go...thank you for your help. here's the end result:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void elements(float*,float*);
    int Finish();
    float equation(double p, double v, double a, double b, double n);
    
    int main()
    {
      do
       {
        FILE *output = NULL;
    
             float p,                  /*Pressure*/
                   v,                  /*Volume of Container*/
                   n,                  /*Number of Moles*/
                   a,                 /*Inputted by user defined element*/
                   b,                 /*Inputted by user defined element*/
                   eqfinal;           /*Van der Waals equation*/
    
        if((output=fopen("c:answer.txt","w")) == NULL)
           printf("Error opening data file.\n");
        else
        {
             printf("Van der Waals Equation:\n\n");
             printf("     an^2\n"
                    "(P + -----)(V-nb)=nRT\n"
                    "      V^2\n\n");
    
             elements(&a,&b);
    
             printf("\nElement has a and b defined as:"
                    "\na=%f b=%f\n\n",a,b);
    
             printf("Please enter the amount of pressure (atm), volume (L), and "
                    "moles (mol):\n(Separated by a Space)\n\n");
             scanf("%f %f %f",&p,&v,&n);
    
             printf("\nPressure=%.3f Volume=%.3f Moles=%.3f\n\n",p,v,n);
    
    
    
             printf("Temperature is equal to %.3f Kelvin\n\n",equation(p,v,a,b,n));
    
             fprintf(output,"AC\nKA\n\n"
                          "Since a=%f and b=%f\n"
                          "Pressure=%f atm Volume=%f L and Moles=%f mol\n\n"
                          "Temperature is equal to %f Kelvin\n\n",a,b,
                          p,v,n,equation(p,v,a,b,n));
             fclose(output);
        }
    
      }while ( Finish() == 0 );
    
      system("PAUSE");
      return 0;
    }
    
    void elements(float *a, float *b)
    {
      int user_element;
    
      float constants[6][2]={
                             {3.640, .04267},
                             {.03457, .0237},
                             {3.716, .04081},
                             {1.378, .03183},
                             {6.803, .05636},
                             {5.536, .03049}
                            };
    
      printf("Which element do you want to solve for T?\n\n");
      printf("1-Carbon Dioxide\n2-Helium\n3-Hydrogen Chloride\n"
             "4-Oxygen\n5-Sulfur Dioxide\n6-Water\n\n");
      scanf("%d",&user_element);
    
      if(user_element<1 || user_element>6)
       {
          printf("\nError, Rerunning Choices!\n\n");
          elements(a,b);
       }
      else
       {
          switch(user_element)
          {
             case 1:
                   *a=constants[0][0];
                   *b=constants[0][1];
                   break;
             case 2:
                   *a=constants[1][0];
                   *b=constants[1][1];
                   break;
             case 3:
                   *a=constants[2][0];
                   *b=constants[2][1];
                   break;
             case 4:
                   *a=constants[3][0];
                   *b=constants[3][1];
                   break;
             case 5:
                   *a=constants[4][0];
                   *b=constants[4][1];
                   break;
             case 6:
                   *a=constants[5][0];
                   *b=constants[5][1];
                   break;
          }
       }
    }
    
    float equation(double p, double v, double a, double b, double n)
    {
      float eqfinal,
            eq1,
            eq2;
    
      const float R=8.314472;
    
      eq1=p+a*((n*n)/(v*v));
      eq2=v-n*b;
      eqfinal=(eq1*eq2)/(n*R);
    
      return eqfinal;
    }
    int Finish()
    {
      char cAnswer = 0;
      while (cAnswer != 'Y' && cAnswer != 'N')
      {
          printf("Want to quit ([Y]es/[N]o)?\n");
          scanf("%c", &cAnswer);
          cAnswer = toupper(cAnswer);
      }
      return (cAnswer == 'Y');
    }

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    eqfinal is now unused. You can remove it.
    You're still mixing doubles and floats. Select one type and stay with it.

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    is mixing them really a pain? heh, well, i been trying to fix this with a friend for days and now that i got some "light", i feel better since the deadline for this project is tomorrow.

    am i mixing them only in that output statement, or do u mean mixing them in the entire program?, i've tried lots of diff ways to make it work and so far, this seems to do the job despite it being messy =\ ......

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're mixing them in the program.
    There are two things to consider:
    If you use double first then floats at other parts, then you're basically truncating a lot of precision.
    Otherwise, you are using unnecessary memory with unnecessary precision.
    Further, you're also using a lot of doubles in float arrays, thus truncating them and causing compiler warnings. You want to avoid compiler warnings if possible.
    Good practice.

    Make all variables double OR float.

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    alright, i'll look into it then, for now imma fix and clean it up a bit, add more and etc......but thanks for the advice.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To get warnings in Dev-C++:

    Under Tools -> Compiler Options:
    Where it says "Add the following commands when calling compiler:" put -Wall in the box. (And make sure the check box gets checked, it doesn't automatically.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 08:17 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM