gaussian elmination into fractions.

This is a discussion on gaussian elmination into fractions. within the C++ Programming forums, part of the General Programming Boards category; im supposed to send a data file containing values that look like this Code: 3 2 3 1 11 4 ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    gaussian elmination into fractions.

    im supposed to send a data file containing values that look like this
    Code:
    3
    2   3   1   11
    4  -2   3    9
    3   5  -3    4
    the first value is put into variable N and the other numbers are put into an array to solve using the gaussian method. here's my code for FLOAT point numbers. i can solve it using FLOAT point numbers.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    ifstream infile;
    
    void printEquation(float array[10][11], int N)
     {
      for (int i=0; i<N; i++)
       {
        for (int j=0; j<(N+1); j++)
         {
          if (j==0)
           { cout << endl;}
          cout << setw(10) << array[i][j];
          }  // for j
       }  // for i
     }
           
    int main()
    {
     char fileName[100], varName[10];
     int N, i, j, temp;
     float arr[10][11];
    
      for(i=0; i<10; i++) // sets variable names
       {
        varName[i] ='a'+i;
       }        
    
     cout << endl;
     cout << "Enter a filename to read the values from: ";
     cin >> fileName;
    
    
     infile.open(fileName);
     
    
     if (infile.fail())
      {
       cout << "Opening file " << fileName << " for input failed.  Goodbye" 
            << endl;
       exit(1);
      }
    
     infile >> N;
    
     cout << N << " Equations" << endl;
     if (N > 10)
      {
       cout << "Opening file " << fileName << " failed because the value N
    that the file reads is greater than 10.  Check your data file.  Goodbye."
            << endl;
       cout << endl;
       exit(1);
      }
    
     for (i=0; i<N; i++)
      {
       for (j = 0; j<(N+1); j++)
        {
         infile >> temp;
         arr[i][j] = temp;
        }
      }
    
       printEquation(arr, N);
    
     for (int i = 0; i < N; i++) // for each column
      {
       cout << endl << "column " << i << ":" << endl;
    
      if (arr[i][i] !=0){
       float divisor = arr[i][i];
    
       for (int k=0; k < (N+1); k++)
         {arr[i][k] /= divisor;}
       }
       printEquation(arr, N);
    
       cout << endl;
       for (int j=0; j<N; j++) // for each row
        {
         if (j !=i)
          {
           float multiplier = arr[j][i];
           for (int k=0; k<(N+1); k++)
             arr[j][k] -= arr[i][k] * multiplier;
     
           cout << endl;
       printEquation(arr, N);
    
          }
        }
    
       }
     cout << endl;
           printEquation(arr, N);
    
    
    
    
    /*
     for (i=0; i<N; i++)
       {
        cout << endl;
        cout << varName[i] << "=" << arr[0][N+1] << endl;
        cout << endl;
       }
    */
    
     cout << endl;
    }
    but for this assignment..im supposed to change all the numbers into fractions! this is what i've tried so far...and im getting millions of errors. please help!!

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    ifstream infile;
    
    class Fraction 
    {
    
    private:
    
      int numerator;
      int denominator;
    
    public:
    
      Fraction()
       {
        numerator = 0;
        denominator = 1;
       }
      
      void setNumerator(int n)
       {
        numerator = n;
       }
    
      void dumpFraction()
       {
        cout.width(4);
        cout << numerator;
        if (denominator !=1)
          cout << "/" << denominator;
       }
    
      void div( Fraction divisor )
       {
        numerator *= divisor.denominator;
        denominator *= divisor.numerator;
       }
    
      void mult( Fraction multiplier)
       {
        numerator /= multiplier.denominator;
        denominator /= multiplier.numerator;
       }
    
    }; // class Fraction
    
    void printEquation(Fraction array[10][11], int N)
     {
      for (int i=0; i<N; i++)
       {
        for (int j=0; j<(N+1); j++)
         {
          if (j==0)
           { cout << endl;}
          cout << setw(10) << array[i][j];
          }  // for j
       }  // for i
     }
           
    int main()
    {
     
     char fileName[100], varName[10];
     int N, i, j, temp;
     Fraction arr[10][11];
    
      for(i=0; i<10; i++) // sets variable names
       {
        varName[i] ='a'+i;
       }        
    
     cout << endl;
     cout << "Enter a filename to read the values from: ";
     cin >> fileName;
    
    
     infile.open(fileName);
    
     if (infile.fail())
      {
       cout << "Opening file " << fileName << " for input failed.  Goodbye" 
            << endl;
       exit(1);
      }
    
     infile >> N;
    
     cout << N << " Equations" << endl;
     if (N > 10)
      {
       cout << "Opening file " << fileName << " failed because the value N
    that the file reads is greater than 10.  Check your data file.  Goodbye."
            << endl;
       cout << endl;
       exit(1);
      }
    
     for (i=0; i<N; i++)
      {
       for (j = 0; j<(N+1); j++)
        {
         infile >> temp;
         arr[i][j].setNumerator(temp);
        }
      }
    
       printEquation(arr, N);
    
    
     for (int i = 0; i < N; i++) // for each column
      {
       cout << endl << "column " << i << ":" << endl;
    
      if (arr[i][i] !=0)
      {
       Fraction divisor = arr[i][i];
    
       for (int k=0; k < (N+1); k++)
         {
          arr[0][k].div(divisor);
         }
       }
       printEquation(arr, N);
    
       cout << endl;
       for (int j=0; j<N; j++) // for each row
        {
         if (j !=i)
          {
           Fraction multiplier = arr[j][i];
           for (int k=0; k<(N+1); k++)
             arr[j][k] -= arr[i][k] * multiplier; 
           cout << endl;
       printEquation(arr, N);
    
          }
        }
    
       }
     cout << endl;
           printEquation(arr, N);
    
    
    
    
    /*
     for (i=0; i<N; i++)
       {
        cout << endl;
        cout << varName[i] << "=" << arr[0][N+1] << endl;
        cout << endl;
       }
    */
    
     cout << endl;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Havenīt quite read your code, but, you donīt mention what is your problem. Compiler errors? Malfunctions? What?

  3. #3
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    I didn't look at all that code, but the simpliest way is to create a new data type, Fractions (which I see you have) and use it in the original code the same way as you used floats (or doubles). Overload all the operators to work properly, and you shouldn't have to modify a thing in the original code. If you do this, you'll be using C++'s feature to their full advantage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 04:10 AM
  3. Replies: 1
    Last Post: 03-17-2005, 07:53 AM
  4. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 06:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21