Thread: Gaussian Elimination!!!

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    8

    Gaussian Elimination!!!

    This is what I have so far but I need help figuring out how to print the matrix from the function!!!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
       int choice;
       do
        {
             printf("____Menu____\n");
             printf("1: Create Augmented Matrix\n");
             printf("2: Print Current Augmented Matrix\n");
             printf("2: Solve Augmented Matrix\n");
             printf("3: See Solution Matrix\n");
             printf("5: Exit\n");
             scanf("%d", &choice);
    
    
             switch(choice)
             {
                 case 1: cmat();
                     break;
                 case 2: printmat();
                     break;
                 case 3: solvemat();
                     break;
                 case 4: solutionmat();
                     break;
                 case 5: printf("Quitting program!\n");
                     break;
                 default: printf("Invalid choice!\n");
                     break;
             }
        } while (choice !=3);
    }
    
    
    void cmat(void)
    {
        int i,j,n;
        float A[20][20];
        printf("\nEnter the order of matrix: ");
        scanf("%d",&n);
        printf("\nEnter the elements of augmented matrix:\n\n");
        free(A);
        for(i=1; i<=n; i++)
        {
           for(j=1; j<=(n+1); j++)
              {
               printf("A[%d][%d] : ", i,j);
               scanf("%f",&A[i][j]);
              }
    }
    }
    
    
    void printmat(void)
    {
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    This is what I have so far but I need help figuring out how to print the matrix from the function!!!
    What are you getting now and how is it different from what you were expecting?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This is what I have so far but I need help figuring out how to print the matrix from the function!!!
    It's much more basic than that, if you're trying things like free(A)

    Let's keep it simple for the moment.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_MAT_SIZE  10
    
    void cmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE]);
    void printmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE]);
    void solvemat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE]);
    void solutionmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE]);
    
    int main()
    {
      float mat[MAX_MAT_SIZE][MAX_MAT_SIZE];
      int choice;
      do
      {
        printf("____Menu____\n");
        printf("1: Create Augmented Matrix\n");
        printf("2: Print Current Augmented Matrix\n");
        printf("2: Solve Augmented Matrix\n");
        printf("3: See Solution Matrix\n");
        printf("5: Exit\n");
        scanf("%d", &choice);
        
        switch(choice)
        {
          case 1: cmat(mat);
               break;
          case 2: printmat(mat);
               break;
          case 3: solvemat(mat);
               break;
          case 4: solutionmat(mat);
               break;
          case 5: printf("Quitting program!\n");
                         break;
          default: printf("Invalid choice!\n");
                          break;
        }
      } while (choice !=3);
      return 0;
    }
    
    
    void cmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
      int i,j,n;
      printf("\nEnter the order of matrix: ");
      scanf("%d",&n);
      printf("\nEnter the elements of augmented matrix:\n\n");
      for(i=1; i<=n; i++)
      {
        for(j=1; j<=(n+1); j++)
        {
          printf("A[%d][%d] : ", i,j);
          scanf("%f",&mat[i][j]);
        }
      }
    }
    
    void printmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
    }
    
    void solvemat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
    }
    
    void solutionmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
    }
    A large fixed sized matrix makes it easier to think about the rest of the problem.

    Your first step is to return 'n' from cmat() and pass that as a parameter to printmat().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2015
    Posts
    8
    thanks! should I use pointers for that? That's what I'm having more trouble with. How do I keep what I get from the first function int he memory and translate it to the printmat function?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Everything you need is in the example I posted.

    There is no need to create pointers, or allocate memory, or any other advanced technique.

    Just write a loop inside printmat and see.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2015
    Posts
    8
    So I'm putting this in my printmat function but it doesn't work
    Code:
    void printmat(float mat[MAX_MAT_SIZE][MAX_MAT_SIZE])
    {
        int i,j,n;
        for(i=0; i<n; i++)
      {
        for(j=0; j<(n+1); j++)
        {
          printf("%f : ", mat[i][j]);
          printf("\n");
    }
      }}

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int i,j,n;
    OK, so what is the value of n in your for loop.

    Quote Originally Posted by salem
    Your first step is to return 'n' from cmat() and pass that as a parameter to printmat().
    As I said, you need to return n from your input function, and pass that n to your print function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2015
    Posts
    8
    I don't really understand how to return the value from cmat and pas it to printmat. Do I just add return(n) at the end of my cmat function?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yeah, that would be a good start.

    I suggest you read your reference book / course notes on how to declare a function and return a result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But I see you've managed to get someone else to think for you already.
    Help with functions!!!

    Oh well, nevermind - there goes another learning opportunity for you.

    You might consider that on your journey from the sea to the top of Everest, you've barely crawled to the top of the beach.
    Quite how you'll fair when the hills appear is hard to say (badly IMO).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gaussian Elimination
    By jhosie_g in forum C Programming
    Replies: 1
    Last Post: 11-13-2015, 01:39 AM
  2. Gaussian Elimination program
    By whatever125 in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2011, 05:11 AM
  3. Gaussian elimination in C
    By Meander14 in forum C Programming
    Replies: 2
    Last Post: 09-26-2007, 03:43 AM
  4. Gaussian Elimination
    By Fiverz in forum C Programming
    Replies: 1
    Last Post: 02-05-2003, 12:59 PM
  5. Gaussian Elimination Problem
    By Inexorable in forum C Programming
    Replies: 1
    Last Post: 11-08-2002, 02:25 AM

Tags for this Thread