C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 8th, 2006, 11:45 PM
hero hero is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 hero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 31 sec
Reputation Power: 0
Printing an array after it's been sorted?

Hi all, I was wondering, if I wanted to print the array of the following code, how would I do that?

Code:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>          // for rand()

const int NUM_PRODUCTS = 30;
const int LOW_VALUE = 0;
const int FAILURE = -1;

int binarySearch(int arr[], int target);
void showResults(int result, char str[], int target);

int main()
{
      int arr2[NUM_PRODUCTS];
     int randInt, result, target, k;

      srand((unsigned)time(NULL)); // seed generator


     // also fill arr2 with ordered values for binary search (ascending order)
     for (k = 0; k < NUM_PRODUCTS; k++)
     {
          randInt = rand() % (NUM_PRODUCTS+1) + LOW_VALUE;
          arr2[k] = k;
     }

     cout << "Enter product id: ";          // prompt for target for binary search
     cin >> target;

     result = binarySearch(arr2, target);
     showResults(result, "BINARY SEARCH", target);

      cout << endl;
      system ("PAUSE");
      return EXIT_SUCCESS;
}


// binary search defined for integer arrays previously sorting in ascending order
int binarySearch(int arr[], int target)
{
     int middlePosition, middleValue, result = -1;
     int low = 0, high = NUM_PRODUCTS - 1;
     bool isFound = 0;

     while ( !isFound && low <= high)
     {
          middlePosition = (low + high) / 2;
          middleValue = arr[middlePosition];
          if (target == middleValue)
          {
               result = middlePosition;
               isFound = 1;
          }
      else if (target < middleValue)
            high = middlePosition - 1;
      else
            low = middlePosition + 1;
      }
    return result;
}

void showResults(int result, char str[], int target)
{

     cout << endl << endl << str << endl << "-------------" << endl;
     if (result != FAILURE)
          cout << "Found product id [" << target << "] in array position [" << result << "]" << endl << endl;
     else
          cout << "TARGET [" << target << "] not found." << endl << endl << endl;
     
}



Reply With Quote
  #2  
Old April 9th, 2006, 10:31 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
printing out of arrays is usually done with a for loop, like so:
Code:
for (int i = 0; i < ARRAY_LENGTH; ++i)
{
	  cout << array[i] << endl;
}

make sure to get ARRAY_LENGTH right or your program may crash or overwrite important memory.

if your array is a null-terminated string or you are using some convention of that form (where the last element of the array is guaranteed to be 0 or some other unique value) then you can test for "arr[i] != ARRAY_TERM_VALUE" instead of "i < ARRAY_LENGTH".

there are many arrays in your code. which one, specifically, do you want to print out (if you still need more help)?

Reply With Quote
  #3  
Old April 10th, 2006, 09:39 AM
hero hero is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 hero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 31 sec
Reputation Power: 0
So could I just put that for loop in the showResults function or should I create a new one?

I am trying to print out the arr2 array after it's been sorted.

The way the array works is block [0] of the array's value is [0], block [1]'s value is [1], etc. I just need to cout that to the user.

Reply With Quote
  #4  
Old April 10th, 2006, 03:40 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
you can put the for loop in showResults, but you'll have to pass arr2 as a parameter to showResults. By the way, arr2 is never sorted in your code. It is searched for target, but not sorted (I think this is a flaw in your logic, because the binarySearch function says it requires an array in ascending order. Filling in array with random values in no way guarantees ascending order).

Reply With Quote
  #5  
Old April 10th, 2006, 11:03 PM
hero hero is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 hero User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 31 sec
Reputation Power: 0
revised

Code:
#include <iostream.h>
#include <stdlib.h>

const int NUM_PRODUCTS = 30;
const int LOW_VALUE = 0;
const int FAILURE = -1;

int binarySearch(int arr[], int target);
void showResults(int result, char str[], int target);

int main()
{
     int arr1[NUM_PRODUCTS];
     int result, target, k;




     //fill arr1 with ordered values for binary search (ascending order)
     for (k = 0; k < NUM_PRODUCTS; k++)
     {
          arr1[k] = k;
     }

     cout << "Enter product id: ";          // prompt for target for binary search
     cin >> target;

     result = binarySearch(arr1, target);
     showResults(result, "BINARY SEARCH", target);

      cout << endl;
      system ("PAUSE");
      return EXIT_SUCCESS;
}


// binary search defined for integer arrays previously sorting in ascending order
int binarySearch(int arr[], int target)
{
     int middlePosition, middleValue, result = -1;
     int low = 0, high = NUM_PRODUCTS - 1;
     bool isFound = 0;

     while ( !isFound && low <= high)
     {
          middlePosition = (low + high) / 2;
          middleValue = arr[middlePosition];
          if (target == middleValue)
          {
               result = middlePosition;
               isFound = 1;
          }
      else if (target < middleValue)
            high = middlePosition - 1;
      else
            low = middlePosition + 1;
      }
    return result;
}

void showResults(int arr1,int result, char str[], int target)
{

     cout << endl << endl << str << endl << "************" << endl;
     if (result != FAILURE)
          cout << "The product ID was found [" << target << "] in array position [" << result << "]" << endl << endl;
     else
          cout << "The product ID [" << target << "] was not found, please try again." << endl << endl << endl;
          for (int i = 0; i < 30; ++i)
          {
	      cout << arr1[i] << endl;
          }

}

Reply With Quote
  #6  
Old April 13th, 2006, 03:19 PM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
pass arr2 to a seprate sort function as a paremeter

void sort_arr2(int[], int); //prototype

int arr2[#of elements]
sort_arr2(arr2, maxlength of array,i think)): // function call

for(int t =0; t<maxlength; t++)
{
cout<<Arr2[t]<<endl; // that should "spit out" the sorted array , granted you placed the sort function before "spitting it out"
}




void sort_arr2(int A[], int b) // function definition
{
//sort definition...





}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Printing an array after it's been sorted?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT