Thread: Error in my array code

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    38

    Error in my array code

    the duty of this code is ... it should multiply 1 column array * 1 column array and display the result

    i modified the last part of code but still doesn't give correct result
    then, last part is wrong !

    Code:
    #include <cstdlib>#include <iostream>
    #include<conio.h>
    
    
    int main()
    {
        int m1[20],i,k,m2[20],mult[20],c1,c2,n;
    
    
        
            printf("Enter 20 number for First matrix \n");
            printf("\n");
            for(i=0;i<3;i++)
            {
                
                    scanf("%d",&m1[i]);
            }
            printf("You have entered the first matrix as follows:\n");
            for(i=0;i<3;i++)
            {
                
                    printf("%d\t",m1[i]);
                printf("\n");
            }
            printf("Enter 20 numbers for Second matrix \n");
            printf("\n");
            for(i=0;i<3;i++)
            {
                
                    scanf("%d",&m2[i]);
            }
            printf("You have entered the second matrix as follows:\n");
            for(i=0;i<3;i++)
            {
                
                    printf("%d\t",m2[i]);
                printf("\n");
            }
            
            printf("Now we multiply both the above matrix \n");
            printf("The result of the multiplication is as follows:\n");
            
                                    
                        
                         
                  mult[i]=0;  
                   for(i;i<6;i++)
                   {
                                   mult[i]=m1[i]*m2[i];
                    printf("%d\n",mult[i]);
                    }
                
                printf("\n");
                
             getch();
        
        
    
    
        return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you only ran your first two arrays up to 3 elements (lines 13 and 27) ... why did you try to print 6 in line 47.
    Also you do not intialize i to any start value in that loop so it's going to start wherever you left off in the previous loop, effectively printing elements 3 to 6 of an array where elements 3 to 6 are totally uninitialized.


    Moreover you are telling the operator to enter 20 in each... but only accepting 3 (is that just for testing?).

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Quote Originally Posted by CommonTater View Post
    If you only ran your first two arrays up to 3 elements (lines 13 and 27) ... why did you try to print 6 in line 47.
    Also you do not intialize i to any start value in that loop so it's going to start wherever you left off in the previous loop, effectively printing elements 3 to 6 of an array where elements 3 to 6 are totally uninitialized.


    Moreover you are telling the operator to enter 20 in each... but only accepting 3 (is that just for testing?).
    yeah, i wanted to test
    Never mind, i solved it..

  4. #4
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    so ur multiplying two 1dimensional arrays.this is the function
    Code:
    for(i=0;i<3;i++)
     {
       mult[i]=0;
       mult[i]=m1[i]*m2[i];
     }
    for(i=0;i<3;i++)
     printf("%d",mult[i]);
    Last edited by vikasvijayan; 10-02-2011 at 11:46 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vikasvijayan View Post
    so ur multiplying two 1dimensional arrays.this is the function
    Code:
    for(i=0;i<3;i++)
     {
       mult[i]=0;
       mult[i]=m1[i]*m2[i];
     }
    for(i=0;i<3;i++)
     printf("%d",mult[i]);
    Close but no cigar...

    Code:
    for(i=0;i<3;i++)
     {
       mult[i]=0;            <--- this line does nothing.
       mult[i]=m1[i]*m2[i];
     }
    for(i=0;i<3;i++)       <--- these two loops can easily be combined.
     printf("%d",mult[i]);
    Giving...


    Code:
    for(i=0;i<3;i++)
     {
       mult[i]=m1[i]*m2[i];
       printf("%d",mult[i]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  2. i get an error after trying to run this code
    By Hanz in forum C Programming
    Replies: 6
    Last Post: 09-07-2005, 09:06 AM
  3. error with code
    By dayknight in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2004, 02:43 PM
  4. error in my code, please help
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 04:10 PM
  5. error in code
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-25-2001, 07:39 AM