
May 7th, 2008, 10:41 PM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 2
Time spent in forums: 35 m 40 sec
Reputation Power: 0
|
|
|
Memory & arrays - Smallest to Largest
I am trying to create an array that will take 10 inputs into the array from the user. After this is done, then it is to go through the array and make the smallest number to the largest. and then state it again except this time largest to the smalled. This is what I have so far.... I know I need some help...
Code:
#include <iostream>
using namespace std;
int smallestIndex( int[], int); // function prototype
void main()
{
int arr[9];
int position;
// Get information for the array.
cout << "Please enter 10 numbers for this array: ";
cin >> arr[0];
cin >> arr[1];
cin >> arr[2];
cin >> arr[3];
cin >> arr[4];
cin >> arr[5];
cin >> arr[6];
cin >> arr[7];
cin >> arr[8];
cin >> arr[9];
position = smallestIndex(arr, 9);
}
int smallestIndex( int arr[], int size)
{
int smallestIndex=0;
int temp=arr[0];
int i;
for(i=0; i<size; i++)
{
if(arr[i]<temp)
{
smallestIndex = i;
temp=arr[i];
}
}
return smallestIndex;
}
|