0

I have a function repsEqual that takes an array and integer and returns 1 if the array contains only digits of the number in the same order that appear in the same number. Otherwise it returns 0.

int repsEqual(int a[], int len, int n)

If a is {3,2,0,5,3} and n is 32053 return 1 because the array contains only the digits of the number in same order as they are in the number.

If a is {0,3,2,0,5,3} and n is 32053 return 1; we can ignore leading zeros.

I tried like this

int repsEqual(int a[], int len, int n)
{

    int len = sizeof(a)/sizeof(a[0]);
    //storing elements in array
    for(int i=0;i<len;i++)
    {
        scanf("%d", &a[i]); //eg storing:3 2 0 5 3
    }

    //asking user integer number and storing in next array
    scanf("%d",&a2[num]);//eg 32053

}

Now I need to check if a2 elements are in same order as a1, but do not know how to get started.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David
  • 31
  • 1
  • 6
  • 2
    `int len = sizeof(a)/sizeof(a[0]);` is wrong, `a` is a pointer here. Plus, isn't `len` a function argument? And what is `sacanf`? – Yu Hao Apr 05 '16 at 03:34
  • 1
    your approach is absolutely incorrect according to your destination. can i give you a solution or you wanna try any more? – cjahangir Apr 05 '16 at 03:40
  • The specification of your `repsEqual()` function precludes it doing any I/O. You need code similar to what you show (but definitely not identical to it) in your `main()` function, or a function called by `main()`, but your code currently in `repsEqual()` bears no resemblance to what you said it should do. – Jonathan Leffler Apr 05 '16 at 04:00

3 Answers3

3

This is what you want

int repsEqual(int a[], int len, int n)
{
    for (int i = 0; i < len; i++)
    {
        if (a[len - i - 1] == n % 10)
            n /= 10;
        else
            return 0;       
    }

    //For cases where your number-length is longer than your array length
    if (n != 0) return 0; 

    return 1;
}

First you have your array, say like a[5] = { 5, 2, 3, 1, 4}
Basically what i do is looping the array from end to start, thats a[len - i - 1]
Then i check it with the last character of n thats n%10
So example with n = 52314, the first if statement check if (52314 % 10) which is 4 equal with a[4] which is also 4
if the 2 character match then the loop continue first by remove the last character of n: 52314 / 10 = 5231. And the next loop will check for 5231 % 10 and a[3]
else the loop break mid-way and return 0 indicate that a mis-match is found

finally after all the character in array is checked and no mismatch is found, it will return 1, as the pattern match

Note: a function should only does what its name says
In your case, check if an array and an integer have the same pattern
User input should be put outside somewhere else, after you have the inputs (the array, the len, and n) you then pass-in to repsEqual for checking

tu4n
  • 4,200
  • 6
  • 36
  • 49
0

Try matching the number (n) backwards against the array 'a'. To do this you'll want to modulus the smallest digit from 'n', by getting the remainder from dividing by 10. Then remove the smallest digit from 'n' by dividing by 10.

int repsEqual(int a[], int len, int n)
{
  int i;
  int temp;
  if (0 == len || NULL == a)
    return 0;  // no array, or elements, doesn't match a real number (n).

  temp = n;
  for (i = len - 1; i >= 0; --i)
  {
    if (a[i] != (temp % 10))
      return 0;  // remainder mismatch against array element.

    temp = temp / 10; // removes the smallest digit.
  }
  return 1;
}

By modulus 10 on your n you get the remainder of dividing by 10. IE 452 % 10 = 2. Then by dividing be ten we remove the smallest digit IE 452 / 10 = 45.

amisam
  • 91
  • 6
  • @JonathanLeffler Thanks, guess I missed it. But I'm missing the final check against 'n' as nmtuan has above so his answer is preferable to mine anyway. – amisam Apr 05 '16 at 04:20
  • Yeah, I see that. Given data: `int a5[] = {2, 0, 5, 3}; enum { A5_SIZE = sizeof(a5) / sizeof(a5[0]) }; int n5 = 32053;`, this code incorrectly returns 1. The values in `a5` represent the last 4 digits of the number in `n5`. – Jonathan Leffler Apr 05 '16 at 04:34
  • This code doesn't ignore leading zeros in the array. – Jon Anderson Apr 05 '16 at 16:09
  • @JonAnderson sure it does :-) it just doesn't check for remaining digits in the int after the array has finished being processed. – amisam Apr 07 '16 at 06:06
  • @amisam Oops. You're right. – Jon Anderson Apr 07 '16 at 21:40
0

This seems to be some homework, haha. Anyway I gave u a quick/ugly sample to start with.

#include <stdio.h>

int repsEqual(int a[],int len , int n)
{
    char str[100];
    sprintf(str, "%d", n);
    int i;
    int nonzeroIndex;
    for(i=0; i<len; i++){
        if (a[i] != 0)
            break;
    }
    nonzeroIndex = i;
    printf("nonzeroIndex is %d\n", nonzeroIndex);
    for(i= nonzeroIndex; i <len; i++){
        if (a[i] !=  str[i - nonzeroIndex] - 48) {
            printf("diff at %d\n", i);
            return 0;
        }
    }
    return 1;
}

int main()
{
    int a[5];
    a[0] = 0;
    a[1] = 2;
    a[2] = 0;
    a[3] = 5;
    a[4] = 3;
    int output = repsEqual(a, 5, 2053);
    printf("result: %d\n", output);
}
gpliu
  • 229
  • 3
  • 14