-2
int smallindex = 0;
int secsmallindex = 0;

I put the lines above into gobalslope.
The rest of the code:

#include <iostream> 
using namespace std;

int main() {
    int list[10] = { 33,6,3,4,55,22,5,6,7,6 };
    for (int a = 1; a <= 10; a++)
    {
        if (smallindex > list[a]) {
            secsmallindex = smallindex;
            smallindex = a;
        }
        else if (list[a] < secsmallindex && list[a] > smallindex) {
            secsmallindex = a;
        }
    }
    cout << secsmallindex << "   " << smallindex;
}

I want to find the second smallest elements and smallest number.
But it can not find it.
The output is 2 and 10.

user438383
  • 5,716
  • 8
  • 28
  • 43
Dasiy
  • 1
  • 2
  • 2
    Where do you initialize `smallindex` and `secsmallindex`? – 001 May 14 '22 at 16:48
  • 1
    Also, you are reading one past the end of the array. – 001 May 14 '22 at 16:49
  • 2
    Hi! Please format your post properly, and what is `secsmallindex` and `smallindex` initially? And what is your output? Copy your whole code to be able to reproduce the problem. – simre May 14 '22 at 16:49
  • In addition to the problems mentioned by the other commenters, you are comparing your index value with the value of array elements, which is not what you want to do. – EddieLotter May 14 '22 at 16:52
  • Better to avoid `using namespace std` - see here https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. – wohlstad May 14 '22 at 17:01
  • Welcome to Stack Overflow. When you write code, take small steps; instead of trying to write a program that does two things, start with a program that does the simpler one. In this case, finding the smallest element. And until it works perfectly, have it print out diagnostic information, like `cout << "comparing " << list[a] << " to " << list[b] << endl;` – Beta May 14 '22 at 17:02
  • @wohlstad Why? This is his/her main. It is totally fine to use `using namespace` there. You should avoid it in the header files! That is the only place where it can cause really painful problems later... – simre May 14 '22 at 17:07
  • @simre I agree that `using namespace` in header files is far more problematic than in a cpp (and very small at that). But I am in the opinion that it is a bad habit, better to be avoided altogether if/when possible. The link in my comment contains several arguments in favor of this approach. – wohlstad May 14 '22 at 17:13
  • @wohlstad I don't think this is the way to go: "bad habit, don't do it, std:: is safer".... I think the way to go is to understand what the hell are you doing. This is the same problem as in the "monkey test". Where the monkeys are punished if any of them touched the banana, then they are swapped out 1 by 1, and at the end no original monkey was there but they did not touched the banana because they had no idea why, but other monkeys will hit them if they tried, since none of them knew at this time what happens if any of them touches it, they just learned that you can't touch that... – simre May 14 '22 at 17:21
  • @simre - as I wrote, the link contains arguments why it better be avoided altogether (so the monkey story seems irrelevant to me). But it is by no means a must. The link also mentions an option to use it locally (e.g. inside a method) to limit its scope. – wohlstad May 14 '22 at 18:17

2 Answers2

1

You had some problems. Mostly index ranges of an array, and comparing index with the actual value stored in the array. I commented out the old (problematic) lines and added the correct ones with some description. (Demo)


#include <iostream>

using namespace std;

int main() {
    /// You don't need these variables to be global.
    /// Minimise the scope of the variables!
    int smallindex = 0;
    int secsmallindex = 0;

    int list[10] = {33, 6, 3, 4, 55, 22, 5, 6, 7, 6};
    
    /// for (int a = 1; a <= 10; a++) <- old line
    /// In C and C++, array indexing is 0 based! Your last index is 9
    for (int a = 1; a < 10; a++)
    {
        /// if (smallindex > list[a]) <- old line
        /// You comparing the index to the ath element but you should
        ///     compare element to element
        if (list[smallindex] > list[a]) 
        {
            secsmallindex = smallindex;
            smallindex = a;
        }
        /// else if (list[a] < twosmallindex && list[a] > smallindex) <- old line
        /// Same as before, compare element to element not element to index
        else if (list[a] < list[secsmallindex] && list[a] > list[smallindex])
        {
            secsmallindex = a;
        }
    }
    cout << secsmallindex << " " << smallindex;
}

Output: 3 2

simre
  • 647
  • 3
  • 9
0

You need to compare the elements of the array. Not the index vs element

if(list[smallindex] > list[a])

user438383
  • 5,716
  • 8
  • 28
  • 43