I did this program and something is strange. isn't supposed the array start at index 0? why in my program it started at 1? I don't know how i did it. Could my IDE the one that made this "problem"?
Please help me understand what have a i done.
thanks
#include <iostream>
usingnamespace std;
int main()
{
float time[20]; //
int a,first=20,y,x;
for (x=1;x<21;x++)
{
cout << "Enter the time of the person number " << x << " : ";
cin >> time[x];
}
for (y=1;y<20;y++)
{
if (time[y]<first)
{
first=time[y];
a=y;
}
}
getchar();
cout << "The person " << a << " was the faster.";
cout << time[1];
getchar();
return 0;
}
The index at 0 is preserved and still present. Your for loops don't start a zero, so change that and you'll be in business.
for (x=0;x<21;x++)
for (y=0;y<20;y++)
When you're iterating through a container, remember to start at the first index (which is always 0). Since you had x=1 and y=1, when you began iterating, it starts at the time[1] in the array and leaves time[0].
You have a problem with your for loop at line 7.
You're looping 20 times, but the last time through the loop you storing into time[20]. That's a problem. Your array goes from [0]-[19]. There is no time[20]. You're likely to trash some memory you shouldn't.
How big are time time values? At line 5 you initialize first to 20. If all your time values are > 20, you'll never execute lines 16 and 17. When you get to line 21, a will be uninitialized.
At line 22, don't you want to print time[a] rather than time[1] ?