Help with Troubleshooting arrays
Nov 8, 2015 at 4:08pm UTC
Im trying to print out an array in ascending order at the end of my code but I keep getting a minimum value of 2 and then the first number is 2 when I dont have 2 inputted.
const int ARRAY_SIZE=10;
using namespace std;
/*
*
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
int findMax(int maximum[ARRAY_SIZE])
{
int largest=maximum[0];
for (int m=0;m<ARRAY_SIZE;m++)
{
if (maximum[m]>largest)
{
largest=maximum[m];
}
}
return largest;
}
int findMin(int minimum[ARRAY_SIZE])
{
int smallest=minimum[0];
for (int i=0;i<ARRAY_SIZE;i++)
{
if (minimum[i]<smallest)
{
smallest=minimum[i];
}
}
return smallest;
}
int main(int argc, char ** argv) {
int numbers[ARRAY_SIZE];
int current_size=10;
int count=0;
int other=ARRAY_SIZE;
int temp;
int temp1;
cout<<"Please Enter 10 integers " <<endl;
for (int i=0; i<ARRAY_SIZE; i++)
{
count++;
cin>> numbers[i];
}
cout<<endl;
cout<<"The numbers entered were: " <<endl;
cout<<endl;
for (int n=0; n<count; n++)
{
cout<<numbers[n] <<endl;
}
cout<<endl;
//numbers in the opposite way they were entered
cout<<endl;
temp=numbers[0];
numbers[0]=numbers[10];
numbers[10]=temp;
cout<<"The numbers entered in reverse order are: " <<endl;
for (int n=ARRAY_SIZE-1; n>0; n--)
{
cout<<numbers[n]<<endl;
}
cout<<numbers[10];
cout<<endl;
cout<<"Maximum number: " << findMax(numbers) <<endl;
cout<<"Minimum number: " << findMin(numbers) <<endl;
//Numbers in ascending order
cout<<"The numbers in ascending order: " <<endl;
for (int a=0; a<current_size; a++)
{
for (int b=0; b<current_size;b++)
{
if (numbers[a]<numbers[b])
{
temp1=numbers[a];
numbers[a]=numbers[b];
numbers[b]=temp1;
}
}
}
for (int a=0;a<current_size;a++)
{
cout<<numbers[a]<<endl;
}
return 0;
}
Last edited on Nov 8, 2015 at 4:09pm UTC
Nov 8, 2015 at 6:50pm UTC
1 2 3 4 5
int numbers[ARRAY_SIZE];
...
temp=numbers[0];
numbers[0]=numbers[10];
numbers[10]=temp;
If
ARRAY_SIZE is 10, what are the valid indexes for
numbers ?
Also, you can't reverse an array by swapping only 2 elements.
Topic archived. No new replies allowed.