Problem in arrays

This is a discussion on Problem in arrays within the C Programming forums, part of the General Programming Boards category; Hi, I'm making a little software, what make and sum array or matrices (I don't know in english )... I ...

  1. #1
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6

    Question Problem in arrays

    Hi,


    I'm making a little software, what make and sum array or matrices (I don't know in english )... I can make the first array, is fine, but in the second array, the program copy the last row on the other rows...


    I need a help, please...


    and sorry my english, I'm brazilian and ... my english isnt very good ...thank you


    I use DEV C++ on Win Seven

    Code:
    int main(){     
         
          int i,j;
          int linA,colA;
          int mA[linA][colA];
          int aij,mult_i,mult_j;
          
          
          printf("### DETERMINANTE DE UMA MATRIZ ###\n\n\n");
    //CRIANDO A MATRIZ
          printf("\n### MATRIZ A ###\n\n");
          
          printf("linhas ? "); //number of rows
          scanf("%i",&linA);
          
          printf("\n\ncolunas ? ");//number of columns
          scanf("%i",&colA);
          
          for(i=0; i<linA; i++)
          {
                   for(j=0; j<colA; j++)
                   {
                            printf("Digite o numero da %i linha, %i coluna : (MATRIZ A): ",i,j); //first number
                            scanf("%i",&mA[i][j]);
                            
                            }
                 printf("\n");
                   }
          
          for(i=0; i<linA; i++)
          {
                   for(j=0; j<colA; j++)
                   {
                            printf("%i ",mA[i][j]);
                            }
                   printf("\n");
                            
                            }
                            
    //CRIANDO A SEGUNDA MATRIZ - second array
          int linB,colB;    
          int mB[linB][colB];
          i=0;
          j=0;
          
          printf("\n### MATRIZ B ###\n\n");
           
          printf("linhas ? ");
          scanf("%i",&linB);
          
          printf("\n\ncolunas ? ");
          scanf("%i",&colB);
          
          for(i=0; i<linB; i++)
          {
                   for(j=0; j<colB; j++)
                   {
                            printf("Digite o numero da %i linha, %i coluna : (MATRIZ B): ",i,j);
                            scanf("%i",&mB[i][j]);
                            
                            }
                 printf("\n");
                   }
          
                for(i=0; i<linB; i++)
          {
                   for(j=0; j<colB; j++)
                   {
                            printf("%i ",mB[i][j]);
                            }
                   printf("\n");
                            
                            }    
    
    system("pause >> log");
    
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int main(){    
          
          int i,j;
          int linA,colA;
          int mA[linA][colA]; // at this point linA and colA contain garbage
    Kurt

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    What include files are you including? You need at least two for your program, stdio.h and stdlib.h.

    Also, I doubt that your compiler supports the C99 standard so you should be using constants when you define your array size.


    Jim

  4. #4
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6
    Quote Originally Posted by jimblumberg View Post
    What include files are you including? You need at least two for your program, stdio.h and stdlib.h.

    Also, I doubt that your compiler supports the C99 standard so you should be using constants when you define your array size.


    Jim
    Sorry, is

    #include<stdio.h>#include<stdlib.h>

    Tell me another compiler please =D

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I use DEV C++ on Win Seven
    Quote Originally Posted by jimblumberg View Post
    Also, I doubt that your compiler supports the C99 standard ....
    GCC supported VLA's long before C99 as an extension
    Kurt

  6. #6
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6
    Quote Originally Posted by ZuK View Post
    GCC supported VLA's long before C99 as an extension
    Kurt
    thanks

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    In my opinion, when learning the language you should avoid using compiler specific hacks. I suggest in this case you either use fixed array sizes, and limit your data entry to that number of elements or learn to use dynamic memory using malloc/free.

    Jim

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I agree. And I was never happy that gcc enabled its extensions by default.
    Kurt

  9. #9
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    943
    Don't use Dev-C++, it has a very old version of GCC

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Epy View Post
    Don't use Dev-C++, it has a very old version of GCC
    Agree with Epu(metal slug i think). Go to a newer IDE

  11. #11
    Registered User mathiasbenitez's Avatar
    Join Date
    Mar 2012
    Location
    São Paulo - Brazil
    Posts
    6
    Quote Originally Posted by std10093 View Post
    Agree with Epu(metal slug i think). Go to a newer IDE
    is that the DEV-C + + in college was recommended to be in Portuguese, but I'll look for this GCC

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    Well since you are using Windows your could also investigate Microsoft Visual C++ express edition, or Code::Blocks. Both are free and more up to date than DevC++.

    Jim

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by mathiasbenitez View Post
    is that the DEV-C + + in college was recommended to be in Portuguese, but I'll look for this GCC
    In Greece too.But this is only for the very start of coding Netbeans,eclipse and Microsoft visual studio 2012 is what you could try...and of course what jim said

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays problem
    By hamidhqs in forum C Programming
    Replies: 5
    Last Post: 03-05-2012, 11:41 PM
  2. Problem with Arrays
    By dldsob in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2009, 08:43 PM
  3. problem with arrays
    By implor in forum C Programming
    Replies: 9
    Last Post: 06-01-2009, 06:12 AM
  4. Problem with arrays
    By bgavran3 in forum C++ Programming
    Replies: 12
    Last Post: 07-25-2008, 05:26 PM
  5. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 02:06 AM

Tags for this Thread


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