# Thread: Help with calculations

1. ## 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. 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. Sorry I'm not seeing how this will work. Could you explain further please.

4. 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. 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.

6. 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. 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. 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. 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. 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. 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. 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. 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. Originally Posted by zacs7
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. USE THIS LOOP

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

Popular pages Recent additions