Thread: simple matrix

  1. #31
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Thanks I removed it and its working FINALLY

  2. #32
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Just a simple misunderstanding -> To be fair, I'm not well known for my verbal skills :P
    Fact - Beethoven wrote his first symphony in C

  3. #33
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Okay so I've modified my code
    I now want it to work like this
    Code:
    cmd> A 2 2 1 2 3 4
    (where A is the name of the matrix it is assigned to and 2 2 are the rows and columns and 1 2 3 4 are the elements of the matrix)
    displays matrix
    cmd> A 2 2 3 4 5 6 
    (new matrix A displayed)
    so "cmd>" is kind of this thing awaiting my order. Further on I plan on letting the user even enter a second matrix B


    So this is what I came up with. If there is some other way of writing cmd> without using strings and that is more easier please let me know. I dont want to end up jumbling strings arrays and pointers.

    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_CMD_LEN 25
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2;
      int **A;
      int i = 0, j = 0, flag =1;
      char a;
      char command[MAX_CMD_LEN];
      char *p;
    
    
       do{
        printf("\ncmd> ");
    
    
        if(fgets(command,  MAX_CMD_LEN, stdin) != NULL){
          if((p = strchr( command, '\n')) != NULL)
            *p = '\0';
        }
        if(command[0] == 'A'){
    
    
    printf("Enter the elements of the first matrix write 99 when done:");
    scanf("%d %d",&row_size1,&col_size1);
    
    
      A= malloc(row_size1*sizeof(int*));
      for(i=0;i<row_size1;i++)
        {
          A[i]=malloc(col_size1*sizeof(int));
        }
    
    
      for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
            {
              scanf("%d",&A[i][j]);
       }
               }
    
    
    matrix_display(A, row_size1, col_size1);
    }
    }
       
    while (flag ==1);
    return 0;
    }
    
    void matrix_display(int *matrix, int row_size1, int col_size1)
    {
      int i,j;
    
    printf("matrix  is\n");
    for(i=0;i<row_size1;i++)
        {
          for(j=0;j<col_size1;j++)
          printf("%d ", matrix[i][j]);
          printf("\n");
        }
    
    for(i=0;i<row_size1;i++)
        {
          free(matrix[i]);
        }
     free(matrix);
    Last edited by zafy; 11-14-2012 at 04:55 PM.

  4. #34
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    OK - I'm going to give you back you code with good indentation - It will be very obvious that you have not got your braces right. Copy and paste this code into where ever you are coding from and try to keep it consistent with this
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    #define MAX_CMD_LEN 25
    
    
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    
    
    int main (void)
    {
        int row_size1,col_size1,row_size2,col_size2;
        int **A;
        int i = 0, j = 0, flag =1;
        char a;
        char command[MAX_CMD_LEN];
        char *p;
    
    
    
    
        do
        {
            printf("\ncmd> ");
    
    
    
    
            if(fgets(command,  MAX_CMD_LEN, stdin) != NULL)
            {
                if((p = strchr( command, '\n')) != NULL)
                    *p = '\0';
            }
            if(command[0] == 'A')
            {
    
    
    
    
                printf("Enter the elements of the first matrix write 99 when done:");
                scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
                A= malloc(row_size1*sizeof(int*));
                for(i=0; i<row_size1; i++)
                {
                    A[i]=malloc(col_size1*sizeof(int));
                }
    
    
    
    
                for(i=0; i<row_size1; i++)
                {
                    for(j=0; j<col_size1; j++)
                    {
                        scanf("%d",&A[i][j]);
                    }
                }
            }
    
    
    
    
            matrix_display(A, row_size1, col_size1);
        }
    }
    
    
    while (flag ==1);
    return 0;
    }
    
    
    void matrix_display(int *matrix, int row_size1, int col_size1)
    {
        int i,j;
    
    
        printf("matrix  is\n");
        for(i=0; i<row_size1; i++)
        {
            for(j=0; j<col_size1; j++)
                printf("%d ", matrix[i][j]);
            printf("\n");
        }
    
    
        for(i=0; i<row_size1; i++)
        {
            free(matrix[i]);
        }
        free(matrix);
    Fact - Beethoven wrote his first symphony in C

  5. #35
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Sorry about the braces I caught on to that right after I posted it and deleted the extra one.
    Even though I am getting warnings, the program is running but not the way i want it to
    you see like I mentioned above "cmd>" isnt the first thing printing

  6. #36
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Add this to after your printf statement
    Code:
        do
        {
            printf("\ncmd> ");
            fflush(stdout); 
    Fact - Beethoven wrote his first symphony in C

  7. #37
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Isnt fflush a C++ reference?
    It is not making a difference
    Code:
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    
    
    
    
    #define MAX_CMD_LEN 25
    
    
    
    
    
    
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    
    
    
    
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2;
      int **A;
     int i = 0, j = 0, flag =1;
      char a;
      char command[MAX_CMD_LEN];
      char *p;
    
    
    
    
    
    
    
    
        do
          {
            printf("\ncmd> ");
     fflush(stdout);
    
    
    
    
    
    
            if(fgets(command,  MAX_CMD_LEN, stdin) != NULL)
              {
                if((p = strchr( command, '\n')) != NULL)
                  *p = '\0';
              }
            if(command[0] == 'A')
              {
     printf("Enter the elements of the first matrix\n:");
                scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
    
    
    
    
                A= malloc(row_size1*sizeof(int*));
     for(i=0; i<row_size1; i++)
                  {
                    A[i]=malloc(col_size1*sizeof(int));
                  }
    
    
    
    
    
    
    
    
                for(i=0; i<row_size1; i++)
                  {
                    for(j=0; j<col_size1; j++)
     {
                        scanf("%d",&A[i][j]);
                      }
                  }
    
    
    
    
    
    
    
    
    
    
            matrix_display(A, row_size1, col_size1);
          }
     }
    
    
    
    
    while (flag ==1);
    return 0;
    }
    
    
    
    
    void matrix_display(int *matrix, int row_size1, int col_size1)
    {
      int i,j;
     printf("matrix  is\n");
      for(i=0; i<row_size1; i++)
        {
          for(j=0; j<col_size1; j++)
            printf("%d ", matrix[i][j]);
          printf("\n");
        }
     
    
    for(i=0; i<row_size1; i++)
        {
          free(matrix[i]);
        }
      free(matrix);
    and does this line make sense that I have in the code:
    Code:
    if(command[0] == 'A') // if first element if matrix is A then allocate a matrix A and print it out
              {

  8. #38
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Isnt fflush a C++ reference?
    no.

    fgets puts everything up to the '\n' character into the array called "command" -> Nothing will be left on the stdin (buffered input from the keyboard), so you can't use any of the scanf's to read the command line any more.

    You need to decide if you want to put the command line into the array as a string and work with that, or leave everything on stdin and get them one at a time.
    Fact - Beethoven wrote his first symphony in C

  9. #39
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    I don't know which one is easier to do?

  10. #40
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    If I wanted to put the command line into the array as a string how exactly would I do that ?

  11. #41
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In the code from post #37/#34/#33 you are already doing it with the fgets statement.
    Fact - Beethoven wrote his first symphony in C

  12. #42
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Okay so if I already have that then should I change my remaining scanf's to fscanfs becuase then I do include stdin

  13. #43
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    If because of the fgets everything I enter including my matrix values are stored in the command array, then what should i do?
    use something besides fgets

  14. #44
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If it were up to me, I'd use the fgets and then go through each entry using strtok - But this might be a bit of a learning curve for you

    You are probably better getting rid of the fgets and replacing it with getchar, as it will only get the first character off stdin -> If it is 'A', then scanf for the two size integers, and then scanf for each element integer after that.
    Fact - Beethoven wrote his first symphony in C

  15. #45
    Registered User
    Join Date
    Nov 2012
    Posts
    106
    Is this what you mean?
    Code:
    
    #include<stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_CMD_LEN 25
    
    
    void matrix_display(int *matrix, int row_count, int column_count);
    
    
    int main (void)
    {
      int row_size1,col_size1,row_size2,col_size2;
      int **A;
      int i = 0, j = 0, flag =1;
      char a;
      char command[MAX_CMD_LEN];
      char *p;
    
    
        do
          {
            printf("\ncmd> ");
            fflush(stdout);
    
    
    
    
            while ((p = getchar()) != '\n' && p != EOF)
            if(command[0] == 'A')
              {
                printf("Enter the elements of the first matrix\n:");
                scanf("%d %d",&row_size1,&col_size1);
    
    
    
    
                A= malloc(row_size1*sizeof(int*));
                for(i=0; i<row_size1; i++)
                  {
                    A[i]=malloc(col_size1*sizeof(int));
                  }
    
    
    
                for(i=0; i<row_size1; i++)
                  {
                    for(j=0; j<col_size1; j++)
                      {
                        scanf("%d",&A[i][j]);
                      }
                  }
    
    
                matrix_display(A, row_size1, col_size1);
              }
          }
    
    
    
        while (flag ==1);
        return 0;
    }
    
    void matrix_display(int *matrix, int row_size1, int col_size1)
    {
      int i,j;
      printf("matrix  is\n");
      for(i=0; i<row_size1; i++)
        {
          for(j=0; j<col_size1; j++)
            printf("%d ", matrix[i][j]);
          printf("\n");
        }
    
    
    
    
      for(i=0; i<row_size1; i++)
        {
          free(matrix[i]);
        }
      free(matrix);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  2. help with simple programs. Matrix multiplications
    By ovadoggvo in forum C Programming
    Replies: 1
    Last Post: 05-18-2005, 11:46 PM
  3. Matrix Multiplication with simple syntax! plz help.
    By codebox in forum C Programming
    Replies: 6
    Last Post: 11-05-2004, 09:03 AM
  4. Simple Matrix Operations?
    By devin in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2003, 12:10 AM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM