0

enter image description here

This is the sample output of the question.My swapping part is not functioning well...

int *smallest, *current = array;
    for(int i=0; i<MAX-1; i++, current++){ //partial code given by the question

        smallest = current;
        cout << "\nCurrent=" << *current;
        int tmp = *current;
        for(int j = i; j < MAX; j++,smallest++){ //my code 
            if(*smallest < tmp){
                tmp = *smallest;
            }
        }
        *smallest = tmp;
        cout << " Smallest=" <<*smallest<< endl;

        int tmp2; //not functioning from this line onwards
        tmp2 = *smallest;
        *smallest = *current;
        *current = tmp2;
     }
1
  • 1
    The algorithm is what wrong...you need only one loop of iterations to find a minimum. Commented Sep 16, 2019 at 6:05

3 Answers 3

3

First of all, you certainly do not need 2 for loops for this. That being said you may just the algorithm library directly for this task:

https://en.cppreference.com/w/cpp/algorithm/min_element

#include <vector>
#include <algorithm>

int main(){
    std::vector elements = {8,6,4,2,16};
    return *std::min_element(std::begin(elements), std::end(elements));
}

Try it yourself: https://godbolt.org/z/oHkMid

And using raw pointers you just need to do that instead - something like the following:

return *std::min_element(array, array + MAX);

or if array has known size

return *std::min_element(std::begin(array), std::end(array));

And what's wrong with you original code ? when you get to the last iteration of the outer loop you only examine minimum of the last element (main problem was: you have two loops).

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

Comments

2

You don't need two for loops to find the smallest element in an array. It can be done in only one traversal of the array.

int *smallest, *current;
smallest = current = array;                     // both smallest and current refer to &array[0]
for (; current - array <= MAX - 1; ++current)   // current will increment till the last element of the array which is at MAX-1 distance from &array[0]
    if (*current < *smallest)                   // if element at *current is less than element at *smallest then update smallest.
        smallest = current;
current = nullptr;                              // current refers to the element at MAX distance from &array[0] hence it should be set to nullptr.

cout << *smallest << endl;

4 Comments

Why not mentioning std::min or std::min_element? Knowing the standard library functions is essential for learning any language, but especially important for C++.
OP asked to find the smallest element using pointers. By looking at their code I assumed they're learning about pointers hence I gave an answer accordingly.
As darune's answer shows, standard library functions work perfectly for what OP wants. Most of the time they do.
@lucieon I aggree with nada: you need to precise in here - answers should always lean towards use of "current best practice" unless theres some explicit reason not to - and even then should mention the "best practice".
1

I'm don't fully understand your algorithm. If you just want to find the smallest element in an array, you don't need two for loops.

However, I do see some problems in your code.

for(int j = i ;j < MAX-i ; smallest++){ // Here you don't want to increment smallest !
                                        // You only want to change smallest
                                        // when *current is less than *smallest
                                        // You probably want ++j instead
                                        //
                                        // Further "MAX-i" should be "MAX-1"

   if(array[j] < *current){             // Here you don't want to compare array and *current
                                        // Instead you want:
                                        //     array[j] < *smallest

       smallest = &array[i];            // Here you want to use j instead of i:
                                        //      smallest = &array[j]
   }
}

If you "just" want to find the smallest element in the array using pointers, you can do it like:

  int *smallest = array;
  int *current = array;
  for(int i=0; i<MAX-1; i++, current++){
    cout << "\nCurrent=" << *current;

    if(*current < *smallest){
      smallest = current;
    }

    cout << " Smallest=" <<*smallest<< endl;
  }

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.