Thread: Help with calculations

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    127

    Help with calculations

    I've got ten numbers that I need to multiply by their place in the array, does anyone have any ideas of how I could go about this or any sites that might be able to assist me.

    i.e. 1234567890

    1 x 10 (1st place in the array)
    2 x 9
    3 x 8
    4 x 7
    5 x 6
    6 x 5
    7 x 4
    8 x 3
    9 x 2
    0 x 1 (10th place in the array)

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A loop, such as
    Code:
    int numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    int i;
    
    for(i = 0; i < (sizeof(numbers) / sizeof(numbers[0])); i++)
    {
        /* i is the 0-based position in the array, starting from 0 or (sizeof(numbers) / sizeof(numbers[0])) - i */
        /* numbers[i] is the value */
    }
    And similar logic for working backwards (from 0, 9, 8... 1) - so you can get it's "position".

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry I'm not seeing how this will work. Could you explain further please.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Okay, step through the array (like that loop) and multiply it's position by it's value, For example

    Code:
    int numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    int noElements = sizeof(numbers) / sizeof(numbers[0]);
    int i;
    
    for(i = 0; i < noElements; i++)
    {
        numbers[i] = numbers[i] * (noElements - i);    /* note the number array is destroyed */
        printf("&#37;d, ", numbers[i]);
    }
    
    /* output
    10, 18, 24, 28, ... etc
    */
    Or step through the array backwards.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Each number is multiplied by a position weight, the products are summed, and the sum must be a multiple of 11.

    So I have got the below to confirm that it is a multiple of 11, but I'm still not understanding how to run the the products code. Sorry it's just not sinking in.

    Code:
       if ((total_sum &#37; 11) == 0)
          validity = 1;
       else
          validity = 0;
       return validity;
    Also would it matter if say the numbers were seperated by letters and I only wanted the number position to count?

    i.e. 1a2-3a4-5 with 1 would still be multiplied by 10, 2 by 9 etc ignoring the seperation by the letters and dashes.
    Last edited by Taka; 09-01-2007 at 04:04 PM.

  6. #6
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    1 x 10 (1st place in the array)
    2 x 9
    3 x 8
    4 x 7
    5 x 6
    6 x 5
    7 x 4
    8 x 3
    9 x 2
    0 x 1 (10th place in the array)
    In the last line, did you mean 10 x 1 ?

    I've got ten numbers that I need to multiply by their place in the array
    I'm not entirely sure what you mean. Please clarify.

    FWIW, arrays start from 0 so array[0] is the first element in the array, array[1] is the second, and so on.

    Do you definitely mean to multiply? The reason I ask is because you want the result to be divisible by 11. 10+1=11, 9+2=11, 8+3=11 and so on.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry 1234567890 was only an example. It can be any sequence of numbers as long as their is only 10 digits. So it could be:

    6666666666
    1212121212
    2840439372 etc

    The user will punch in 10 digits and then what I want to happen is the code multiplys them and then add the products together with the product being divided by 11 to see if it passes the test.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So if your enter the strng something like this

    Code:
    6666666666
    1212121212
    And the multiplication should be done like this is it

    Code:
    1 x 10 (1st place in the array)
    2 x 9
    3 x 8
    4 x 7
    5 x 6
    6 x 5
    7 x 4
    8 x 3
    9 x 2
    0 x 1 (10th place in the array)
    Is that right. If you follow the above step you get ten different output

    ssharish2005

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    Sorry I'm not explaining this well.

    So a person enters a string:
    9731012865

    The program reads the string then does the calculations on it:
    (9 x 10) + (7 x 9) + (3 x 8) + (1 x 7) + (0 x 6) + (1 x 5) + (2 x 4) + (8 x 3) + (6 x 2) + (5 x 1)

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what part of this do you find difficult? The multiplying by "10-position"?
    Or the checking if the resulting number is correct?
    Or reading the number in?

    Since this smells very much like homework, I'm not going to write down a function to do this, because the board has a policy of "not doing other peoples homework". (Anyone who's passed the first few steps of programming should be able to solve this sort of problem anyways).

    --
    Mats

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    127
    It is homework and I'm not asking for the code I'm just asking for some help in directing me to the right place.

    I am having trouble with the multiplying by position of it.

    If it was the other way round (As in the 1st number was multiplied by 1)I would understand it but I am not understanding how to get it to 'count down' so to speak.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I already showed you how, See post #4

    There are 'noElements' elements (ie 10), and you start from 0 (1) and work your way to the end, it's "count down" is the noElements - it's position.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, as a hint, have you thought about how you could mathematicaly express the number you should multiply by?

    Alternatively, you could use some of the table means that have already been described. [But you certainly don't have to].

    By the way, accumulating after multiplying by zero is pretty meaningless, right? - but I would ignore that right now...

    --
    Mats

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    I already showed you how, See post #4

    There are 'noElements' elements (ie 10), and you start from 0 (1) and work your way to the end, it's "count down" is the noElements - it's position.
    Indeed.

    --
    Mats

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    34
    USE THIS LOOP

    Code:
    for(i=0;i<10;i++)
    printf("&#37;d",(10-i)*a[i]);
    this will work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  2. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM
  4. simple arithmetic calculations
    By viciousv322 in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 07:13 AM
  5. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM