1

I keep on getting the same numbers 166 and 74. I am trying to get 74 166 250 273 441 545 659 710 808 879 924 931. I really have no idea where to find this bug. I do know that main function is correct but I am not sure on where to find the bug that is just giving me 166 and 74.

#include <stdio.h>

// Swap the values pointed to by a and b.
void swap( int *a, int *b )
{
  int temp = *a;

  *a = *b;

  *b = temp;
}

// Return a pointer to the element of the given array with the smallest value.
int *findSmallest( int *list, int len )
{
  int *smallest = list;
  for ( int i = 1; i < len; i++ ) {

       if(*smallest > *(list + i)) {

       *smallest = *(list + i);

       }

  }

  return smallest;
}

// Print the contents of the given list.
void printList( int *list, int len )
{
  while ( len ) {

    len--;

    printf("%d ", *(list + --len));


  }

  printf( "\n" );
}

int main()
{
  // A list of random-ish values.
  int list[] = { 808, 250, 74, 659, 931, 273, 545, 879, 924, 710, 441, 166 };
  int len = sizeof( list ) / sizeof( list[ 0 ] );

  // For each index, find the smallest item from the remaining
  // (unsorted) portion of the list.
  for ( int i = 0; i < len; i++ ) {

    int *p = findSmallest( list + i, len - i );

    // Swap the smallest item into the first position in the unsorted part of the
    // list.
    swap( list + i, p );
  }

  printList( list, len );
}
}
4
  • 1
    Use list[--len] if printing in reverse order is acceptable. Commented Feb 15, 2019 at 16:16
  • Using pointers, would that be *(a - i) ? Commented Feb 15, 2019 at 16:21
  • 1
    *(list + --len). You need to decrement before accessing as list[len] is outside of the array bounds. Commented Feb 15, 2019 at 16:23
  • It worked, thanks. Updated the question because there seems to be a bug. Commented Feb 15, 2019 at 16:47

2 Answers 2

3
len--;
printf("%d ", *(list + --len));

You're decrementing len twice.


...but the main problem is this line in findSmallest:

*smallest = *(list + i);

Here smallest points to an element in the list, and you're overwriting that element. Instead, you should be changing smallest itself so it points to another element:

smallest = (list + i);

With these two fixes, here's the output:

931 924 879 808 710 659 545 441 273 250 166 74

The list, correctly sorted and printed back-to-front.

Sign up to request clarification or add additional context in comments.

5 Comments

It does seem to be another bug but it does not fix the 166 and 74 part.
@AHunt You're right. I found the actual problem in your code and edited my answer.
Thanks, how would I put it as front to back?
I changed the sign in if(*smallest > *(list + i)) since this was ordering from largest to smallest and I wanted from smallest to largest
The list itself is sorted correctly (smallest element at the front). It's displayed in back-to-front order because your printList function iterates in reverse.
0

this code:

int *findSmallest( int *list, int len )
{
    int *smallest = list;
    for ( int i = 1; i < len; i++ ) {

       if(*smallest > *(list + i)) {

           *smallest = *(list + i);

       }

    }

should be calling swap() so the original value in position list[0] is not overlayed in the statement: *smallest = *(list + i);

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.