1

I'm trying to sort an array so that the values are ordered from largest to smallest.

Then I want to modify the values so that no two values are equal, by subtracting 1 from the higher index value and adding 1 to the lower index value.

I've managed to order the values how I want, but I'm stuck on how to modify the array values so that no two are equal. How should I proceed with this problem?

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

/*declare variables*/
int s, t, c, l, e;
int RawVals[5];

/*this sorts an input array from largest to smallest*/
int cmpfunc (const void *a, const void *b)
{
  return (*(int *) b - *(int *) a);
}

/* DiePyramid Generator */
int main ()
{
/*Value inputs are taken here*/
  printf ("Enter Skill Level, "), scanf ("%d", &s);
  printf ("Enter Applied Tags, "), scanf ("%d", &t);
  printf ("Enter characteristic Value, "), scanf ("%d", &c);
  printf ("Enter Enhanced Tags, "), scanf ("%d", &e);
  printf ("Enter Legendary Tags, "), scanf ("%d", &l);

/*These inputs are then put into the RawVals array*/
  RawVals[0] = s;
  RawVals[1] = t;
  RawVals[2] = c;
  RawVals[3] = e;
  RawVals[4] = l;

/*Print RawVals before sorting*/
  printf("\n");
  printf("Entered Array: ");
  for (int i=0;i<5;i++){
    printf("%d ", RawVals[i]);
  }

/*This function then takes the RawVals array, and sorts it using cmpfunc*/
  qsort (RawVals, 5, sizeof (int), cmpfunc);

/*Add in some spacing between array outputs*/
  printf("\n");
  printf("\n");

/*This prints out the values in the RawVals array after sorting*/
  printf(" Sorted Array: ");
  for (int i=0; i<5; i++){
      printf ("%d ", RawVals[i]);
    }

/*Pyramid Forming Function*/    
  for (int i=0;i<5;i++){
    int j = 0;
    int k = 1;
    for (int p=0;p<5;p++){
      if (RawVals[j] >= RawVals[k]){
        if (RawVals[j] > 0){
          RawVals[j]++;
          RawVals[k]--;
        }
      }
    j++;
    k++;
    }
  }

/*Print out the modified values that now form the pyramid*/
  printf("\n");
  printf("\n");
  printf(" Modded Array: ");
    for (int i=0; i<5; i++){
        printf ("%d ", RawVals[i]);
      }
}

Using the above,

an input of 1 2 2 4 5 should give me 5 4 3 2 0

the actual output is 10 4 -3 7 -4

1
  • 2
    In the section /*Pyramid Forming Function*/ variable int k = 1 can index out of range on the last i loop iteration, and access RawVals[5]. So you have undefined behaviour. Commented Jan 17, 2019 at 23:50

1 Answer 1

1

As Weather Vane pointed out, k is going one beyond the end.

Also, your outer if condition is incorrect. It should be == and not >=

Here's the corrected code.

Note the change in the for loop for p to prevent k from going too high (i.e. it should do only 4 iterations and not 5)

/*Pyramid Forming Function*/
for (int i = 0; i < 5; i++) {
    int j = 0;
    int k = 1;

    for (int p = 1; p < 5; p++) {
        if (RawVals[j] == RawVals[k]) {
            if (RawVals[j] > 0) {
                RawVals[j]++;
                RawVals[k]--;
            }
        }
        j++;
        k++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.