| parham93 (2) | |
|
i want to write a function that would get a copy of an array and go through it to see which of the below conditions is satisfied by that array. function prototype is this : unsigned g( const unsigned a[], unsigned elements ) . g returns a code in the range [0, 8) according to the data in a 0: elements < 2, so no element has a predecessor. e.g. {} e.g. {1234} 1: elements >= 2, and every element is < its predecessor. e.g. {10, 9, 2} 2: elements >= 2, and all the elements are equal. e.g. {8, 8, 8, 8} 3: at least one element is < its predecessor, at least one element is == its predecessor, but no element is > its predecessor. e.g. {10, 10, 8, 8, 5, 5, 5, 5, 3} 4: elements >= 2, and every element is > its predecessor. e.g. {2, 4, 8, 16} 5: at least one element is < its predecessor, at least one element is > its predecessor, but no element is == its predecessor. e.g. {3, 2, 3, 4} 6: at least one element is == its predecessor, at least one element is > its predecessor, but no elements is < its predecessor. e.g. {5, 5, 10, 20} 7: at least one element is < its predecessor, at least one element is == its predecessor, and at least one element is > its predecessor. e.g. {5, 4, 5, 5} If I haven’t made a mistake, you should be able to convince yourself that every array will fall into exactly one of these 8 categories. g’s job is to return the number of that category. For instance, g( {5, 4, 5, 5} should return 7 Comments: i am confused and don't know how to proceed, i would appreciate it if anyone explain where should i start. | |
|
|
|
| vlad from moscow (3660) | ||
If you want it then do it!:) | ||
|
|
||
| JLBorges (1754) | |||
This will print out the counts of how many elements are less than, equal to or greater than their immediate predecessor:
Take it up from there. | |||
|
|
|||
| vlad from moscow (3660) | ||||
|
The same as above but using standard algorithms. Also I changed the original array int a[] = { 10, 10, 8, 8, 5, 5, 5, 5, 1, 3 } ;
The output is
If the code is not compiled with MS VC++ then remove the enum and substitute its enumerators for magic numbers 0, 1, and 2 correspondingly.:) | ||||
|
Last edited on
|
||||
| vlad from moscow (3660) | ||||
So taking into account the previous code you can easy write the general function
| ||||
|
Last edited on
|
||||