Multiply 2 array's ?

This is a discussion on Multiply 2 array's ? within the C Programming forums, part of the General Programming Boards category; Hello , where am i wrong with muliply 2 array's ? Code: #include <stdio.h> #include <stdlib.h> int main(int argc, char ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Multiply 2 array's ?

    Hello , where am i wrong with muliply 2 array's ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char *argv[])
    {
      
      char egn[9];
      int check[8] = {2,4,8,5,10,9,7,3,6};
      int result[8];
      
      printf("Type your ID number: ");
      scanf("%s",&egn);
    
    
      result[0] = egn[0]*check[0];
      printf("%d\n",result);
        
      system("PAUSE");	
      return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    This is definitely not the way to do it. What are you trying to do anyway?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    can you show me how can i multiply 2 array's :
    arr[0]*arr2[0]

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Omar Do View Post
    arr[0]*arr2[0]
    Here you are multiplying the 1st value of arr with the 1st value of arr2. What's the mathematical algorithm for what you want to do?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are reading in a string and then using it to overwrite the address of your char array. Thankfully that address is local so you are really just detonating yourself in a confined environment. Only God knows what the result of that will be. My guess is a segmentation fault and a core dump.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    There's more than one way to multiply two vectors and your code gives no hint as to which one you want. You're probably looking for either the "dot product" or the "cross product". The main difference between them is that the dot product produces a scalar result (a single number) whereas the cross product produces another vector (perpendicular to the two given vectors).

    So which do you want?

    EDIT: Actually, I suppose you could want to just multiply each element by the corresponding element in the other array (a kind of nonuniform scaling) but I don't know what that's called.

    EDIT2: Now that I look into it, I'm not sure how to calculate the cross product of two nine-dimensional vectors! As I see it, it would probably take 8 of them for the concept to even be defined.
    Last edited by oogabooga; 03-16-2012 at 02:35 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Think about these questions for a moment...

    How many multiplications do you want to perform?
    How many have you asked it to do?
    How many items can an array of size 8 hold? (Hint, it's not 9)
    How many results do you want to print out?
    How many results are you trying to print currently?
    Do you know how to use loops?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    @oogabooga
    -The cross product is only properly defined in 3 and 7 dimensional space, and you need 2 and 6 vectors respectively to calculate it.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheBigH View Post
    7 dimensional space
    Wrong forum.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by quzah View Post
    Wrong forum.
    Don't care.
    Code:
    while(!asleep) {
       sheep++;
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheBigH View Post
    Don't care.
    I guess it was a bit much to expect you to see the humor in that.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    assuming the input for scanf is 01234567, you would want result[0] = 2 * 0? from the code you posted, you would get result[0] = 2 * 48, 48 being the ascii value for '0', so i think you want: result[0] = egn[0]*(check[0] - 48); if this is different from what you wanted please be a bit more specific and possibly provide a sample input for check[] and the ending values for result[] that you would want.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    You are asking to multiply 2 arrays.

    What comes to mind is this:
    Code:
    int array1[4] = {1,2,3,4}
    int array2[4] = {5,6,7,8}
    
    int result[4] = {5,12,21,32}
    As for what your attached code is doing, it is multiplying user char values with int values. Generally speaking this is a horrible idea because what is A*6? In computer terms it is 260 (because of ASCII characters),

    What about 1*4 when 1 is a char value? It is 196!

    Edit: So how did Calculus 3 get involved in this again? :P
    Last edited by JonathanS; 03-16-2012 at 09:30 PM.
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiply using Bitwise
    By ganesh bala in forum C Programming
    Replies: 3
    Last Post: 01-30-2009, 12:35 AM
  2. multiply by 7
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 05-22-2006, 03:19 PM
  3. multiply connections
    By piotrek_no_1 in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-13-2005, 12:38 PM
  4. Multiply it by 10 then Int to Hex
    By Mr. Acclude in forum C Programming
    Replies: 16
    Last Post: 09-23-2004, 09:15 PM
  5. multiply int..
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-14-2002, 09:23 AM

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