Colloquium Exercise - STUCK!

The exercise:

Write a function that takes 3 arguments. The function has to bring back a new number that has been generated with the replacement of the figure that is on a given position in the number with a figure that is been transferred as an argument(have in mind that the position of the figure is being counted from right to left,starting from one). Write a main program in which the newly formed numbers will be printed for numbers of a range written by the user.

example: if you wrote the numbers 2276,3 and 5 the function should bring back the number 2576

If you didn't understand the text, the example shows that in the number 2276, the number has been counted from right to left by the second argument "3" and in the place of the figure "2" has been put the figure "5".

This is where I got stuck, I can't figure out how to make the replacement. Any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int argument(int x,int y,int z)
{
    return 0;
}

int main()
{
    int a,b,c;
    printf("Enter a value for a(100-999):");
    scanf("%d",&a);
    printf("Enter a value for b(1-3):");
    scanf("%d",&b);
    printf("Enter a value for c(1-9):");
    scanf("%d",&c);

    argument(a,b,c);
    return 0;

}


Thanks for the help in advance. :)

Edit: If you have trouble understanding the exercise please do say, because I might have some translating errors. Cheers!
Last edited on
You are suppose to find the value of the digit at that (y) position and subtract it from x, then add z * 10 ^ y

To get the digits you could do something like:
1
2
3
4
5
digit3 = num / 100;
num -= digit3;
digit2 = num / 10;
num -= digit2;
...
There are two ways you could do this. First, you could convert the number to a string, replace the digit in the string, and convert back to a number. Second, you could do it mathematically. I'd do it mathematically since it will run much faster.

To do that, work out how you'd do it by hand. Then convert your algorithm into code. Here's a hint: do it in two steps.
Step 1: replace the nth digit with 0
Step 2: replace the nth digit with the number you want.

Figure out how to do it this way and then see if you can reduce the number of steps.
Well I found a solution, a friend of mine helped me out and we took some of your ideas into consideration.

"Two heads are better then one."

Thank you again. :)

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <stdlib.h>

int new_numb(int n,int a,int b)
{
    int arg,i,arg1=1,digit;
    arg=n;

    for(i=1;i<=a;i++)
    {
        n=n/10;
    }
    n=n*10+b;

    for(i=1;i<a;i++)
    {
        digit=arg%10;
        arg1=arg1*10+digit;
        arg=arg/10;
    }

    digit=0;
    for(i=1;i<a;i++)
    {
        digit=arg1%10;
        n=n*10+digit;
        arg1=arg1/10;
    }
    return n;
}

int main ()
{
    int n,a,b,numb;
    printf("Enter 3 digits:\n");
    scanf("%d %d %d",&n,&a,&b);
    numb=new_numb(n,a,b);
    printf("%d",numb);

    return 0;
}
Topic archived. No new replies allowed.