# Thread: Need some help with arrays

1. ## 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.
Please help  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)
{
????????????????????
}``` 2. the quick and easy method would be just to use the reverse( ) function from the <algorithm> library:

Code:
```reverse(&list, &list);   //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; //  3. Its pretty easy man you just need to use your brain. 4. 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] << " ";``` 5. 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. 6. Try:
Code:
`for (index = listSize-1; index >=0; index--)`
Instead because it needs 1 extra space in the array. 7. 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. 8. 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 ???? 9. 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. 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???? 11. Code:
`for (index = -1; index >=0; index--)`
need to use loop conditions that meet the need of your array algorithm. 12. I fixed it and it works now.
Thanks guys  Popular pages Recent additions 