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.