Solved

Array comments

Posted on 2006-12-01
4
255 Views
Last Modified: 2010-04-01
I am wondering if you could comment each line of  this code so that I can understand what is doing.

#include <iostream>
using namespace std;

void printArray(int a[], int length){
cout << "[";
 for (int count = 0 ; count < length; count++)
       cout << a[count] << " " ;
cout << "]"<< endl;
}

void enterIntegers(int a[],int length){
 cout << "please enter " << length << " integers" << endl;  
 for (int count = 0 ; count < 5 ; count++)
      cin >> a[count];                                    
printArray(a,length);                                 
}


void swap(int& first, int& second)
{
       int temp = first;
       first = second;
       second = temp;
}

int minimum_from(int a[], int position, int length)
{
       int min_index = position;

       for (int count = position + 1 ; count < length ; count    ++)
               if (a[count] < a[min_index])
                       min_index = count;
               return min_index;
}
         
void selection_sort(int a[], int length)                
{
   for (int count = 0 ; count < length - 1 ; count++)
           swap(a[count],a[minimum_from(a,count,length)]);
}
           

           




int main(){
 int my_array[5];
 enterIntegers(my_array,5);
 selection_sort(my_array,5);
 cout << "The array after sorting it:" << endl;
 printArray(my_array,5);
 char c;cin >> c;
 system ("pause");
 return 0;
}
0
Comment
Question by:mayan1
  • 2
4 Comments
 
LVL 6

Expert Comment

by:xRalf
ID: 18053755
Hi mayan1,
this is implementation of selection sort algoritm. Here is the algoritm http://en.wikipedia.org/wiki/Selection_sort.
Which lines of code don't you undertstand? Have you problem with C++ (syntax, semantics) or algorithm itself?
0
 

Author Comment

by:mayan1
ID: 18054602
Hello xRalf ,
The program works as designed there are no problems with syntax or semantics or the algorithm.  I would like to know when it's sorting, reading, printing, insertion etc.
0
 
LVL 6

Expert Comment

by:xRalf
ID: 18054751
sorting:
for (int count = 0 ; count < length - 1 ; count++)
           swap(a[count],a[minimum_from(a,count,length)]);

I have to go through the whole array from 0 to the end.
You will take the first element and go through the remainder of the array. You will find the minimum element with function minimum_from(...) and
swap it with the first element (if the first element was smaller).
You will take the second element ....
atc.
After the end number of steps the array will be sorted.

reading:
enterIntegers(...) {...}

writing:
printArray(...) {...}

insertion:
The function swap(...) is swaping the first yet unsorted element with the minimum from the remainder, so after each swap we have added one element to the sorted part of array.
0
 
LVL 4

Accepted Solution

by:
Raymun earned 200 total points
ID: 18054932
Hello,

/* swap first with second (obviously) */
void swap(int& first, int& second)  // passed by reference (addresses of first and second), which is why there is no return value
{
       int temp = first;      // next line will overwrite first so store first in temp variable
       first = second;            
       second = temp;
}

/* Return minimum value from a[ position ] to a[ length - 1 ] inclusive */
int minimum_from(int a[], int position, int length)
{
      int min_index = position;

             for (int count = position + 1 ; count < length ; count++) {      // iterate through a[] starting at position + 1 up to length - 1
                     if (a[count] < a[min_index]) {                         // if value at a[count] is < value at a[min_index] then the new minimum value is at a[count]
                  min_index = count;                         // update min_index to reflect new minimum value in a[]
            }
      }
               
      return min_index;
}

/* Will sort from a[0] to a[length] */
void selection_sort(int a[], int length)
{
   for (int count = 0 ; count < length - 1 ; count++)                  // iterate through a[] starting at a[0] up to a[length -1]
           swap(a[count],a[minimum_from(a,count,length)]);            // uses the return value from  minimum_from as the index in a[], then uses the
}                                                      //  value at that index in a[], along with the value at a[count] to send to swap

Hope this helps
-Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modify static variables within a class in VC++ 3 95
What does std::atomic give me? 7 113
Best book to learn C++ 4 70
White board coding practice 3 77
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now