Home > Mobile >  Sorting out Array of several integers, numbers in array seems to have shifted by 1
Sorting out Array of several integers, numbers in array seems to have shifted by 1

Time:06-26

I'm trying to solve a question to find the lowest and highest numbers in an array in C Language. I tried swapping the numbers that are close to each other to allign the numbers from small(left) to big(right). For example, if the array is 20, 10, 35, 30, 7, first compare 20 and 10, if 20 is larger than 10, swap the numbers to 10, 20. then compare 20 and 35, 20 is smaller than 35, so go on. then compare 35 and 30, 35 is bigger than 30, swap numbers to 30, 35. then compare 35 and 7, 35 is bigger than 7, swap numbers to 7, 35. Did these 'swappings' again 3 more times to align the numbers perfectly. After I've done the swappings, I just printed the first array number and the last array number, but the numbers aren't correct, and it looks like the numbers have shifted by 1. For example, if I align the above array, it is 7[0], 10[1], 20[2], 30[3], 35[4]. (marked the indices by []) So, when I print the indice[0], and indice[4], I expected the numbers to show 7 and 35. But in fact I have to print indice[1], and indice[5] to get the numbers 7 and 35 to be printed. The numbers seem to have shifted by 1..

I really want to know why the numbers have shifted by 1 in the array.

Thank you for reviewing the question.

I'll also post the original question that I'm trying to solve. "Q. Input the number N first to decide how much numbers to enter, then input the N-numbers. Print the lowest and highest number in the N-numbers you have input."

And here's my code..

#include<stdio.h>
#pragma warning(disable:4996)
int main(void)
{
    int input, i, j, temp, k;
    int value[100] = { 0 };
    scanf("%d", &input);
    for (i = 0; i < input; i  )
    {
        scanf("%d", &value[i]);
    }
    for (k = 0; k < input; k  )
    {
        for (j = 0; j < input; j  )
        {
            if (value[j] > value[j   1])
            {
                temp = value[j   1];
                value[j   1] = value[j];
                value[j] = temp;
            }
        }
    }
    printf("%d %d\n", value[0], value[input-1]);
    return 0;
}

CodePudding user response:

Because you're iterating over the whole array, value[j 1] walks off the end of the user's input. Since value was initialized 0 to, temp = value[j 1] will be 0. So 0 will always be your min (unless the user enters negatives).

Instead, iterate only up to j < input - 1.

I'm trying to solve a question to find the lowest and highest numbers in an array in C Language.

You don't need to sort the array, you can do this in a single pass.

// Initialize min and max to be the first value.
int min = value[0];
int max = value[0];

// Then loop through the rest of the array checking if each value is
// smaller than min and/or larger than max.
for (i = 1; i < input; i  ) {
  if( value[i] < min ) {
    min = value[i];
  }

  if( value[i] > max ) {
    max = value[i];
  }
}

printf("min: %d, max: %d\n", min, max);

Note: It's not necessary to declare all your variables up front. You can declare them as you need them. And you don't need different iterators for each loop, you can reuse i as I have above.

  • Related