1.

Solve : C++ Int Array Sorter?

Answer»

I typed up a SMALL C++ program for sorting int arrays. I was wondering if there are any issues with the program. As always, all help is appreciated.
Code: [Select]#include <iostream>

void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}


int main()
{
//Create an array of integers.
const int SIZE_OF_A = 10;
int a[SIZE_OF_A];
int i = 0;
int j;
//Generate an array of random integers.
for(int k = 0; k < SIZE_OF_A; k++)
{
a[k] = rand()%11;
}
//Sort the array values from smallest to largest
// if one index is greater than the next, swap the indexes go back to beginning of CHECK
while(i != (SIZE_OF_A - 1)){
j = i + 1;
if (a[i] > a[j]){
swap(a[i], a[j]);
i = 0;
} else if (a[i] <= a[j]){
i++;
}
}
//Print the sorted array to console.
for(int i = 0; i < SIZE_OF_A; i++)
{
STD::cout << a[i] << std::endl;
}
//Pause and wait for USER to exit.
std::cout << "Enter any alphanumeric value(s) and press enter to exit. " << std::endl;
char response;
std::cin >> response;
return 0;
}



Discussion

No Comment Found