CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Exclamation Does an array class exist like this.

    Hey, I was just writing this program to practice functions and arrays and i was wondering if there is a class or package out there that does the same thing i have here. I am new to C++ so i dont have many good references for C++ classes and packages. And i know that i am re-inventing the wheel by writing these functions they are for my practice.

    Code:
    #include <iostream>
    #include <iomanip>
    
    
    
    
    //Function ProtoTypes.
    int sumArray(int[], int);
    double sumArray(double[],int);
    int largest(int[],int);
    int largest(double[],int);
    int smallest(int[],int);
    int smallest(double[],int);
    double average(double[], int);
    double average(int[],int);
    bool isAscending(int[],int);
    bool isDescending(int[],int);
    
    int linearSearch(int[],int,int);
    int linearSearch(double[],int,double);
    int linearSearch(char[],int,char);
    int binarySearch(int[], int , int);
    int binarySearch(double[],int,double);
    int binarySearch(char[],int,char);
    
    void bubbleAscending(int[],int);
    void bubbleAscending(double[],int);
    void bubbleAscending(char[],int);
    void bubbleDescending(int[],int);
    void bubbleDescending(double[],int);
    void bubbleDescending(char[],int);
    void selectionAscending(int[],int);
    void selectionAscending(double[],int);
    void selectionAscending(char[],int);
    
    
    
    
    
    
    using namespace std;
    
    
    
    int main()
    {
    
        //MAIN IS EMPTY ,, I took out all my testing crap.
    
    cout <<"Waiting to Exit"<<endl;
    cin.get();
    return(0);
    }
    
    
    /**************************************************************************
    **********************   BASIC OPERATIONS   *******************************
    **************************************************************************/
    
    
    //Returns the sum of all elements in the array.
    //Accepts integer array and integer size
    int sumArray(int array[], int size)
    {
        int sum=0;
        for(int i=0;i<size;i++)
            sum += array[i];
            
        return(sum);
    }
    
    //Returns the sum of all elements in the array.
    //Accepts double array and integer size
    double sumArray(double array[], int size)
    {
        double sum=0;
        for(int i=0;i<size;i++)
            sum += array[i];
            
        return(sum);      
    }
    
    //Returns the index of the largest element in the arrray
    //Accepts integer array and integer size
    int largest(int array[], int size)
    {
        int largest=0; //Index of largest element
        
        for(int i=1;i<size;i++)
            if(array[largest] < array[i])
                largest=i;
                
        return(largest);
    }
    
    
    //Returns the index of the largest element in the arrray
    //Accepts double array and integer size
    int largest(double array[], int size)
    {
        int largest=0; //Index of largest element
        
        for(int i=1;i<size;i++)
            if(array[largest] < array[i])
                largest=i;
                
        return(largest);
    }
    
    
    //Returns the index of the first occurance of the smallest element in the array
    //Accepts integer array and integer size
    int smallest(int array[], int size)
    {
        int smallest=0; //Index of smallest Element
        
        for(int i=1;i<size;i++)
            if(array[smallest]>array[i])
                smallest=i;
        
        return(smallest);
    }
    
    
    //Returns the index of the first occurance of the smallest element in the array
    //Accepts double array and integer size
    int smallest(double array[], int size)
    {
        int smallest=0; //Index of smallest Element
        
        for(int i=1;i<size;i++)
            if(array[smallest]>array[i])
                smallest=i;
        
        return(smallest);
    }
    
    
    //Returns the average of an Arrays Elements
    //Accepts double array and integer size
    double average(double array[], int size)
    {
        double sum=0.0;
        
        for(int i=0;i<size;i++)
            sum += array[i];
        
        return(sum/size);
    }
    
    //Returns the average of an Array Elements
    //Accepts integer array and integer size
    double average(int array[], int size)
    {
        double sum=0.0;
        
        for(int i=0;i<size;i++)
            sum += array[i];
        
        return(sum/size); 
    }
    
    //Determines if an array is sorted in Ascending order
    //Accepts integer array and integer size.
    //Returns true if in ascending order.
    bool isAscending(int array[],int size)
    {
        for(int i=0;i<size-1;i++)
            if(array[i] > array[i+1])
                return(false);
                
        return(true);   
    }
    
    //Determines if an array is sorted in Descending order
    //Accepts integer array and integer size.
    //Returns true if in Descending order.
    bool isDescending(int array[],int size)
    {
        for(int i=0;i<size-1;i++)
            if(array[i]<array[i+1])
                return(false);
                
        return(true);
    }
    
    
    /**************************************************************************
    **************************   LINEAR SEARCH    *****************************
    **************************************************************************/
    
    
    
    //Uses a linear search on an Integer array.
    //Returns the index of the element matching find argument,
    //      returns -1 if element is not present.
    int linearSearch(int array[], int size, int find)
    {  
        for(int i=0;i<size;i++)
            if(array[i]==find)
                return(i);
        
        return(-1);
    }
    
    //Uses a linear search on a double array.
    //Returns the index of the elemnt matching find argument,
    //      returns -1 if element is not present.
    int linearSearch(double array[], int size, double find)
    {
        for(int i=0;i<size;i++)
            if(array[i]==find)
                return(i);
        
        return(-1);
    }
    
    //Uses a linear serch on a char array.
    //Returns the index of the element matching find argument.
    //      returns -1 if the element is not present.
    int linearSearch(char array[], int size, char find)
    {
        for(int i=0;i<size;i++)
            if(array[i]==find)
                return(i);
        
        return(-1);
    }
    
    /**************************************************************************
    **************************   BINARY SEARCH    *****************************
    **************************************************************************/
    
    //Uses a binary search on a sorted Integer array.
    //Returns the index of the element matching find argument,
    //      returns -1 if element is not present.
    int binarySearch(int array[], int size, int find)
    {
        int first=0;          //Index for the beggining of the search portion
        int last=size-1;      //Index for the end of the search portion
        int middle;           //Index for the middle of the search portion
    
        //Continue untill the array has been halved down to 1 Element.
        while(first<=last)
        {
            middle=(first+last)/2;      //Determine middle position
            
            //If the middle element is the one being search for return it.
            if(find==array[middle])     
                return(middle);
            
            //The find value will be in the left half (if it exists).
            else if(array[middle]>find)    
                last= middle-1;
            //The find value will be in the right half (if it exists).
            else
                first= middle+1;
        }
        return(-1);
    }
    
    //Uses a binary search on a sorted double array.
    //Returns the index of the elemnt matching find argument,
    //      returns -1 if element is not present.
    int binarySearch(double array[], int size, double find)
    {
        int first=0;
        int last=size-1;
        int middle;
    
        while(first<=last)
        {
            middle=(first+last)/2;
            
            if(find==array[middle])
                return(middle);
            
            else if(array[middle]>find)
                last= middle-1;
            else
                first= middle+1;
        }
        return(-1);
    }
    
    //Uses a binary serch on a sorted char array.
    //Returns the index of the element matching find argument.
    //      returns -1 if the element is not present.
    int binarySearch(char array[], int size, char find)
    {
        int first=0;
        int last=size-1;
        int middle;
    
        while(first<=last)
        {
            middle=(first+last)/2;
            
            if(find==array[middle])
                return(middle);
            
            else if(array[middle]>find)
                last= middle-1;
            else
                first= middle+1;
        }
        return(-1);  
    }
    
    
    /**************************************************************************
    *****************************   SORTING   *********************************
    **************************************************************************/
    
    
    //Uses a BubbleSort algorithm on an integer array to sort in Ascending order. 
    //Accepts an integer array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleAscending(int array[], int size)
    {
        int temp;
        bool swapMade=true;
        
        //Continues while a swap was made in the previous itteration.
        // A complete itteration, where no swap was made, will end the loop.
        while(swapMade)                 
        {
            swapMade=false;
            for(int i=0; i<size-1 ;i++)
                if(array[i]>array[i+1])
                {
                    //Swaping the contents.
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                    swapMade=true;
                }
        }
    }
    
    
    //Uses a BubbleSort algorithm on an double array to sort in Ascending order. 
    //Accepts a double array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleAscending(double array[], int size)
    {
        double temp;
        bool swapMade=true;
        
        while(swapMade)                 
        {
            swapMade=false;
            for(int i=0; i<size-1 ;i++)
                if(array[i]>array[i+1])
                {
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                    swapMade=true;
                }
        }
    }
    
    
    //Uses a BubbleSort algorithm on an char array to sort in Ascending order. 
    //Accepts a char array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleAscending(char array[], int size)
    {
        char temp;
        bool swapMade=true;
        
        while(swapMade)                 
        {
            swapMade=false;
            for(int i=0; i<size-1 ;i++)
                if(array[i]>array[i+1])
                {
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                    swapMade=true;
                }
        }
    }
    
    
    //Uses a BubbleSort algorithm on an integer array to sort in Descending order. 
    //Accepts an integer array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleDescending(int array[], int size)
    {
        int temp;
        bool swapMade=true;
        
        while(swapMade)                 
        {
            swapMade=false;
            for(int i=0; i<size-1 ;i++)    
                if(array[i]<array[i+1])
                {
                    swapMade=true;
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                }
        }
    }
    
    
    //Uses a BubbleSort algorithm on a double array to sort in Descending order. 
    //Accepts an double array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleDescending(double array[], int size)
    {
        double temp;
        bool swapMade=true;
        
        while(swapMade)                 
        {
            swapMade=false;
            for(int i=0; i<size-1 ;i++)    
                if(array[i]<array[i+1])
                {
                    swapMade=true;
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                }
        }
    }
    
    
    //Uses a BubbleSort algorithm on a char array to sort in Descending order. 
    //Accepts an char array and an integer size.
    //This will modify the array passed in (it will be sorted).
    void bubbleDescending(char array[], int size)
    {
        char temp;
        bool swapMade=true;
        
        while(swapMade)                 
        {
            swapMade=false;
    
            for(int i=0; i<size-1 ;i++)    
                if(array[i]<array[i+1])
                {
                    swapMade=true;
                    temp=array[i];
                    array[i]=array[i+1];
                    array[i+1]=temp;
                }
        }
    }
    
    
    //Uses a SelectionSort algorithm on an integer arry to sort in Ascending order.
    //Accepts an integer array and an integer size.
    //Modifys the array passed in (it will be sorted) 
    void selectionAscending(int array[], int size)
    {
        int start;       //Starting index of the unsorted portion.
        int smallest;    //Index of the smallest element in unsorted portion.
        int temp;        //Temporary storage for swaping.
        
        
        /*Outer loop continues untill start has moved to the next-to-last index. Once
        *    the last 2 indices have been compared no more comparisons are neccesary.
        *    The update moves the starting point of the unsorted portion and
        *    assigns the starting point as the smallest element.*/
        for(start=0,smallest=0; start<size-1 ; start++,smallest=start)
        {
            //This loop cycles through the unsorted portion looking for the smallest element
            for(int i=start+1; i<size ;i++)
                if(array[smallest] > array[i])
                    smallest=i;
                    
            //Moving the smallest element in the unsorted portion to the start of the
            //      unsorted portion.
            temp=array[start];
            array[start]=array[smallest];
            array[smallest]=temp;
        }
    }
    
    
    //Uses a SelectionSort algorithm on a double arry to sort in Ascending order.
    //Accepts an double array and an integer size.
    //Modifys the array passed in (it will be sorted) 
    void selectionAscending(double array[], int size)
    {
        int start;      
        int smallest;    
        double temp;        
        
        for(start=0,smallest=0; start<size-1 ; start++,smallest=start)
        {
            for(int i=start+1; i<size ;i++)
                if(array[smallest] > array[i])
                    smallest=i;
                    
            temp=array[start];
            array[start]=array[smallest];
            array[smallest]=temp;
        }
    }
    
    
    //Uses a SelectionSort algorithm on a char arry to sort in Ascending order.
    //Accepts an char array and an integer size.
    //Modifys the array passed in (it will be sorted) 
    void selectionAscending(char array[], int size)
    {
        int start;      
        int smallest;    
        char temp;        
        
        for(start=0,smallest=0; start<size-1 ; start++,smallest=start)
        {
            for(int i=start+1; i<size ;i++)
                if(array[smallest] > array[i])
                    smallest=i;
                    
            temp=array[start];
            array[start]=array[smallest];
            array[smallest]=temp;
        }
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Does an array class exist like this.

    http://www.cplusplus.com/reference/

    std::min_element and std::max_element in <algorithm> should be obvious.
    std::accumulate in <numeric> for sum.

    Average would be a trivial extension of accumulate.

    std::binary_search and std::find in <algorithm> for binary and linear search. For some tree-based containers like std::set and std::map, std::find may do binary search instead. More importantly, std::binary_search only returns a bool rather than the element itself unlike find----so choose the algorithm based on needs, and the underlying container based on desired runtime.

    std::sort and std::stable_sort are the two sorting algorithms available. Typically they'll implement quicksort and mergesort. Bubble and selection sort are *not* available, but then they're pretty crappy sorting algorithms anyway.

    There is std::valarray which has a few more specific options, but it isn't widely used.
    Last edited by Lindley; February 23rd, 2009 at 11:26 AM.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Does an array class exist like this.

    If you #include <algorithm>, you will be able to replace:
    • sumArray with std::accumulate
    • largest with std::max_element
    • smallest with std::min_element
    • linearSearch with std::find
    • binarySearch with std::lower_bound
    • the sorts with std::sort and/or std::stable_sort, depending on your requirements

    There are no direct replacements for average, isAscending and isDescending, but you can implement average with the help of std::accumulate.

    EDIT:
    Oh yes, Lindley is right, std::accumulate is found in <numeric>, not <algorithm>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Does an array class exist like this.

    Good call on std::lower_bound, I forgot about that one.

    The meaning of lower_bound and upper_bound may not be obvious until you play around with STL a bit. Once you do, the reason they're defined as they are should become obvious.

  5. #5
    Join Date
    Feb 2009
    Location
    USA
    Posts
    68

    Re: Does an array class exist like this.

    Hey thanks laser and lindley i knew there was something out there and that website looks like it will be very usefull.

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Does an array class exist like this.

    adjacent_find in combination with the appropriate function/functor checks whether a sequence is ascending or descending

    Code:
    struct check_ascending
    {
       bool operator()( int i1, int i2 ) const
       {
          return i1 > i2;
       };
    };
    
    struct check_descending
    {
       bool operator()( int i1, int i2 ) const
       {
          return i1 < i2;
       }
    };
    
    vector<int> v;
    // filling vector omitted
    vector<int>::iterator pos = adjacent_find( v.begin(), v.end(), check_ascending() );
    if( v.end() == pos )
    {
       // elements are sorted ascending
    }
    
    pos = adjacent_find( v.begin(), v.end(), check_descending() );
    if( v.end() == pos )
    {
       // elements are sorted descending
    }
    - Guido

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured