You are on page 1of 4

ASSIGNMENT IN

PROGRAMMING 2
PRESENTED BY: MARK JOED ORTIZ
#include <iostream> In this first part of the code, the
using namespace std; program declare that the array
size limit is only 5, and it also
#define ARRAY_SIZE 5
declare the I and j variables for
int main()
{ the looping part of the program
int numbers[ARRAY_SIZE], i ,j ,temp; and temp for temporary holder of
the value. The for loop function is
cout<<"Simple C++ Example Program for Sorting to read the amount of arrays
(Ascending Order) In Array\n";
inserted by the user. And the cout
// Read Input part is where the user will input
for (i = 0; i < ARRAY_SIZE; i++) how many arrays the way to input
{ in the program.
cout<<"Enter the Number : "<< (i+1) <<" : ";
cin>>numbers[i];
}
// Array Sorting - Ascending Order In the part of the code it contains
for (i = 0; i < ARRAY_SIZE; ++i) a nested loop that will display the
{ input values of the user. And the if
for (j = i + 1; j < ARRAY_SIZE; ++j)
statement is comparing the sizes
{
if (numbers[i] > numbers[j]) of the arrays inserted by the user.
{ The temp value on the other hand
temp = numbers[i]; is declared as a temporary holder
numbers[i] = numbers[j]; of a value swapping the value of i
numbers[j] = temp;
}
and j if needed depending on
} what order it will display the value
} (descending or ascending).
cout<<"Sorting Order Array: \n"; In this final part of the code it will
for (i = 0; i < ARRAY_SIZE; ++i) display the input arrays by the
cout<<numbers[i]<<endl; user in ascending order using a
for loop.
return 0;
}

You might also like