본문 바로가기
728x90

c 예제코드21

[C Examples] Struct과 포인터를 활용해서 입력한 점수 저장하고 출력하기: struct, pointer Struct과 포인터를 활용해서 입력한 점수 저장하고 출력하기 포스트 난이도: HOO_Intern # Example Code 이번 예제코드에서는 Struct과 Pointer를 활용해서 사용자가 입력한 점수를 저장하고 다시 출력하면서 평균값을 산출해 낼 수 있다. 코드의 내용 자체는 매우 간단하기에 이번 예제코드에서 중점적으로 봐야 하는 부분은 Struct과 포인터가 어떻게 사용되는지이다. 코드 자체에서 하고자 하는 프로세스 자체가 간단하기 때문에 각 함수의 역할들을 이해하기 수월하다. #include #include struct node { float value; struct node* next; }; struct node* head = NULL; void displayList() { struct nod.. 2023. 12. 1.
[C Examples] Stack을 활용해서 Stack 값 바꿔보기, Dynamic stack Stack을 활용해서 Stack 값 바꿔보기, Dynamic stack 포스트 난이도: HOO_Junior # Example Code 1 이번 포스트에서는 C언어의 Stack (스택) 값을 바꿔보는 예제코드를 통해 스택에 대해서 보다 더 익숙해질 수 있다. 특히 이번 예제코드에서는 동적 배열 또는 동적 스택이라고 불리는 Dynamic stack에 대해서 살펴볼 수 있다. 아래의 예제코드 1을 보면 Lottery 숫자가 스택으로 주어져있는 상황에서 사용자가 임의의 7자리 숫자를 입력하고 난 뒤에 중간 스택에 새로운 값이 추가되는 걸 확인할 수 있다. 이때 예제코드 1에서는 중간 배열의 스택이 지속적으로 추가가 되어 처음 스택보다 스택의 값이 증가하는 걸 알 수 있다. 반면에 예제코드 2에서는 중간에 새롭게.. 2023. 11. 9.
[C Examples] Stack push(), pop()을 활용해서 stack overflow와 underflow을 살펴보는 예제코드 Stack push(), pop()을 활용해서 stack overflow와 underflow을 살펴보는 예제코드 포스트 난이도: HOO_Junior # Example Code 이번 포스트에서는 push()와 pop() 기능들을 살펴보면서 Stack에 대해서 복습해 볼 수 있다. 이전 예제코드에서 다뤘던 Struct, 구조체와 더불어 이번 코드에서는 Stack에서 사용되는 기본적인 기능들을 통해서 Stack의 overflow와 underflow가 어떻게 이루어지는 지를 살펴볼 수 있다. 코드가 점차적으로 길어지고 기능들을 추가되다 보니, 어려울 수도 있겠지만 나눠서 살펴보면 이해하기가 훨씬 수월하다. 각 기능들이 어떻게 작동하고 해당 예제코드에서 어떤 역할을 수행하는지를 우선적으로 이해해 보는 게 좋다. .. 2023. 10. 13.
[C Examples] struct과 pointer를 활용한 선수별 점수 출력하기 struct과 pointer를 활용한 선수별 점수 출력하기 포스트 난이도: HOO_Intern # Example codes 이번 포스트에서는 struct과 pointer를 활용하여 선수별 점수를 출력하는 예제코드를 살펴볼 수 있다. 아래의 예제코드를 살펴보면 struct과 더불어 각 선수의 아이디를 입력받아 저장하는데, 이 과정에서 포인터를 활용하여 데이터가 저장되는 걸 알 수 있다. 여기서 추가적으로 아이디를 입력했을 때 중복 여부를 확인할 수 있는 조건 블록을 작성해 줄 수도 있다. 포인터를 확실히 익혀야 다음 단계로 넘어갈 수 있기 때문에 아래의 예제코드를 통해서 포인터와 struct 사용에 대해서 확실히 이해하고 넘어가도록 하자. #include // Define the Player structu.. 2023. 10. 2.
[C Examples] 누가 시험을 잘봤을까#2 (exam highest, lowest, average): for loop, array, if statement, void) 누가 시험을 잘 봤을까#2 (exam highest, lowest, average): for loop, array, if statement, void) 포스트 난이도: HOO_Junior # Example codes 이번 예제코드는 "누가 시험을 잘 봤을까" 포스트에 이은 두 번째 버전의 예제코드이다. for loop, array, if statement를 사용하고 있는 건 기존 포스트에 나와있는 코드와 동일하지만 void를 사용해서 코드 구조 자체를 바꾸어 보았다. 이처럼 반복적으로 사용하는 기능에 대해서는 void를 사용해서 main과 구분하여 사용이 가능하다. 처음 c 프로그래밍을 배우는 학생들에게 있어서 main에 모든 걸 다 쏟아 부는 게 훨씬 쉽고 간단하게 느껴진다는 걸 글쓴이도 백 퍼센트 이.. 2023. 9. 19.
[C Examples] 누가 시험을 잘봤을까 (exam highest, lowest, average): for loop, array, if statement 누가 시험을 잘 봤을까 (exam highest, lowest, average): for loop, array, if statement 포스트 난이도: HOO_Intern # Example Codes 이번 포스트는 C언어를 기반으로 한 최댓값과 최솟값 그리고 평균값을 구해보는 예제코드이다. 예제코드에서 중점적으로 살펴봐야 할 부분은 배열을 어떻게 활용하고 있는지이다. 아래의 예제코드는 1차 배열을 활용하여 각 값들을 비교하여 원하는 값을 산출해내고 있다. #include int main() { int exam[] = { 70, 80, 94, 85, 47, 68, 98, 77, 85, 88 }; int highest = exam[0]; int lowest = exam[0]; float total = 0; .. 2023. 9. 19.
[C Examples] 고양이 중에 누가 가장 밥을 많이 먹나: for loop, 2 dimensional array, if statement 고양이 중에 누가 가장 밥을 많이 먹나: for loop, 2 dimensional array, if statement 포스트 난이도: HOO_Junior # Example codes 글쓴이는 동물 애호가이다. 오늘은 간단하면서도 재미있는 예제코드를 만들어보았다. 이번 예제코드에서는 고양이 3마리가 매일 얼마만큼의 밥을 먹으며, 최종적으로 누가 가장 많은 밥을 며칟날 먹었는지를 산출해 내는 걸 확인할 수 있다. 예제코드에서 유심히 살펴봐야 할 부분은 이차 배열을 활용하여 각각의 값들을 저장해주고 있으며, 이를 이중 for loop에서 어떻게 데이터 값들이 저장되고 if문에서 특정 값을 산출해 내기 위해 어떻게 계산이 되는 지이다. 거두절미하고 코드를 살펴보도록 하자. #include int main() .. 2023. 9. 18.
[C Examples] do while문을 사용해서 섭씨를 화씨로 변환하기 do while문을 사용해서 섭씨를 화씨로 변환하기 포스트 난이도: HOO_Intern # Example Codes 이번 예제코드를 통해서 섭씨를 화씨로 산출되는 걸 확인할 수 있다. 이번 코드에서는 do while문을 어떤 식으로 사용되고 있는지를 살펴볼 수 있다. do while문을 사용하지 않더라도 섭씨를 화씨로 계산하는 식을 작성함으로써 화씨 값이 산출이 되지만 do while문을 통해서 섭씨가 영하로 나오는 값이 계산이 안되게끔 설정이 가능하다. 예를 들어서 아래의 코드를 살펴보면, celsius 값이 0보다 작을 경우 다시 0보다 큰 값을 작성하도록 코드가 구성되어 있는 걸 볼 수 있다. 이처럼 do while문을 통해서 원하는 산출 값을 얻기 위한 반복적인 작업이 가능하다. #include .. 2023. 9. 3.
[C Examples] void와 array를 사용해서 시험 점수 평균과 가장 높은 점수 구하기 예제코드 void와 array를 사용해서 시험 점수 평균과 가장 높은 점수 구하기 예제코드 포스트 난이도: HOO_Junior # Example codes 이 예제코드의 핵심은 main function을 최대한 사용하지 않고 void와 array (배열)를 통해서 4개의 시험 점수를 비교해서 최댓값을 구하고 4개의 시험 점수에 대한 평균값을 구하는 것이다. 아래의 예제코드를 통해서 void와 array를 어떤 식으로 활용하는지를 살펴보고 이해할 수 있다. 또한 array를 사용하는 데 있어서 포인터를 활용하고 있기 때문에 포인터에 대해서 아래의 예제코드를 통해서 살펴볼 수 있다. 이 외에도 array에 각 값들을 저장하는 데 있어서 사용된 반복문인 for loop과 if else문을 통해서 각 값들을 비교 분석하.. 2023. 9. 3.
[C Examples] C 예제코드: 사칙연산 계산기 만들기, switch() C 예제코드: 사칙연산 계산기 만들기, switch() 포스트 난이도: HOO_Junior # Example Codes #include #include int main() { int num1,num2; float result; char ch; printf("Enter first number: "); scanf("%d",&num1); printf("Enter second number: "); scanf("%d",&num2); printf("Choose operation to perform (+,-,*,/,%): "); scanf(" %c", &ch); printf("You typed %c operator. \n", ch); switch(ch) { case '+': result=num1+num2; break.. 2023. 4. 14.
[C Examples] 입력된 숫자의 각 자리수를 더하고 3과 9로 나누어지는지 살펴보는 예제코드: do while(), if() 입력된 숫자의 각 자리수를 더하고 3과 9로 나누어지는지 살펴보는 예제코드: do while(), if() 포스트 난이도: HOO_Junior # Example Codes #include int main() { int num, digit, sum=0; printf("Enter a positive integer: "); scanf("%d", &num); do { digit = num % 10; sum += digit; num /= 10; } while (num != 0); printf("The sum of the digits = %d\n", sum); num = sum; do { num -= 3; } while (num >= 3); if (num == 0) { printf("%d is divisible b.. 2023. 3. 23.
[C Examples] 입력한 온도 값에 따라 온도 상태 구분하기 예제 코드: if(), else if(), while() 입력한 온도 값에 따라 온도 상태 구분하기 예제 코드: if(), else if(), while() 포스트 난이도: HOO_Junior # Example codes #include int main(){ int temperature[50]; int n,i,hot=0,pleasant=0,cold=0; float average=0; printf("Enter number of temperatures:"); scanf("%d",&n); printf("Enter Temperatures: "); while(i=60&&temperature[i] 2023. 3. 22.
[C Examples] 시간대별 통화요금 계산기 예제코드: if, else, char 시간대별 통화요금 계산기 예제코드: if, else, char 포스트 난이도: HOO_Junior # Example Codes #include #include int main() { int min, dayOrNight; float rCharge, pCharge; char serviceCode; printf("The rates are computed as follows: Regular service: $10.00 plus first 50 minutes are free.\n"); printf("Charges for over 50 minutes are $0.20 per minute, Premium service: $25.00 plus. \n"); printf("Enter your service code:");.. 2023. 2. 20.
[C Examples] 세금 계산기 예제코드: if, else if 세금 계산기 예제코드: if, else if 포스트 난이도: HOO_Intern # Example Codes #include #include int main() { float salary,tax; tax = 0; printf("Income: $"); scanf("%f",&salary); if (salary < 1000) { tax = 0.01 * salary; } else if (salary < 2000) { salary=salary-1000; tax = 0.01 * salary; tax = tax + 10; } else if (salary 2023. 2. 19.
[C Examples] 7 Digit ISSN, 8번째 숫자 산출하기 예제코드: if(), else(), %d 7 Digit ISSN, 8번째 숫자 산출하기 예제코드: if(), else(), %d 포스트 난이도: HOO_Junior # Example Codes #include int main() { int frontISSN, frontISSNOne, frontISSNTwo, frontISSNThree, frontISSNFour, backISSN, backISSNOne, backISSNTwo, backISSNThree, backISSNPer, modNum, unknownNum; printf("Enter the 7 digit ISSN in form of XXXX-XXX:"); scanf("%d - %d", &frontISSN, &backISSN); frontISSNOne = frontISSN/1000; frontI.. 2023. 2. 15.
[C Examples] 달러를 동전으로 바꾸는 예제코드 달러를 동전으로 바꾸는 예제코드 포스트 난이도: HOO_Intern # Example Codes #include int main() { int cents, halfDollars, quarters, dimes, nickels, pennies; printf("Enter change in [cents]: "); scanf("%d",&cents); printf("The change that has been entered is %d \n",cents); halfDollars = (cents*2)/100; printf("Half dollars: %d \n",halfDollars); cents=cents - ((halfDollars*100)/2); quarters=cents/25; printf("Quarters: %.. 2023. 2. 8.
[C Examples/Q&A] 체질량 지수(BMI) 계산하는 예제코드: if, else if, && 체질량 지수(BMI) 계산하는 예제코드: if, else if, && 포스트 난이도: HOO_Junior # Example code 1 #include #include int main() { int bmi, height, weight; printf("Enter the weight: "); scanf("%d",&weight); printf("Enter the height: "); scanf("%d", &height); bmi= (703*weight)/(height*height); if(bmi< 18.5) printf("Underweight"); if(18.5 2023. 2. 7.
[C Examples] 적금 계산기 적금 계산기 포스트 난이도: HOO_Intern # Example codes #include #include int main() { float A, r; int P, t; printf("Enter inital balance\n"); scanf("%d", &P); printf("Enter Annual interest rate\n"); scanf("%f", &r); printf("Enter how many years\n"); scanf("%d", &t); A=P*(1+r*t); printf("The balance after %d years will be (%.2f $)\n", t, A); printf("Posted by HOO."); return 0; } Enter inital balance 3000 Ent.. 2023. 1. 31.
[C Examples] 킬로미터(Km)를 마일(Miles)로 변환하기: scaf(), %.f 킬로미터(Km)를 마일(Miles)로 변환하기: scaf(), %.f 포스트 난이도: HOO_Intern # Example codes #include #include int main() { float miles, distance; printf("Enter the distance (in Km): \n"); scanf("%f", &distance); miles= (distance/1.6); printf("%.2f Kilometers = %.2f Miles\n", distance, miles); printf("Posted by HOO."); return 0; } Enter the distance (in Km): 5600 5600.00 Kilometers = 3500.00 Miles Posted by HOO. 2023. 1. 31.
[C Examples] 총 지불액 계산기(팁, 세금 포함): float, scanf, %.f 총 지불액 계산기(팁, 세금 포함): float, scanf, %.f 포스트 난이도: HOO_Intern # Example codes #include #include int main() { int numPeople; float bill, tips, tax, billAfter, amountPerson, tipPerson; printf("총 액수를 입력하세요.\n"); scanf("%f",&bill); printf("총 사람 수를 입력하세요.\n"); scanf("%d",&numPeople); printf("Tip percentage를 입력하세요.\n"); scanf("%f",&tips); tax = bill*0.05; printf("Tax: %.2f $\n", tax); billAfter=bill+tax;.. 2023. 1. 30.
[C Examples] 도시의 인구 수 예측하기, 예제코드: scanf(), putchar() 도시의 인구 수 예측하기, 예제코드: scanf(), putchar() 포스트 난이도: HOO_Intern # Example Codes #include int main() { double hooPopulation; int t; printf("Enter a year after 2022>"); putchar ('\n'); scanf("%d",&t); hooPopulation = 88.732+(5.889*(t-2022)); printf("Predicted HOO City population for %d (in thousands): ",t); putchar ('\n'); printf("%.5f",hooPopulation); putchar ('\n'); printf("Posted by HOO"); return 0.. 2023. 1. 30.
728x90