jumping into c++
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for ( int i = 0; i < size; i++ )
{
int index = findSmallestRemainingElement( array, size, i );
swap( array, i, index );
}
}
int findSmallestRemainingElement (int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if ( array[ i ] < array[ index_of_smallest_value ] )
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
void swap (int array[], int first_index, int second_index)
{
int temp = array[ first_index ];
array[ first_index ] = array[ second_index ];
array[ second_index ] = temp;
}
// small helper method to display the before and after arrays
void displayArray (int array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
// you'll see this pattern a lot for nicely formatting
// lists--check if we're past the first element, and
// if so, append a comma
if ( i != 0 )
{
cout << ", ";
}
cout << array[ i ];
}
cout << "}";
}
int array_mean(int array[], int size){
double sum = 0;
for(int i = 0 ; i < size ; i++){
sum+=array[i];
}
return int(sum/size);
}
void displayArray_bigandsmall(int array[], int size){
cout << "Biggest and Smallest" <<array[0] << ", " << array[size-1] << endl;
cout << "Mean = " << array_mean(array, size) << endl;
}
int insertion_sort(int array[], int size)
{
sort( array, size );
}
int main ()
{
int size;
cout<< "Please give me the size";
cin >> size;
int array[size];
srand(time(NULL));
for ( int i = 0; i < size; i++ )
{
// keep the numbers small so they're easy to read
array[ i ] = rand() % 100;
}
cout << "Original array: ";
displayArray( array, size );
cout << '\n';
insertion_sort(array, size);
cout << "Sorted array: ";
displayArray( array, size );
cout << '\n';
displayArray_bigandsmall(array, size);
}
'프로그래밍 공부 > Jumping into C++' 카테고리의 다른 글
[Jumping into C++] 2차원으로 구성된 임의의 크기의 곱셈표. P.189 (0) | 2020.11.16 |
---|---|
[C++ 더 쉽게 더 깊게 실습과제] 1인용 틱택토 게임 (0) | 2020.11.11 |
[C++] 소수인지 판별해 출력하는 프로그램 (0) | 2020.11.10 |
[C++] 2인용 틱택토 게임 (0) | 2020.05.02 |
[C++] 값을 넘겨받아 가장 높은 값과 가장 낮은 값, 값들의 평균을 출력한 뒤에 한 행에 하나씩 출력하는 프로그램 (0) | 2020.05.02 |