1. ## Function

This program reads an integer n of M digits (defined at the beginning) and a one-digit integer d; if d is in n it has to print n and and the sum (+) symbol below every occurance of d.
It compiles, and works fine in the case M=1,2 and 3, but it starts doing funny things when M=4 and up...

Thanks!

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M 4

int procesar(int n, int d);

int procesar(int n, int d)
{
int esta=0;
int i,aux;

for(i=0;i<M;i++)
{
aux=n/pow(10,M-(i+1));
if(aux==d)
{
esta=1;
printf("+");
}
else
printf(" ");
n=n-(aux*pow(10,M-(i+1)));
}
return esta;
}

int main()
{
int n,d;

scanf("%d %d",&n,&d);
printf("%d\n",procesar(n,d));

system("PAUSE");
return 0;
}```

2. It would help quite a bit if you describe the difference betwen what you expect and what you actually get (what is "funny").

--
Mats

3. you do not need pow to extrct digits from number
enough % and /

4. Originally Posted by vart
you do not need pow to extrct digits from number
enough % and /
Ok, but it should work too that way.

In the case M=4, this is what happens:

Code:
```1234 4
0```
or

Code:
```1244 4
+ 1```

I´m aware that the "0" and "1" that appears is the return that is in the program (which by the way I don´t know how to get rid of...) but the last 4 shold have a "+" below...

5. My guess would be that your float to integer calculation gets rounding errors, and you are not getting the results you expect.

I added a printf to show the value of n after the "n = n - ..", and it shows this:
Code:
```1234 4
n = 233
n = 33
n = 3
n = 0
0```
I then modified the code to store the result of pow() and it changes the output... So you are definitely suffering from some sort of rounding problems.

Using / and % operators is a much safer method for this type of math.

--
Mats

6. Surely we've figured out what printf is by now.... If you don't want something to print, don't printf it.

And your example is incorrect: input of 1234 4 gives
Code:
```1234 4
+1```
which is what you wanted, so I'm not sure what your problem is.

7. Originally Posted by tabstop
Surely we've figured out what printf is by now.... If you don't want something to print, don't printf it.

And your example is incorrect: input of 1234 4 gives
Code:
```1234 4
+1```
which is what you wanted, so I'm not sure what your problem is.
Actually, I can reproduce the exact results of the original poster - however, I think it may well depend on the compiler & settings. gcc-mingw (3.4.x) that is distributed with dev-cpp seems to reproduce the problem, as stated above - that is, with no extra options - adding -O3, and it "works", but -O1 -ffast-math doesn't, for example.

--
Mats

8. Originally Posted by matsp
Actually, I can reproduce the exact results of the original poster - however, I think it may well depend on the compiler & settings. gcc-mingw (3.4.x) that is distributed with dev-cpp seems to reproduce the problem, as stated above - that is, with no extra options - adding -O3, and it "works", but -O1 -ffast-math doesn't, for example.

--
Mats
Hey, you're right. (I have -O3 set in my IDE, which is why it didn't occur to me to go without. Take it away and bad things happen.)

9. matsp:

Could you give me an example of how it should be with % and /??

So, it is wrong or what?? Now I´m getting confused....

10. Using pow for extracting digits is like using a sledgehammer to swat a fly. Whilst initially that only seems like overkill, it is so much so that it's actually become the opposite in this case. (You'd fail to kill flies with it very effectively at all).

Or if you prefer a different metaphor, you're walking around the block to get next door.

I'm sure you get the picture. Anyway to use / and %:
Using % N on a positive number will give you the remainder if you divided that number by 10. So for example 2368 % 10 gives you 8.
Now, can you spot what operation you can perform on 2368 to move the digits along by one digit, such that you can use % 10 again to get out the 6?
Now, keep doing those two things until... you have zero left.

Simple huh!