1

I need to create a random array in C, find nonnegative values in it, and when there are at least 2 in a row, reverse them. For example, if I have the random array 5 6 -7 -8 9 -4 7 8 2 -2 I need to get 6 5 -7 -8 9 -4 2 8 7 -2. This is what I've tried so far:

    #include <stdio.h>
#include <stdlib.h>
 int ARRAY[100];
int main(void) {
 int i;
 int nn; 
 int temp = ARRAY[i];
 rand();
 
 for (i=0; i<100; ARRAY[i++]=rand()%100-50 );

 printf("FIRST ARRAY:\n");
 for (i=0; i<100; i++)
    printf("%3d ",ARRAY[i]);
 putchar('\n');
 putchar('\n');



for(i=0; i<100; i++){
    if (ARRAY[i]<0) {
        if(!nn) {
            nn = 1;

        }
    }

        else {
            temp=ARRAY[i];
            ARRAY[i] = ARRAY[nn - 1];
            ARRAY[nn - 1] = temp;


            }
    }







 printf("Result:\n");

 putchar('\n');
 for (i=0; i<100; printf("%3d ",ARRAY[i++]));
 putchar('\n');
 return 0;
}

But had no luck with it.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Rexepo LT
  • 15
  • 4

1 Answers1

2

For starters there is no any need to declare the array globally

int ARRAY[100];

Also never use magic numbers like 100. Use named constants.

And it is a bad style of programming to name variables except constants with all upper case letters.

You can write a separate function as it is shown in the demonstrative program below.

#include <stdio.h>

void reverse_non_negative( int a[], size_t n )
{
    for (size_t i = 0; i < n; )
    {
        while (i < n && a[i] < 0) i++;

        size_t j = i;

        while (i < n && !( a[i] < 0 )) i++;

        for (size_t k = 0; k < ( i - j ) / 2; k++)
        {
            int tmp = a[j + k];
            a[j + k] = a[i - k - 1];
            a[i - k - 1] = tmp;
        }
    }
}

int main()
{
    int a[] = { 5, 6, -7, -8, 9, -4, 7, 8, 2, -2 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for (size_t i = 0; i < N; i++)
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    reverse_non_negative( a, N );

    for (size_t i = 0; i < N; i++)
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

The program output is

5 6 -7 -8 9 -4 7 8 2 -2
6 5 -7 -8 9 -4 2 8 7 -2

A more general approach can look the following way

#include <stdio.h>

void reverse_sub_ranges( int a[], size_t n, int predicate( int ) )
{
    for (size_t i = 0; i < n; )
    {
        while (i < n && !predicate( a[i] )) i++;

        size_t j = i;

        while (i < n && predicate( a[i] )) i++;

        for (size_t k = 0; k < ( i - j ) / 2; k++)
        {
            int tmp = a[j + k];
            a[j + k] = a[i - k - 1];
            a[i - k - 1] = tmp;
        }
    }
}

int non_negative( int value )
{
    return !( value < 0 );
}

int main()
{
    int a[] = { 5, 6, -7, -8, 9, -4, 7, 8, 2, -2 };
    const size_t N = sizeof( a ) / sizeof( *a );

    for (size_t i = 0; i < N; i++)
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );

    reverse_sub_ranges( a, N, non_negative );

    for (size_t i = 0; i < N; i++)
    {
        printf( "%d ", a[i] );
    }
    putchar( '\n' );
}

The program output is the same as shown above that is

5 6 -7 -8 9 -4 7 8 2 -2
6 5 -7 -8 9 -4 2 8 7 -2
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for that, but I think it's a little bit too complex for what I need to do. I need to make it as simple as possible, even though you probably think that is simple enough. Thanks anyway. – Rexepo LT Apr 18 '21 at 17:32
  • @RexepoLT You are right. I think there is nothing complex.:) I only moved the appropriate code in a function. – Vlad from Moscow Apr 18 '21 at 17:33
  • Yeah I understand that, but I need to make this task without functions, just a simple "for" cycle. But this might help, thank you. – Rexepo LT Apr 18 '21 at 17:36
  • @RexepoLT No problem. Move the code from the function in main only instead of the variable n use the variable N. In any case you can not do the task using only one for loop because you need to reverse a sub-range using one more loop. – Vlad from Moscow Apr 18 '21 at 17:37