I’m trying to write a function which sort an array and I met a problem. When I sort an array it always return 0 in first position of sorted array. No matter what combination it is.
void sorting(int wielkosc)
{
int t[wielkosc];
//filling
for (int i = 0; i < wielkosc; i++)
{
t[i] = rand()% 100 + 1;
}
// before sort
cout << "before sorting: " << endl;
for (int i = 0; i < wielkosc; i++)
{
cout << "[" << i << "] -> " << t[i] << endl;
}
cout << endl;
// asc...
int iTemp = 0;
for (int i = 0; i < wielkosc; i++)
{
for (int j = i + 1; j <= wielkosc; j++)
{
//jak chcesz zmienic na malejaco to se przekrec '<' na '>'
if (t[j] < t[i])
{
iTemp = t[i];
t[i] = t[j];
t[j] = iTemp;
}}}
// after sorting
cout << "after sorting: " << endl;
for (int i = 0; i < wielkosc; i++)
{
cout << "[" << i << "] -> " << t[i] << endl;
}
cout << endl;
}
Source: Windows Questions C++