'분류 전체보기'에 해당되는 글 162건

  1. 2019.05.06 *6.4 배열과 선택 정렬 selection sort (복습)
  2. 2019.05.06 6.3 배열과 반복문
  3. 2019.05.06 6.1 배열 기초적인 사용법
  4. 2019.05.04 5.9 난수 만들기

#include <iostream>


using namespace std;


void printArray(const int array[],const int length) // const 변수 바꿀 수 x

{

for (int index = 0; index < length; ++index)

cout << array[index] << " ";

cout << endl;

}


int main()

{

const int length = 5;

int array[length] = { 3,5,2,1,4, };


printArray(array, length);


for (int startIndex = 0; startIndex < length - 1; ++startIndex)

{

int smallestIndex = startIndex;


for (int currentIndex = startIndex + 1; currentIndex < length; ++currentIndex)

{

if (array[smallestIndex] > array[currentIndex])

{

smallestIndex = currentIndex;

}

}

//swap to values in the array

{

int temp = array[smallestIndex];

array[smallestIndex] = array[startIndex];

array[startIndex] = temp;

}

}


return 0;

}

*어려움 ㅜㅜ

debug 찍어서 바뀌는거 보고 복습하기 

'programming > c++' 카테고리의 다른 글

6.7 포인터 기본  (0) 2020.01.26
6.5 정적 다차원 배열  (0) 2019.05.06
6.3 배열과 반복문  (0) 2019.05.06
6.1 배열 기초적인 사용법  (0) 2019.05.06
5.9 난수 만들기  (0) 2019.05.04
Posted by 도이(doi)
,

반복문과 배열

sizeof(scores) / sizeof(int)

scores( )배열의 크기는 int의 집합이기에 int로 나눠주면
scores 배열의 개수를 구할 수 있다. 


최대값 최소값 찾기 


'programming > c++' 카테고리의 다른 글

6.5 정적 다차원 배열  (0) 2019.05.06
*6.4 배열과 선택 정렬 selection sort (복습)  (0) 2019.05.06
6.1 배열 기초적인 사용법  (0) 2019.05.06
5.9 난수 만들기  (0) 2019.05.04
5.8 break, continue  (0) 2019.05.04
Posted by 도이(doi)
,

array의 기본적인 사용

컴퓨터 프로그래밍은 0이 1번째 이다. 


구조체 arraay로 사용


array 초기화

array 초기화2

*visual studio 11 부터 = 없어도 됨


학생 수 배열 



array는 compile타임에 사이즈가 정해져야 한다. 

따라서, cin같이 run 타임에 실행되는 값은 받아올 수 없다. 

'programming > c++' 카테고리의 다른 글

*6.4 배열과 선택 정렬 selection sort (복습)  (0) 2019.05.06
6.3 배열과 반복문  (0) 2019.05.06
5.9 난수 만들기  (0) 2019.05.04
5.8 break, continue  (0) 2019.05.04
5.7 반복문 for  (0) 2019.05.04
Posted by 도이(doi)
,

#include <iostream>

#include <cstdlib> //std::radn(), std::srand()

#include <ctime> //std::time()


using namespace std;


/*특정한 정수 사이의 random 넘버*/

int getRandomNumber(int min, int max)

{

static const double fraction = 1.0 / (RAND_MAX + 1.0);

//RAND_MAX는 random넘버를 만들 때 나올 수 있는 가장 큰 숫자 


return min + static_cast<int>((max - min + 1)*(std::rand()*(fraction)));

}


int main()

{

std::srand(static_cast<unsigned int>(std::time(0))); //seed를 바꾸는 

//debuging할 때는 seed를 고정 


for (int count = 1; count <= 100; ++count)

{

cout << getRandomNumber(5,8) << "\t";

if (count % 5 == 0) cout << endl;

}

return 0;

}


랜덤 라이브러리를 사용해서 난수생성(주로 사용)

#include <iostream>

#include <cstdlib> //std::radn(), std::srand()

#include <ctime> //std::time()

#include <random> 


using namespace std;


int main()

{

std::random_device rd;

std::mt19937_64 mersenne(rd());
        //create a mersenne twister(random time과 비슷한 역할)

std::uniform_int_distribution<> dice(1, 6);


for (int count = 1; count <= 20; ++count)

cout << dice(mersenne) << endl;


return 0;

}


'programming > c++' 카테고리의 다른 글

6.3 배열과 반복문  (0) 2019.05.06
6.1 배열 기초적인 사용법  (0) 2019.05.06
5.8 break, continue  (0) 2019.05.04
5.7 반복문 for  (0) 2019.05.04
5.6 do while  (0) 2019.04.30
Posted by 도이(doi)
,