counting array positions

This is a discussion on counting array positions within the C++ Programming forums, part of the General Programming Boards category; I'm trying to count the number of correct (sorted) positions in an array. example: Code: 1 2 3 4 5 ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    counting array positions

    I'm trying to count the number of correct (sorted) positions in an array.
    example:
    Code:
    1 2 3 4 5 6 7 8 9 10
    10 positions are correct.

    example2:
    Code:
    1 2 3 4 5 6 7 8 9 0
    9 positions are correct.

    example3:
    Code:
    10 9 8 7 6 5 4 3 2 1
    0 positions are correct.

    example4:
    Code:
    10 9 8 7 6 5 4 3 2 15
    1 positions are correct.


    The code i'm using now for this doesn't work:
    Code:
    #include <iostream>
    using namespace std;
    
    int PickBestSort(int array1[], int size);
    int PrintArray(int array1[], int size);
    
    
    int main()
    {
    	int array1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    	int array2[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    	int size = 10;
    
    	PickBestSort(array1, size);
    	PrintArray(array1, size);
    
    	cout << endl;
    
    	return 0;
    }
    int PickBestSort(int array1[], int size)
    {
    	float percentSorted = 0;
    
    	for(int i = 0; i < (size-1) ; i++)
    	{
    		//Checks how sorted the array is. The higher "percentSorted" is, the more sorted the array is.
    		if (array1[i] <= array1[i+1])
    		{
    			percentSorted++;
    		}
    	}	
    	cout << endl << "Percent Sorted: " << percentSorted*10 << endl;
    	cout << endl << percentSorted << " out of " << size << " positions are correct." << endl;
    
    
    	//If 20% or less of the array is sorted, then it is considered to be reverse sorted.
    	if ( percentSorted <= 20)
    	{
    		cout << endl << "The array is reverse sorted or close to being reverse sorted." << endl;
    	}
    	//If 80% or more of the array is sorted, then it is considered to be already sorted.
    	else if ( percentSorted >= 80)
    	{
    		cout << endl << "The array is already sorted or close to being already sorted." << endl;
    	}
    	//If between 20% and 80% of the array is sorted, then it is considered to be random.
    	else
    	{
    		cout << endl << "The array is random or close to being random." << endl;
    	}
    
    	return 0;
    }
    int PrintArray(int array1[], int size)
    {
    	cout << endl;
    	for (int j = 0; j < size; j++)
    	{
    		cout << array1[j];
    		cout << " ";
    	}
    
    	return 0;
    }
    So, my counting algorithm (in red) increases by one, if a value is less than the value in front of it.
    Right now, in example 1, my program will only say 9 positions are correct, instead of 10. This is because the last value in the array isn't compared to anything. However, examples 3 and 4 will read correctly. If if change the algorithm to where examples 1 and 2 work, then 3 and 4 won't work (they will say one more position is correct than really is). I can't figure out a counting algorithm that will work for all cases.

    I am only suppose to run through the array once to analyze it, and i can't change the array, because it will mess up the sorting algorithm later on in the program, and i don't think i'm suppose to change the array once the numbers are read into it.

    If you are wondering, my program is suppose to analyze an array, and choose a sorting algorithm based on efficiency. I'm trying to determine how sorted the array is, so i can best choose the most efficient sort.

    Any ideas?
    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're comparing fenceposts to fences. Since there are only nine gaps in a ten-number sequence, you shouldn't want to get a number higher than 9 out of your check.

    Think of a two-number sequence. Either it's right, or it's backward. 0 or 1, not 0 or 2.

    (We just had a fencepost discussion, but if you've forgotten, here's the bad ASCII art:
    Code:
    |---|---|---|---|---|---|---|---|---|
    Ten numbers (|), but only nine connections (the fences between the posts).)

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I think i see what you are saying, but when i try to convert it to a percent (percent of array sorted) i run into a problem

    9/9 = 100%
    8/9 = 90%
    7/9 = 80%
    6/9 = 70%
    5/9 = 60%
    4/9 = 50%
    3/9 = 40%
    2/9 = 30%
    1/9 = 20%
    0/9 = 10%
    ...

    I guess i'm still looking at it the wrong way. If 9 are correct then shouldn't the percent of the array sorted be 100%, and if 0 are correct it should be 0%?
    IDE - Visual Studio 2005
    Windows XP Pro

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I call on my awesome powers of arithmetic to say that
    9/9 = 100%
    8/9 = 88.88888888888888888888888888%
    7/9 = 77.77777777777777777777777777%
    6/9 = 66.66666666666666666666666666%
    5/9 = 55.55555555555555555555555555%
    4/9 = 44.44444444444444444444444444%
    3/9 = 33.33333333333333333333333333%
    2/9 = 22.22222222222222222222222222%
    1/9 = 11.11111111111111111111111111%
    0/9 = 0%

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ok i think i get i now. I was trying to convert the percent of fences correct to the percent of posts correct.
    8/9 = 88.8% of the fences are correct, and that would be 90% of the posts correct.
    ...something like that.
    I'll go with the percent of fences correct.

    Thanks for the explanation.
    IDE - Visual Studio 2005
    Windows XP Pro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21