[C++] 143p 배열 실습문제 1,2

2020. 11. 11. 18:15·프로그래밍 공부/Jumping into C++

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
'프로그래밍 공부/Jumping into C++' 카테고리의 다른 글
  • [Jumping into C++] 2차원으로 구성된 임의의 크기의 곱셈표. P.189
  • [C++ 더 쉽게 더 깊게 실습과제] 1인용 틱택토 게임
  • [C++] 소수인지 판별해 출력하는 프로그램
  • [C++] 2인용 틱택토 게임
Rocketbabydolls
Rocketbabydolls
Rocketbabydolls
  • Rocketbabydolls
    With The Lights Out
    Rocketbabydolls
  • 전체
    오늘
    어제
    • 전체글 (184)
      • 프로그래밍 공부 (117)
        • C (16)
        • Jumping into C++ (9)
        • MFC (C++) (1)
        • 자료구조 (1)
        • 알고리즘 (1)
        • 백준 (C++) (74)
        • 핸즈온 머신러닝 2판 (6)
        • Unseen 3기 준비 (4)
        • 원티드 포텐업 게임 개발자 양성과정 2기 (4)
      • 언리얼엔진5 (63)
        • [Part1] 이득우의 언리얼 프로그래밍 (12)
        • [Part2] 이득우의 언리얼 프로그래밍 (2)
        • [Part2 복습] 이득우의 언리얼 프로그래밍 (3)
        • [Part3] 이득우의 언리얼 프로그래밍 (14)
        • [Part4] 이득우의 언리얼 프로그래밍 (0)
        • FPS 게임 1인 프로젝트 (6)
        • 각종 지식 (11)
        • 블루프린트 Paper2D 로 게임 만들기 (14)
        • 팀 프로젝트 (1)
      • 일상 (1)
      • ETC (1)
        • 맥북 (1)
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      오블완
      실전 C프로그래밍
      티스토리챌린지
      실전C프로그래밍
      핸즈온 머신러닝
      언리얼엔진 eqs generator
      실전 C프로그래밍 나중채
      실전C프로그래밍 나중채
      실전 C프로그래밍 실습문제
      실전C프로그래밍 실습문제
      언리얼엔진 eqs 커스텀
      c++ 17298
      실전 C 프로그래밍
      언리얼엔진5
      c언어
      언리얼엔진
      핸즈온 머신러닝 2판
      언리얼엔진 eqs c++
      언리얼엔진5 fps 프로젝트
      C언어 실습문제
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    Rocketbabydolls
    [C++] 143p 배열 실습문제 1,2
    상단으로

    티스토리툴바