Decrementing an array and print a value if condition met

1.6k views Asked by At

I want to decrement a pointer to the last element of an array and check for the condition that if the value of the pointer was smaller than 5, do nothing and go to the next round of loop and if not, that is if the value is equal or bigger than 5, print the value. So, for the array in the example, I want only 6 to be printed, that is the first encounter of a value equal or bigger than 5. I tried the code below but while being compiled with no error, it doesn't print any value. I'd appreciate your comments on this.

#include<stdio.h>

//The is a C program to decrement an array from the last element to the first. 
    int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2}; 
    int *lastElem, count;
int main (void) {
    lastElem = &x[10];
    for (count = 0; count < 11; count++)
        if (abs(*lastElem) < 5)
        continue;
        else 
        printf("%d\n", *lastElem--); 
    return 0;
}
4

There are 4 answers

5
Sourav Ghosh On BEST ANSWER

There is a problem in your decrement logic. If the value is less than 5, you're missing the decremet.

check the below code.

#include<stdio.h>

//The is a C programme to decrement an array from the last element to the first. 
    int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2};
    int *lastElem, count;
int main (void) {
    lastElem = &x[10];
    for (count = 0; count < 11; count++)
    {
        if (*lastElem >= 5)
            {
            printf("%d\n", *lastElem);
             break ;
             }
        lastElem--;
    }
return 0;
}

EDIT:

As you modified your question to include the absolute value comparions, the changes are like below.

Note : When making some major changes [which will change the behaviour and output] to the original question asked, do make a note of that, and if possible, mention the edit properly.

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

//The is a C programme to decrement an array from the last element to the first. 
int x[11] = {5, -6, -4, -3, -2, -1, 4, 3, 2, 1, -2};
int *lastElem, count;
int main (void) {
        lastElem = &x[10];
        for (count = 0; count < 11; count++)
        {
                if ( abs(*lastElem) >= 5)
                {
                        printf("%d\n", *lastElem);
                        break;
                }
                lastElem--;
        }
        return 0;
}
0
Philipp Murry On

You never assign another address to lastElem than the inital value (that is the 10th element). Also, if you want to go backwards, set count to 10 and let it count to 0. In each loop you have to assign &x[count] to lastElem (or decrement it, as it is a pointer and the address will be decremented according to the object it points to).

11
Iharob Al Asimi On

The line printf("%d\n", *lastElem--); is not always executed it is only executed when abs(*lastElem) >= 5. This is how you should do it

#include<stdio.h>

//The is a C programme to decrement an array from the last element to the first. 
int x[11] = {5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2}; 
int *lastElem, count, value;
int main (void) 
{
    lastElem = &x[10];
    for (count = 0; count < 11; count++)
    {
        value = *(lastElem--);
        if (value < 5)
            continue;
        printf("%d\n", value);
        break;
    }
    return 0;
}

This will do it with continue.

0
Vlad from Moscow On

Here is the only correct code among presented here in other answers that does the task.:)

In fact you need a program that searches backward an alement of an array that satisfies a given condition. And if there is such an element then to output it.

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

int main( void ) 
{
    int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
    const size_t N = sizeof( a ) / sizeof( *a );
    int *first = a, *last = a + N;

    while ( first != last && ( unsigned int )abs( *( last - 1 ) ) < 5 ) --last;

    if ( first != last ) printf( "%d\n", *--last );

    return 0;
}

The output is

6

Below there is demonstrated a general approach for such tasks

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

int * find_backward( const int a[], size_t n, int ( *predicate )( int ) )
{
    const int *first = a, *last = a + n;

    while ( first != last && !predicate( *( last -1 ) ) ) --last;

    return ( int * )last;
}

int is_greater_or_equal_to_5( int x )
{
    return 5 <= ( unsigned int )abs( x );
}

int main( void ) 
{
    int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
    const size_t N = sizeof( a ) / sizeof( *a );


    int *target = find_backward( a, N, is_greater_or_equal_to_5 );  

    if ( target != a ) printf( "%d\n", *--target );

    return 0;
}

Using this approach you can use any integer arrays (even with zero size) and any conditions that are set by means of predicates.

For example if you want to find the last element of an array that is divisible by 3 you can write

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

int * find_backward( const int a[], size_t n, int ( *predicate )( int ) )
{
    const int *first = a, *last = a + n;

    while ( first != last && !predicate( *( last -1 ) ) ) --last;

    return ( int * )last;
}

int divisible_by_3( int x )
{
    return x % 3 == 0;
}

int main( void ) 
{
    int a[] = { 5, 6, -4, -3, -2, -1, 4, 3, 2, 1, -2 }; 
    const size_t N = sizeof( a ) / sizeof( *a );


    int *target = find_backward( a, N, divisible_by_3 );    

    if ( target != a ) printf( "%d\n", *--target );

    return 0;
}

The output is

3

Take into account that this function allows also to deal with constant arrays.:)