| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;
}
|
|
#2
|
|||
|
|||
|
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)? |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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).
|
|
#5
|
|||
|
|||
|
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;
}
}
|
|
#6
|
|||
|
|||
|
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... } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Printing an array after it's been sorted? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|