176만명의 커뮤니티!! 함께 토론해봐요.
미해결
홍정모의 따라하며 배우는 C++
안녕하세요 교수님! 2.7 문자형 char 강의 중 약 13분에 나오는 예제에 대한 질문입니다. 예제에서 c1을 입력받을 때 65를 입력해봤습니다. 출력하기 전에 제 예상으로는 c1은 char형(문자형)이고, 숫자를 입력하면 ASCII코드에 따라 그 숫자에 맞는 문자가 나올 것이라고 생각했습니다. (char c1(65);에서 c1값을 출력했을 때와 같을 것이라고 예상했습니다.) 그런데 제 예상과는 다르게 c1에 ASCII코드 'A'에 해당하는 65를 입력했더니 c1의 값은 6으로 출력이 되었습니다. (ASCII코드도 6에따라 54로 출력되었습니다.) 왜 여기에서는 A가 아닌 6이 출력이 되는지 궁금합니다. 아래에 코드와 출력화면을 첨부합니다. 감사합니다:) #include <iostream> #include <limits> int main() { using namespace std; char c1(65); cout << c1; cin >> c1; cout << c1 << endl; cout << static_cast<int>(c1) << endl; return 0; }
이루자
2020-03-08T12:30:08.730Z
댓글 2
좋아요 1
조회수 375
미해결
홍정모의 따라하며 배우는 C++
(이전 질문 삭제 및 수정 후 다시 질문드립니다:) ) 교수님 안녕하세요, 2.6강 bool 예제에 대한 질문입니다. (19:53초) 교수님께서 "정수 하나를 입력받고 그 숫자가 홀수인지 짝수인지 출력하는 프로그램을 만들어봅시다."라는 과제를 내주셨는데 구현을 아래 코드와 같이 했으며, 홀수 짝수 결과값도 입력값에 따라 잘 출력이 됩니다. 그런데 궁금한 점은, 여기서 제가 사진과 같이 정말 큰 수를 짝수로 입력하게 되면 홀수로 나옵니다. (반대로, 정말 큰 수를 홀수로 입력하게 되면 홀수로 나옵니다.) 이 결과값은 왜 이런지 잘 이해가 되지 않아서 질문드립니다. 감사합니다:) #include <iostream> int main() { using namespace std; int a; cin >> a; if (a % 2 == 0) cout << "짝수" << endl; else cout << "홀수" << endl; return 0; }
이루자
2020-03-08T07:31:57.492Z
댓글 3
좋아요 0
조회수 391
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
아무리봐도 잘못된 부분이 보이지 않는데 count를 print해보았을 때, 왜 count가 7로 나오는지 모르겠습니다.. 혹시 잘못된 부분이 있다면 지적해주시면 감사하겠습니다. #include<iostream> #include<stdio.h> #include<vector> using namespace std; int main() { freopen("input.txt", "r", stdin); int n, count = 1, max = -1; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { count = 1; for (int j = i + 1; j < n; j++) { if (a[i] < a[j] || a[i] == a[j]) count++; //5 7 3 3 12 12 13 10 11 else break; } printf(" %d", count); if (count > max) max = count; } //printf("%d", max); }
2020-02-18T13:33:32.644Z
댓글 1
좋아요 0
조회수 204
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
비쥬얼스튜디오에서 51번 700x700 배열을 선언하면 입력하기 전에 종료되는데 어떻게 하면 되나요??
김태형
2020-01-26T06:21:44.324Z
댓글 2
좋아요 1
조회수 322
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
terminate called after throwing an instance of 'std::bad_alloc' what()… 콘솔창에 이렇게 나오고 실행이 되질않아요..
2020-01-24T17:45:33.853Z
댓글 1
좋아요 0
조회수 412
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
배열크기를 300001로 하는거랑 30001로 하는거랑 success 갯수가 다른데 무슨차이인가요? 300001로하면 4번까진 성공나오고 5번은 타임리밋인데 30001로 배열크기로하면 2번부터 타임리밋이 나옵니다.. #pragma warning(disable:4996) #include <stdio.h> #include <string.h> #include <math.h> #include<time.h> #include<iostream> #define endl '\n'; using namespace std; int a[300001]; int b[300001]; int r[300001]; int main() { int n1,n2,k=0,j=0,max=0; scanf("%d",&n1); for(int i=1;i<=n1;i++) scanf("%d",&a[i]); scanf("%d",&n2); for(int i=1;i<=n2;i++) scanf("%d",&b[i]); for(int i=1;i<=n1;i++) { k=a[i]; if(k>max) max=k; r[k]++; } for(int i=1;i<=n2;i++) { j=b[i]; if(j>max) max=j; r[j]++; } for(int i=1;i<=max;i++) { if(r[i]==2) printf("%d ",i); } }
2020-01-24T17:33:08.322Z
댓글 1
좋아요 0
조회수 255
해결됨
홍정모의 따라하며 배우는 C++
안녕하세요? 이 경우에는 파라미터?로 이니셜라이저 리스트를 받으니까 1. 셀프 어사인먼트 방지하는 방법은 생각을 못했습니다. IntArray & operator= ( const std :: initializer_list < int > & list ) { cout << "assignment operator" << endl; cout << "list.size() In assignment operator " << list . size () << endl; delete[] m_data; int length = list . size (); m_data = new int [length]; int count = 0 ; for ( auto & element : list) { m_data [count] = element; ++count; } return * this ; } //IntArray(list.size()); // 생성자를 통한 초기화는 안되는거고 2. m_data가 데이터를 가지고 있을 수는 있으니까 지워주고 리스트의 길이만큼 new로 초기화해주고 리스트의 값을 복사해줬습니다. 교재인 learncpp에서 해당 부분을 찾아보니 다음과 같이 출력을 해야 한다더라고요. 그런데 리스트의 사이즈는 7인데 5개만 나왔습니다. 자고 일어나서 해결해보겠습니다. 감사합니다. <출력화면> <전체코드> // 9_12.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <cassert> #include <initializer_list> using namespace std ; class IntArray { private: unsigned m_length = 0 ; int * m_data = nullptr ; public: IntArray ( unsigned length = 0 ) : m_length (length) { m_data = new int [length]; } IntArray ( const std :: initializer_list < int > & list ) : IntArray ( list . size ()) { cout << "list.size() In constructor " << list . size () << endl; int count = 0 ; for ( auto & element : list) { m_data [count] = element; ++count; } //for (unsigned count = 0; count < list.size(); ++count) // m_data[count] = list[count]; // error } ~IntArray () { delete[] this -> m_data ; } IntArray & operator= ( const std :: initializer_list < int > & list ) { cout << "assignment operator" << endl; cout << "list.size() In assignment operator " << list . size () << endl; delete[] m_data; int length = list . size (); m_data = new int [length]; int count = 0 ; for ( auto & element : list) { m_data [count] = element; ++count; } return * this ; } friend std :: ostream & operator << ( std :: ostream & out , const IntArray & arr ) { for ( unsigned i = 0 ; i < arr . m_length ; ++i) out << arr . m_data [i] << " " ; out << endl; return out; } }; int main () { int my_arr1 [ 5 ] = { 1 , 2 , 3 , 4 , 5 }; int * my_arr2 = new int [ 5 ]{ 1 , 2 , 3 , 4 , 5 }; auto il = { 10 , 20 , 30 }; IntArray int_array { 1 , 2 , 3 , 4 , 5 }; cout << int_array << endl; int_array = { 7 , 2 , 3 , 2 , 1 , 6 , 5 }; cout << int_array << endl; return 0 ; }
호두
2020-01-22T15:01:40.870Z
댓글 1
좋아요 2
조회수 436
해결됨
홍정모의 따라하며 배우는 C++
안녕하세요? 증감연산자(후위) 오버로딩 할 때 지역변수 temp 를 리턴하는 거라서 이것도 문제가 될 수 있는 건가요? 감사합니다.
호두
2020-01-22T06:45:11.789Z
댓글 2
좋아요 2
조회수 424
해결됨
홍정모의 따라하며 배우는 C++
안녕하세요? Doctor.h와 Patient.h가 분리되어 있을 때는 meetDoctors함수는 Patient.h에 있기 때문에 Doctor.h로 옮길 수가 없는데 이럴땐 어떻게 해야하나요? 감사합니다. <코드> #pragma once #include <iostream> #include <string> #include <vector> #include "Doctor.h" //class Doctor; class Patient { private: std ::string m_name; std ::vector<Doctor*> m_doctors; public: Patient ( const std :: string & name_in ) : m_name (name_in) {} void addDoctor ( Doctor * new_doctor ) { m_doctors . push_back (new_doctor); } void meetDoctors () { for ( auto & elem : m_doctors) std ::cout << "meet doctor : " << elem -> m_name << std ::endl; } void setName ( const std :: string & name_in ) { m_name = name_in; } friend std :: ostream & operator<< ( std :: ostream & out , const Patient & patient ) { out << patient . m_name ; /*for (auto& elem : patient.m_doctors) out << *elem;*/ return out; } friend class Doctor ; }; =====================자답============ 이렇게 해결을 했는데 맞게 한건가요? ㅠㅠ .h, .cpp로 선언과 정의를 분리하고 cpp에서 전부 인클루드하고 헤더 두 군데서 모두 전방선언 해줬습니다. 감사합니다.
호두
2020-01-17T10:30:22.518Z
댓글 2
좋아요 3
조회수 417
미해결
홍정모의 따라하며 배우는 C++
static const int m_value = 3;에서 초기화를 안해주고 바깥의 static 변수 초기화하는 부분에 const를 붙여줬습니다. class Something { public: static const int m_value; }; const int Something::m_value = 3 ; 감사합니다.
호두
2020-01-08T11:46:14.796Z
댓글 5
좋아요 1
조회수 506
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
#pragma warning(disable:4996) #include <stdio.h> #include <string.h> #include <math.h> #include<time.h> #include<iostream> #define endl '\n'; using namespace std; int c[1001]; int arr[20]; int main() { int s, n,tmp,k,wh; scanf("%d%d", &s, &n); for (int i = 0; i < n; i++) { scanf("%d", &tmp); if (arr[0] == 0&&c[tmp]==0) { arr[0] = tmp; c[tmp] = 1; } else if (arr[0]!=0&&c[tmp]==0) { for (int j = i; j > 0; j--) { arr[j + 1] = arr[j]; arr[j] = arr[j - 1]; } c[tmp] = 1; arr[0] = tmp; } else if (arr[0] != 0 && c[tmp] == 1) { for (k = 0; k < i; k++) { if (arr[k] == tmp) break; } for (int j = k; j > 0; j--) { arr[j + 1] = arr[j]; arr[j] = arr[j - 1]; } arr[0] = tmp; } } for (int i = 0; i < s; i++) { printf("%d ", arr[i]); } } 해쉬 느낌으로 배열을 하나선언해서풀어봤습니닫 1,2,3,4,5번에서 1,2번만맞고 3,4,5에서 자꾸 틀리는데 왜틀렸는지를 모르겟어요 ...분명 이론적으로 맞았다 생각이드는데 숫자 한두개씩 틀리더라구요
2020-01-07T18:45:52.125Z
댓글 3
좋아요 0
조회수 279
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
바깥 for문에서 왜 for i = 0 ; i < n-1 을 해주었는지 궁금합니다 for i = 0 ; i < n이 아니고요
2020-01-07T07:36:04.178Z
댓글 1
좋아요 0
조회수 225
미해결
홍정모의 따라하며 배우는 C++
안녕하세요. 연습문제 풀어봤습니다. 9에서 ++ 하면 0이 되게 하는 부분은 m_digit == 9 일 때는 그냥 m_digit에 0을 대입 했습니다. <코드> Digit & Digit :: operator ++ () { if (m_digit == 9 ) { m_digit = 0 ; } else { ++m_digit; } return * this ; } Digit & Digit :: operator -- () { if (m_digit == 0 ) { m_digit = 9 ; } else { --m_digit; } return * this ; } Digit Digit :: operator ++ ( int ) { Digit temp (m_digit); // m_digit++; ++(* this ); return temp; } Digit Digit :: operator -- ( int ) { Digit temp (m_digit); // m_digit++; --(* this ); return temp; } <출력> PS C:\coding\tbcpp_review\Chapter9> .\a.exe prefix --0 9 9 prefix ++9 0 0 postfix 0-- 0 9 postfix 9++ 9 0 <전체코드> #include <iostream> using namespace std ; class Digit { private: int m_digit; public: Digit ( int digit = 0 ) : m_digit (digit) {} // prefix Digit & operator ++ (); Digit & operator -- (); // postfix Digit operator ++ ( int ); Digit operator -- ( int ); friend ostream & operator << ( ostream & out , const Digit & digit ) { out << digit . m_digit ; return out; } }; Digit & Digit :: operator ++ () { if (m_digit == 9 ) { m_digit = 0 ; } else { ++m_digit; } return * this ; } Digit & Digit :: operator -- () { if (m_digit == 0 ) { m_digit = 9 ; } else { --m_digit; } return * this ; } Digit Digit :: operator ++ ( int ) { Digit temp (m_digit); // m_digit++; ++(* this ); return temp; } Digit Digit :: operator -- ( int ) { Digit temp (m_digit); // m_digit++; --(* this ); return temp; } int main () { Digit digit1 ( 0 ); Digit digit2 ( 9 ); Digit digit3 ( 0 ); Digit digit4 ( 9 ); cout << "prefix --0" << endl; cout << "--digit1(0) : " << --digit1 << endl; cout << "digit1(0) : " << digit1 << endl; cout << "prefix ++9" << endl; cout << ++digit2 << endl; cout << digit2 << endl; cout << "postfix 0--" << endl; cout << digit3-- << endl; cout << digit3 << endl; cout << "postfix 9++" << endl; cout << digit4++ << endl; cout << digit4 << endl; return 0 ; } 감사합니다.
호두
2020-01-04T21:44:22.465Z
댓글 1
좋아요 2
조회수 249
해결됨
홍정모의 따라하며 배우는 C++
중괄호 안에서만 x=1이 쓰이고나서 중괄호 밖으로 나오면 다시 main문의 x가 쓰여야하는 것아닌가요? 왜 계속 중괄호 한의 1이 사용되는지 헷갈립니다
tjrwn0716
2020-01-02T14:59:49.473Z
댓글 2
좋아요 1
조회수 250
해결됨
홍정모의 따라하며 배우는 C++
안녕하세요! 선생님 말씀하신 옵션으로 코드블럭스를 설치했는데, MinGW installation manager를 어떻게 열어야하는지 모르겠습니다.. 당장은 visual studio로 공부하겠지만, 차후에 학교 과제를 할 때 code blocks도 많이 이용된다고 해서 gdb를 설치해두고 싶은데, installation manager를 어디서 열어야 하나요??
삼다
2019-12-29T05:41:21.998Z
댓글 2
좋아요 0
조회수 439
미해결
it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비
혹시 n이 주어지면 n번째 위치한 숫자를 반환하려면 어떻게 해야될까요?
두리황
2019-12-05T06:22:47.773Z
댓글 2
좋아요 0
조회수 286
미해결
홍정모의 따라하며 배우는 C++
교수님 저도 이 강의에서 13일전에 나왔던 질문자와 같은 질문이 있습니다. "두 코드를 비교하시면서 동적, 정적 바인딩의 차이를 설명해주셨는데, 결국 result나 func_ptr 모두 런타임에서 op값이 무엇이냐에 따라서 결정되는 것 아닌가요?" 이런질문이 있었는데 강의가 어느부분인지 표기해서 올리라고 하신 답글을 달아주셨네요. 여기에 덧붙여서 좀더 디테일하게 질문을 하자면, 결국 op를 cin으로 런타임에 받아서 그 op에 따른 케이스에따라 result 나 func_ptr이나 모두 결정되는 것이 아닌가합니다. 그래서 cout << result << endl;이나 cout << func_ptr(x,y) << endl; 이나 동일하게 런타임에 입력하는 op값에 따라 결정되는 것이 아닌가 생각합니다. 그래서 차이를 잘 모르겠습니다. 강의는 12.5 동적바인딩과 정적바인딩이고 1분15초쯤입니다. 교수님께서 질문을 하면 고민 더 해보라고 하시는 답글을 매번 해주시는 것 같아서 질문 전에 자료도 찾아보고 고민해봤는데 이 예제에서의 정적바인딩 동적바인딩 차이를 가늠하기가 좀 어렵네요 찾아보면 대부분이 예제를 int a = 0 이런걸 정적바인딩으로 설명해서 이미 컴파일타임에 a가 결정되는 이런걸 정적바인딩이라고 설명을 해놓아서 더욱 헷갈리는 것 같습니다. 교수님의 보다 자세한 의견을 듣고싶습니다.
sualyer010
2019-11-29T12:41:43.103Z
댓글 3
좋아요 5
조회수 1074
미해결
홍정모의 따라하며 배우는 C++
안녕하세요 교수님, 11분 40초에 < 부호에 <=로 바꾸면 run time error가 난다는 게 무슨 뜻인가요? 프로그램 상 작동은 아웃풋 값은 똑같이 작동이 되길래 궁금해서 질문 올립니다. 감사합니다!
민경준
2019-10-09T03:33:33.363Z
댓글 4
좋아요 1
조회수 448
미해결
홍정모의 따라하며 배우는 C++
// // main.cpp // inheritance_ex01 // // Created by Kevin on 04/10/2019. // Copyright © 2019 Kevin. All rights reserved. // #include <iostream> using namespace std; class Mother { // basic class private: int m_i; public: //Mother() {} Mother(const int& i_in) : m_i(i_in) { std::cout << "Mother constructor" << std::endl; std::cout << "m_i " << m_i << std::endl; } void setValue(const int& i_in) { std::cout << "i_in " << i_in << std::endl; m_i = i_in; std::cout << "m_i " << m_i << std::endl; } int getValue() { return m_i; } }; class Child : public Mother { private: double m_d; public: Child(const int& i_in, const double& d_in) : Mother(i_in), m_d(d_in) { std::cout << "Child constructor" << std::endl; Mother::setValue(i_in); } void setValue(const int& i_in, const double& d_in) { } void setValue(const double& d_in) { m_d = d_in; } double getValue() { return m_d; } }; // derived class int main(int argc, const char * argv[]) { Mother mother(512); std::cout << mother.getValue() << std::endl; Child child(1024, 2048); std::cout << "m_i value : " << mother.getValue() << std::endl; // 왜 512인가요? std::cout << "m_d value : " << child.getValue() << std::endl; return 0; }
김영성
2019-10-05T02:43:39.553Z
댓글 4
좋아요 1
조회수 407
해결됨
홍정모의 따라하며 배우는 C++
class Base { public: int x; }; class Derived : Base { }; 상속받은 Derived옆에 Base의 접근지정자를 쓰지 않으면 private으로 접근지정자가 들어가는건가요? 접근지정자를 쓰지않아도 오류가 뜨지않는걸 보니 자동으로 뭔가가 들어가는거 같아요
오늘했던거 까먹는사람
2019-09-18T05:14:51.509Z
댓글 1
좋아요 0
조회수 149