Thread: C++ Array Question

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    Question C++ Array Question

    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 !!!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    indexofArray is an argument which is passed into the function. It tells the function where to start looking.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array question
    By MrMatt027 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2011, 04:05 PM
  2. Replies: 12
    Last Post: 11-21-2010, 09:46 AM
  3. question about the array in C
    By thungmail in forum C Programming
    Replies: 2
    Last Post: 11-06-2009, 11:24 PM
  4. Array question.
    By Zarakava in forum C Programming
    Replies: 11
    Last Post: 01-24-2009, 07:43 AM
  5. array question
    By kantze in forum C Programming
    Replies: 5
    Last Post: 11-22-2006, 04:45 AM

Tags for this Thread