-7

Given an array of ints, return 1 if every element is a 1 or a 2, otherwise return 0;

only12([1,2,2]) -> 1
only12([1,4,2,4]) -> 0
only12([2,1]) -> 1
only12([1,2,12]) -> 0
only12([]) -> 1
only12(NULL) -> 0

prototype:

int only12(const int nums[]);

Any advice? Thanks!

updated answer from HiEd : The result is 1 even the array includes 3.

// answered by HiEd
#include <stdio.h>
int only12(const int nums[])
{
    int size = sizeof(nums)/sizeof(nums[0]);
    int i;
    int result = 1;
    for(i = 0; i < size; i++)
    {
       if(nums[i] != 1 && nums[i]!=2 )
       {
          result = 0;
       }
    }
    //printf("%d",result);
    return result;
}
int main()
{
    const int nums[5]= {1,2,3,1,1};
    int r = only12(nums);
    printf("%d",r);
}

After reading Gerhardh's comment, I decided to give value for size in the function, then the program runs. but still did not get this part:

only12([]) -> 1

this is the code updated:

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

int only12(const int nums[]);

int only12(const int nums[])
{
    size_t i, size = 5;
    int result = 1;

    for(i = 0; i < size; i++)
    {
       if(nums[i] != 1 && nums[i] != 2 )
       {
          result = 0;
       }
    }

    return result;
}

int main()
{
    const int nums[5]= {1,2,2,1,3};

    printf("%d", only12(nums));

    return 0;
}
  • You can use a loop that iterates over the entire array and checks if each of the array elements is 1 or 2. You can `return 0` if you encounter an element that's neither 1 nor 2 and `return 1` after the loop has iterated over the entire array since that means all of the elements in the array is either 1 or 2. – JASLP doesn't support the IES Jul 31 '21 at 05:21
  • You must provide the array size to your function. For arrays passed to a function you cannot use `sizeof`. See [this answer](https://stackoverflow.com/a/10349610/6782754). With the given code you will only check first 1 or 2 values, depending on size of `int` and size of pointers on your system. – Gerhardh Aug 03 '21 at 18:11

1 Answers1

0
#include <stdio.h>
int only12(const int nums[], int size){
    int i;
    int result = 1;
    for(i = 0; i < size; i++){
       if(nums[i] != 1 && nums[i]!=2 ){
           result = 0;
       }
    }
    //printf("%d",result);
    return result;
}
int main()
{
    const int nums[5]= {1,2,3,1,1};
    int size = 5;
    int r = only12(nums, size);
    printf("%d",r);
}

In the code above, we find the size of the array and iterate over it using a for loop. In the for loop, we see if nums[i] is something other than 1 or 2 and we set result to 0 if that's the case. At the end of the function, we return the result. As pointed out in the comments, using sizeof(nums)/sizeof(nums[0]); won't work, so I have passed size as a parameter.

HiEd
  • 160
  • 3
  • 2
    Unlikely to be a big issue in this case, but there's no reason the for loop should continue once a "negative" (array element is not 1 or 2) result is encountered. One need not even have a return in the loop body. You could just have the loop's test check to see if `result` is still `1`. `for (i = 0; i < size && result == 1; i++)`. – Chris Jul 31 '21 at 05:30
  • 1
    `if(nums[i] != 1 || nums[i]!=2 ){` should be `if(nums[i] != 1 && nums[i]!=2 ){` – JASLP doesn't support the IES Jul 31 '21 at 05:39