0

I have 5 integer variables named x_1, x_2, x_3, x_4, x_5.

int x_1, x_2, x_3, x_4, x_5;

I'm initializing those variables' values with srand().

srand(time(NULL));
x_1=rand()%10+1; x_2=rand()%10+1;
...

I need to check whether those variables contain a sequence of ascending numbers (like a straight in poker). Order doesn't matter.

For example:

x_1=3, x_2=5, x_3=1, x_4=2 x_5=4.

This should give me 1. But this should give me 0:

x_1=4, x_2=1, x_3=4 …

(I didn't complete the list because I have multiple 4's already.)

NOTE: I can't Use Arrays and Global variables.

Note: I'm sorry that I can't send full code because I don't even know where to start.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
jongspiration
  • 13
  • 1
  • 4
  • 4
    What exactly does "following each other" mean? Are you checking for duplicates? – dbush Apr 04 '19 at 16:52
  • Have you come across 'arrays'? Your 5 variables should be replaced by a single array. Are you asking about whether the set of numbers in 5 variables (in the array) for a simple sequence if appropriately sorted? Does the sequence have to start at 1 or would [4, 5, 6, 7, 8] be an acceptable sequence? (I understand that the order of the values doesn't matter — but sorting and scanning will be an easy way to spot issues.) – Jonathan Leffler Apr 04 '19 at 17:01
  • 1
    I feel like barring off the use of arrays makes this problem a lot more annoying to work out – Patrick Apr 04 '19 at 17:02
  • By following each other do you mean it simply cant have duplicates or it has to have every integer in the 5 number range (1,2,4,5,6 returns 0)? – Patrick Apr 04 '19 at 17:12
  • @Patrick it can't have duplicates, they have to follow each other not have to be in range. i mean when you order them they have to be (2,3,4,5,6) or (1,2,3,4,5) like this. – jongspiration Apr 04 '19 at 17:23
  • @JonathanLeffler i wish i could use arrays but cannot. the sequence doesn't have to start at 1. the example you gave is also acceptable. – jongspiration Apr 04 '19 at 17:24
  • There are questions about sorting N separate (non-array) variables on SO, which may help you simplify your processing (sort, then check for consecutive numbers in consecutive variables). A recent question was [What is the most efficient way for sorting 3 values using `if` `else`?](https://stackoverflow.com/questions/55364204). I have vaguely relevant code available in my [SOQ](https://github.com/jleffler/soq) (Stack Overflow Questions) repository on GitHub as files `st13.c` and `s6v23.c` etc in the [src/so-4203-5818](https://github.com/jleffler/soq/tree/master/src/so-4203-5818) sub-directory. – Jonathan Leffler Apr 04 '19 at 17:31

1 Answers1

0

From what I have seen you can't have duplicates.
Can start from any number. Numbers can be in any order.
If sorted numbers create secuence. Example: 1,2,3,4,5 or 4,5,6,7,8.
If that's the task given, then:

  1. Check duplicates:

    if(count_how_many_exist(x_1, x_1, x_2...) != 1){//error duplicates} if(count_(x_2,x_1,x_2,...)... //Function code int count_(int search,...){ int result =0; if(search == x_1)result += 1; //... return result; }

  2. Search 4 times if the number exists, starting from the smallest one.

    int small = x_1; if(small > x_2)small = x_2; //... if(count_(small+1, x_1,...) != 1)//error if(count_(small+2... ... if(count_(small+4...