Hi All
I purchased the jumping into C++ and i am a beginner. I now reached the array section which is sorting arrays.
The code below as follows:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int lowest_value_in_array ( int myArray[], int sizeofArray, int indexofArray );
void Do_Swap ( int myArray [], int firstindex, int secondindex);
//////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
void Do_Sort ( int myArray [], int sizeofArray)
{
for ( int i = 0; i < sizeofArray; i++ )
{
int indexofArray = lowest_value_in_array( myArray, sizeofArray, i );
Do_Swap ( myArray, i, indexofArray );
}
}
////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
int lowest_value_in_array( int myArray[], int sizeofArray, int indexofArray )
{
int indexofsmallestvalue = indexofArray;
for ( int i = indexofArray + 1; i < sizeofArray; i++ )
{
if ( myArray[i] < myArray[indexofsmallestvalue])
{
indexofsmallestvalue = i;
}
}
return indexofsmallestvalue;
}
void Do_Swap (int myArray[], int firstindex, int secondindex)
{
int temp = myArray[firstindex];
myArray[firstindex] = myArray[secondindex];
myArray[secondindex] = temp;
}
void displayArray (int myArray[], int sizeofArray)
{
cout << "{";
for (int i = 0; i < sizeofArray; i++)
{
if (i != 0)
{
cout << ", ";
}
cout << myArray[i];
}
cout << "}";
}
int main()
{
int myArray [10];
srand (time (NULL));
for (int i = 0; i < 10; i++)
{
myArray[i] = rand() % 100;
}
cout << "Original Array: ";
displayArray(myArray, 10);
cout << "\n";
Do_Sort (myArray, 10);
cout << "Sorted Array: ";
displayArray(myArray, 10);
cout << "\n";
}
My question is indexofArray is not set to zero and on some they have weird values other than zero and also the loop starts with
"indexofArray + 1"
How does the value "indexofArray" set to zero.
Please help !!!