# Need some help with arrays

• 10-05-2005
newbiec++
Need some help with arrays
I am trying to get my program to return the smallest number and then output my list in reverse order. My largest function works but I can't figure out how to do the smallest or the reverse order.

Code:

``` int main() {         printArray (listA, arraySize);         cout << endl << endl;             cout << "Largest Number: "         << listA [indexLargestElement(listA, arraySize)]         << endl << endl;                 cout << "Smallest Number: "                       ???????       //output the reverse order         printArrayReverse (listA, 0, 5);         cout << endl << endl;     return 0; } {   void printArray (const int list[], int listSize) {         int index;         for (index = 0; index < listSize; index++)                 cout << list[index] << " "; }   int indexLargestElement (const int list[], int listSize) {         int index;         int maxIndex = 0;         for (index = 1; index < listSize; index++)                 if (list[maxIndex] < list[index])                         maxIndex = index;         return maxIndex; } int indexSmallestElement (const int list[], int listSize) { ????????????????????????? }  //function print the elements of an int array in reverse void printArrayReverse (const int list[], int listSize) {     ???????????????????? }```
• 10-05-2005
The Brain
the quick and easy method would be just to use the reverse( ) function from the <algorithm> library:

Code:

```reverse(&list[0], &list[5]);  //pass in the first element and last element of the range of the array to be sorted cout << "In reverse order:  "; for(int  i=0; i<5; i++)       cout << list[i] << "  ";```

using the swap( ) function from <algorithm>
Code:

```for(int i=0, j=array_size; i<array_size/2; i++, j--)     swap(&list[i], &list[j]);```

or.. write your own reverse algorithm:
Code:

```for(int i=0, j=array_size, temp; i<array_size/2; i++, j--) {     temp = list[i];     list[i] = list[j];     list[j] = temp; }```

jmd15's method below is probably a more efficient method of what you are wanting to do though ;)

code == good; // :cool:
• 10-05-2005
loko
Its pretty easy man you just need to use your brain.
• 10-05-2005
jmd15
Print out the array in reverse just initialize index to listSize and instead of incrementing it, decrement it. Like below:
Code:

```              int index;         for (index = listSize; index >=0; index--)                 cout << list[index] << " ";```
• 10-05-2005
newbiec++
I used the follwing code that was provided by jmd15
Code:
int index;

for (index = listSize; index >=0; index--)
cout << list[index] << " ";
[/code]

It does print out the reverse, but when I input 1, 2, 3, 4, 5 it outputs 5, 4, 3, 2, 1 but it also has a large negative number in front of it -85899460 5 4 3 2 1

What do I need to change?
Also, any ideas on how to output the smallest number?

Thanks for the help.
• 10-05-2005
jmd15
Try:
Code:

`for (index = listSize-1; index >=0; index--)`
Instead because it needs 1 extra space in the array.
• 10-05-2005
The Brain
classic symptom of accessing memory outside the bounds of the array.. easy fix. a little bit of trial and error troubleshooting on your part and you would have had it figured out all by yourself.
• 10-05-2005
newbiec++
It worked !!!!!

I don't know if I could have figured it out by myself I am still very green.
Brain your code is way over my head right now. I hope to get that good one day.

To output the smallest number it should have similar code right ????
• 10-05-2005
The Brain
Quote:

To output the smallest number it should have similar code right ????
once your array is sorted.. you should know exactly where the smallest and largest numbers are located ;)
• 10-05-2005
newbiec++
I thought I got the smallest function to work but it only returns the first number entered.
Here is what I put in:

Code:

```int indexSmallestElement (const int list[], int listSize) {         int index;         int minIndex = 0; //assume the first element is the smallest         for (index = -1; index >=0; index--)                 if (list[minIndex] < list[index])                         minIndex = index;         return minIndex;```
How do I get this to return the smallest number????
• 10-05-2005
The Brain
Code:

`for (index = -1; index >=0; index--)`
need to use loop conditions that meet the need of your array algorithm.
• 10-05-2005
newbiec++
I fixed it and it works now.
Thanks guys :)