-1

In this exercise he asks me to create a function Number_pos (N, pos, m) which allows to extract a number composed of m digits from position pos using functions.

Example: N = 12345, pos = 2, m = 3
Number_pos (N, pos, m) = 234

I use an Extraxt_from_position function which extracts a number from a given position, then I use a function which calculates the number of digits of the number to extract, then I have a mirror function which inverts the number and I do the successive division until the number of digits are equal to the number of digits of the number we want to extract.

The problem is: forbidden to use mirror function, can you help me

int Extract_from_position(int n, int r)
{
  int m = 0, s = 0;
  while (n != 0)
  {
    m = n % 10;
    s++;
    if (s == r)
    {
      return n;
    }
    n = n / 10;
  }
}

int Number_of_digits(int n)
{
  int m = 0, s = 0;
  while (n != 0)
  {
    m = n % 10;
    s++;
    n = n / 10;
  }
  return s;
}

int Mirror(int n)
{
  int m = 0, s = 0;
  while (n != 0)
  {
    m = n % 10;
    s = s * 10 + m;
    n = n / 10;
  }
  return s;
}

int Number_Pos(int N, int pos, int m)
{
  int x = Extract_from_position(N, pos);
  int y = 0;
  int R = Mirror(x);
  int T = Number_of_digits(R);
  while (T >= m + 1)
  {
    y = R % 10;
    R = R / 10;
    T--;
  }
  return Mirror(R);
}

int main()
{
  int n, pos, nbcx;
  printf("Give n :");
  scanf("%d", &n);
  printf("Give the position :");
  scanf("%d", &pos);
  printf("give the number of digits of the number to extract :");
  scanf("%d", &nbcx);
  printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d  \n", pos, nbcx, Number_Pos(n, pos, nbcx));
}
csavvy
  • 761
  • 4
  • 13
MED LDN
  • 684
  • 1
  • 5
  • 10

2 Answers2

2

UPDATE: Count from right
If you want count the digits from right, the NumberPos function will be just:

#include <stdio.h>
#include <math.h>

int NumberPos(int N, int pos, int m)
{
    int trc = (int)(N / (int)pow(10, pos - 1));
    trc = trc % (int)pow(10, m);
    return trc;
}

int main()
{
  int n, pos, nbcx;
  printf("Give n :");
  scanf("%d", &n);
  printf("Give the position :");
  scanf("%d", &pos);
  printf("give the number of digits of the number to extract :");
  scanf("%d", &nbcx);
  printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d  \n", pos, nbcx, NumberPos(n, pos, nbcx));
}

And the output will be, for example:

Give n :1234567 
Give the position :3
give the number of digits of the number to extract :2

The result after the amber extract from position 3 on the right and the number of digits 2 is : 45 

OLD: This could be a solution (in basically 4 line):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int NumberPos(int N, int pos, int m)
{
    int digit = floor(log10(abs(N))) + 1;
    int trc = N % (int)pow(10, digit - pos + 1);
    digit = floor(log10(abs(trc))) + 1;
    trc = (int)(trc / (int)pow(10, digit - m));
    return trc;
}

int main()
{
  int n, pos, nbcx;
  printf("Give n :");
  scanf("%d", &n);
  printf("Give the position :");
  scanf("%d", &pos);
  printf("give the number of digits of the number to extract :");
  scanf("%d", &nbcx);
  printf("\nThe result after the amber extract from position %d on the right and the number of digits %d is : %d  \n", pos, nbcx, NumberPos(n, pos, nbcx));
}

The output will be:

Give n :12345
Give the position :2
give the number of digits of the number to extract :2

The result after the amber extract from position 2 on the right and the number of digits 2 is : 23 

UPDATE: Library restriction
If for whatever reason you are not allowed to use math.h or stdlib.h you can:

lorenzozane
  • 1,214
  • 1
  • 5
  • 15
  • the code is not working correctly ... Give n: 12345 Give the position: 2 give the number of digits of the number to extract: 2 The result after the amber extract from position 2 on the right and the number of digits 2 is: 23 – MED LDN Nov 17 '20 at 11:40
  • @MEDLDN Wait sorry what's the problem? The 2 digits of 12345 from the 2 position are 23.. that's correct. I misunderstand the question or some parameter? – lorenzozane Nov 17 '20 at 11:48
  • The 2 digits of 12345 from the 2 position are 34 ,you start count position from the right to the left and extract 2 numbers also from the right to the left – MED LDN Nov 17 '20 at 11:53
  • @MEDLDN Absolutely not, and the results that this code provide are exactly the one provided by the code you mark as answer. The only thing that could create misunderstanding is whether to start counting the first digit with index 0 or index 1. I am counting from index 1. Sorry but I really can't see the problem, if there is – lorenzozane Nov 17 '20 at 11:57
  • 1
    Ah, mine sorta works by accident then. Based only on the example I assumed it was a 1-based count from the left to find the start of extraction and then extract X digits from there. – Retired Ninja Nov 17 '20 at 11:57
  • @RetiredNinja well your code and mine provide the same results.. so, you got the problem? – lorenzozane Nov 17 '20 at 11:58
  • My *guess* based on the single sample given was pos was 1-based from the left. If it is supposed to be from the right then the concept stays the same but the math would be a bit different. The problem description is vague and open to interpretation. – Retired Ninja Nov 17 '20 at 12:01
  • @RetiredNinja I have absolutely interpreted the request like you. The problem then is that I don't understand how your output can be interpreted as correct "by accident". – lorenzozane Nov 17 '20 at 12:04
  • Bad choice of words, sorry. It *works* because nobody noticed the numbers I used for `pos` wouldn't produce the same output if you're supposed to count from the right. I assume that for this result counting from the right that `pos` would need to be 4 since I added 2 digits to the number I used for testing. `Num: 1234567, Pos: 2, Digits: 3 - Result: 234` – Retired Ninja Nov 17 '20 at 12:10
  • @MEDLDN well, I have updated the answer with a solution that count the position from right and not from left. This also semplify the code. – lorenzozane Nov 17 '20 at 12:25
1

Something like this might work. I trim the digits you don't want on the right then mod to mask off the digits on the left you don't want.

Based on your sample I assume that pos is 1-based. If not there's a comment on the code you would need to remove.

You'd probably want to add error checking to make sure that pos and num_digits are valid for the given N, but that's an exercise for you.

#include <stdio.h>

int Number_of_digits(int n)
{
    int count = 0;
    while (n != 0)
    {
        n = n / 10;
        ++count;
    }
    return count;
}

int Number_Pos(int N, int pos, int num_digits)
{
    int len = Number_of_digits(N);
    pos -= 1; //pos is 1 based.

    //trim right side
    for (int i = 0; i < len - num_digits - pos; ++i)
    {
        N /= 10;
    }

    //calculate mod to keep num_digits.
    int m = 10;
    for (int i = 0; i < num_digits - 1; ++i)
    {
        m *= 10;
    }

    return N % m;
}

int main()
{
    int n = 1234567;
    int pos = 2;
    int num_digits = 3;
    int result = Number_Pos(n, pos, num_digits);
    printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);

    pos = 3;
    num_digits = 4;
    result = Number_Pos(n, pos, num_digits);
    printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);

    pos = 1;
    num_digits = 4;
    result = Number_Pos(n, pos, num_digits);
    printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);

    pos = 6;
    num_digits = 2;
    result = Number_Pos(n, pos, num_digits);
    printf("Num: %d, Pos: %d, Digits: %d - Result: %d\n", n, pos, num_digits, result);

    return 0;
}

Output:

Num: 1234567, Pos: 2, Digits: 3 - Result: 234
Num: 1234567, Pos: 3, Digits: 4 - Result: 3456
Num: 1234567, Pos: 1, Digits: 4 - Result: 1234
Num: 1234567, Pos: 6, Digits: 2 - Result: 67
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35