-2

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;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Lucia
  • 901
  • 1
  • 11
  • 16

4 Answers4

3

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;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • OP wanted the first occurence of the number greater than 4(6 in OP's case) to be printed and not all the numbers greater than 4 – Spikatrix Dec 01 '14 at 12:43
  • @coolguy I tried to point out yhe erroneous part. However, will update the answer to accomodate that also. – Sourav Ghosh Dec 01 '14 at 12:48
  • Sorry, a question, if these numbers were float rather than integers, would the only change be in the printf statement, that is `printf(%.2f\n", *lastElem);` – Lucia Dec 01 '14 at 15:40
  • @homa that's true but there will be other side effects. `abs ()` operates only on integers, if i'm not wrong. – Sourav Ghosh Dec 01 '14 at 15:43
  • fabs() works to get the absolute value of floating numbers in c – Lucia Dec 01 '14 at 16:34
3

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).

Philipp Murry
  • 1,660
  • 9
  • 13
1

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.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

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.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335