[C Question] 더블 포인터와 구조체
카테고리: c-question-lv1
1. 과목과 총점구하기 ( 전역 변수 )Permalink
#include <stdio.h>
int record[5][5];
void writeRecord();
void writeSumRecord();
void showAllRecord();
int main()
{
writeRecord(); // 입력
writeSumRecord(); // 총점 구하는 함수 ( 9칸 )
showAllRecord(); // 값을 보여줌
return 0;
}
조건 : main은 어떤 것도 건드려서는 안 된다 !
정답
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start | |
// #include "header.h" | |
#include <stdio.h> | |
int record[5][5]; | |
void writeRecord(); | |
void writeSumRecord(); | |
void showAllRecord(); | |
int main() | |
{ | |
/* | |
성적관리 프로그램 작성 | |
4과목이고 학생은 네 사람이다 ! | |
이 네사람의 네 과목 점수를 입력받는다. | |
마지막 열은 총점 | |
미리 선언해 놓은 배열에 다음의 형태로 그 값을 저장. | |
메인은 지금 이 상태에서 | |
그 어떤 것도 만지면 안됨 -> 애초에 만질 필요가 없음 | |
No Touch Zone임 | |
*/ | |
writeRecord(); // 입력 | |
writeSumRecord(); // 총점 구하는 함수 ( 9칸 ) | |
showAllRecord(); // 값을 보여줌 | |
return 0; | |
} | |
void writeRecord() | |
{ | |
int i, j; | |
for (i = 0; i < 4; i++) | |
{ | |
printf("%d번째 학생의 성적 입력 \n", i + 1); | |
for (j = 0; j < 4; j++) | |
{ | |
printf("과목 %d: ", j + 1); | |
scanf_s("%d", &record[i][j]); | |
} | |
} | |
} | |
/* | |
0,4 1,4 2,4 3,4 | |
4,4 | |
4,0 4,1 4,2 4,3 | |
*/ | |
void writeSumRecord() | |
{ | |
int i, j; | |
for (i = 0; i < 4; i++) | |
{ | |
for (j = 0; j < 4; j++) | |
{ | |
record[i][4] += record[i][j]; // 오른쪽 합계 | |
record[4][j] += record[i][j]; // 밑 합계 | |
} | |
record[4][4] += record[i][4]; // 총 합계 // 아하 ~ | |
} | |
} | |
void showAllRecord() | |
{ | |
int i, j; | |
for (i = 0; i < 5; i++) | |
{ | |
for (j = 0; j < 5; j++) | |
printf("%3d ", record[i][j]); | |
printf("\n"); | |
} | |
} | |
// ---------------------- 2번째 버전 ---------------------- | |
void writeRecord() | |
{ | |
int i, j; | |
for (i = 0; i < 4; i++) | |
{ | |
printf("%d번째 학생의 성적 입력 \n", i + 1); | |
for (j = 0; j < 4; j++) | |
{ | |
printf("과목 %d: ", j + 1); | |
scanf_s("%d", &record[i][j]); | |
record[i][4] += record[i][j]; | |
} | |
} | |
} | |
// record[i][4] += record[i][j]; // 오른쪽 합계 | |
void writeSumRecord() | |
{ | |
int i, j; | |
for (i = 0; i < 4; i++) | |
{ | |
for (j = 0; j < 4; j++) | |
record[4][j] += record[i][j]; // 밑 합계 | |
record[4][4] += record[i][4]; // 총 합계 // 아하 ~ | |
} | |
} |
2. 더블 포인터의 활용Permalink
#include <stdio.h>
void MaxAndMin(int* arr, int len, int** maxPtr, int** minPtr);
int main()
{
/* 연습문제
다음과 같이 두개의 int형 포인터 변수와 길이가 5인 int형 배열을 선언한다.
그리고 MaxAndMin이란 이름의 함수를 정의
호출하면서 두 포인터 변수에 대한 정보를 전달
어떠한 정보를 어떻게 전달할지는 스스로 결정
함수 호출이 완료되면, 포인터 변수 maxPtr에는 가장 큰 값이 저장된
배열요소의 주소 값!!!이,
minPtr에는 가장 작은 값이 저장된 배열 요소의 주소 값!!!!이 저장됨.
*/
int* maxPtr;
int* minPtr;
int arr[5];
int i, len;
len = sizeof(arr) / sizeof(int);
// 배열에 값 입력
for (i = 0; i < len; i++)
{
printf("정수 입력 %d : ", i + 1);
scanf_s("%d", &arr[i]);
}
// 함수 호출
// 출력
printf("최대: %d, 최소: %d", *maxPtr, *minPtr);
return 0;
}
조건
메인 5줄 ( 변수 빼고 )
함수 6줄
정답
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start | |
// #include "header.h" | |
#include <stdio.h> | |
void MaxAndMin(int* arr, int len, int** maxPtr, int** minPtr); | |
int main() | |
{ | |
/* 연습문제 | |
다음과 같이 두개의 int형 포인터 변수와 길이가 5인 int형 배열을 선언한다. | |
그리고 MaxAndMin이란 이름의 함수를 정의 | |
호출하면서 두 포인터 변수에 대한 정보를 전달 | |
어떠한 정보를 어떻게 전달할지는 스스로 결정 | |
함수 호출이 완료되면, 포인터 변수 maxPtr에는 가장 큰 값이 저장된 | |
배열요소의 주소 값!!!이, | |
minPtr에는 가장 작은 값이 저장된 배열 요소의 주소 값!!!!이 저장됨. | |
메인 5줄 ( 변수 빼고 ) | |
함수 6줄 | |
*/ | |
int* maxPtr; | |
int* minPtr; | |
int arr[5]; | |
int i, len; | |
len = sizeof(arr) / sizeof(int); | |
// 배열에 값 입력 | |
for (i = 0; i < len; i++) | |
{ | |
printf("정수 입력 %d : ", i + 1); | |
scanf_s("%d", &arr[i]); | |
} | |
// 함수 호출 | |
MaxAndMin(arr, len, &maxPtr, &minPtr); | |
printf("최대: %d, 최소: %d", *maxPtr, *minPtr); | |
return 0; | |
} | |
// 포인터 변수의 주소값을 받는 더블포인터 dM??Ptr | |
void MaxAndMin(int* arr, int len, int** dMaxPtr, int** dMinPtr) | |
{ | |
int i; | |
// 1줄로 | |
*dMinPtr = *dMaxPtr = &arr[0]; // 주소값을 arr의 주소값으로 선언 | |
// 주소값을 변경하는 반복문을 써야할 듯? | |
for (i = 1; i < len; i++) | |
{ | |
if (**dMaxPtr < arr[i]) *dMaxPtr = &arr[i]; // +1 없이 | |
if (**dMinPtr > arr[i]) *dMinPtr = &arr[i]; | |
} | |
} |
3. 직원 구조체Permalink
#include <stdio.h>
struct employee
{
char name[50];
char addr_num[15];
int salary;
};
int main()
{
/* 연습문제
문자열 형태의 종업원 이름과 주민등록번호
그리고 정수 형태의 급여정보를 저장할 수 있는 employee라는 이름의 구조체 정의
구조체 변수를 하나 선언한 다음 사용자 입력받아서 변수를 채우고,
구조체 변수에 채워진 데이터를 출력한다.
*/
struct employee salary_info;
// 입력
// 출력
printf("--- 종합 정보--- \n\n이름: %s \n주민등록번호: %s \n월급은?: %d", salary_info.name, salary_info.addr_num, salary_info.salary);
return 0;
}
정답
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start | |
// #include "header.h" | |
#include <stdio.h> | |
struct employee | |
{ | |
char name[50]; | |
char addr_num[15]; | |
int salary; | |
}; | |
int main() | |
{ | |
/* 연습문제 | |
문자열 형태의 종업원 이름과 주민등록번호 | |
그리고 정수 형태의 급여정보를 저장할 수 있는 employee라는 이름의 구조체 정의 | |
구조체 변수를 하나 선언한 다음 사용자 입력받아서 변수를 채우고, | |
구조체 변수에 채워진 데이터를 출력한다. | |
*/ | |
struct employee salary_info; | |
fputs("이름은?: ", stdout); | |
scanf_s("%s", salary_info.name, (unsigned int)sizeof(salary_info.name)); | |
fputs("주민등록번호는?: ", stdout); | |
scanf_s("%s", salary_info.addr_num, (unsigned int)sizeof(salary_info.addr_num)); | |
fputs("월급은?: ", stdout); | |
scanf_s("%d", &salary_info.salary); | |
printf("--- 종합 정보--- \n\n이름: %s \n주민등록번호: %s \n월급은?: %d", salary_info.name, salary_info.addr_num, salary_info.salary); | |
return 0; | |
} |
댓글 남기기