Reputation: 173
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;
}
Upvotes: 0
Views: 220
Reputation: 10962
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).
Upvotes: 3
Reputation: 959
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;
Upvotes: 2
Reputation: 44274
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;
}
Upvotes: 1