Search code examples
c++vectorminimum

Finding the smallest element of a vector


I'm 10th grade student, only 2 weeks of coding. I have a homework to fix this code if not working from the book with the title "Finding the smallest element of a vector". I've been stuck here over 5 days, and tomorrow is my due date.

    #include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int i, T[10], int min;
    for (i = 10; i < 10; i = i + 1)
        cin >> T[i];
    min = T[0];

    for (i = 1; i < 10; i++)
        if (T[i] < min)
            min = T[i];

    cout << "Min= " << min;
    return 0;
} 

What should I change in order to work? Thank you.


Solution

  • There were two lines wrong: int i, T[10], int min; and for (i = 10; i < 10; i = i + 1). Here is a working link.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i, T[10], min;
        for (i = 0; i < 10; i = i + 1)
            cin >> T[i];
        min = T[0];
        for (i = 1; i < 10; i++)
            if (T[i] < min)
                min = T[i];
        cout << "Min = " << min;
        return 0;
    }