일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- join
- 우선순위큐
- 그리디
- 누적합
- DFS
- 그래프 이론
- 분할정복
- 시뮬레이션
- 구현
- 다시
- 백트래킹
- 서브쿼리
- 다익스트라
- 해시
- 재귀
- GROUP BY
- BFS
- 트리
- DP
- 에라토스테네스의 체
- 플로이드-워셜
- 다이나믹프로그래밍
- 투포인터
- 수학
- 그래프 탐색
- 자료구조
- 다이나믹 프로그래밍
- 크루스칼
- 브루트포스
- MST
- Today
- Total
기록하고 까먹지 말기
Final Coding Test 본문
#include <stdio.h>
int main(){
// declaring variables for program
int n, i, search=0;
printf("Enter size of array : ");
scanf("%d", &n); // entering size of array
if(n <= 0 || n > 10){ //checking whether entered size of array in in required range
printf("%d is an invalid value.", n); // if not, end program
return 0;
}
// declaring array based on entered size
int arr[n];
int* ptr = arr; // pointing array
printf("Enter elements in array: "); // entering elements from user with pointer
for(i=0; i<n; i++){
scanf("%d", (ptr+i));
}
printf("Enter element to search: "); // entering number to search
scanf("%d", &search);
// searching in array
for(i=0; i<n; i++){
if(*(ptr+i) == search){ // if found
printf("%d is found at %d position.", search, (i+1)); // printing output
return 0;
}
}
// if not
printf("%d is an invalid search value.", search); // printing output
return 0;
}
array를 입력받고 원하는 element를 search하는 프로그램이다.
#include <stdio.h>
int main(){
// declaring arrays and variables
int arr1[3][3];
int arr2[3][3];
int i, j;
// entering elements of first matrix
printf("Enter elements in first matrix\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++){
scanf("%d", &arr1[i][j]);
}
}
// entering elements of second matrix
printf("\nEnter elements in second matrix\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++){
scanf("%d", &arr2[i][j]);
arr1[i][j]+=arr2[i][j]; // calculating sum of both matrices with entering second matrix's element
}
}
// printing output
printf("Sum of both matices is:\n");
for(i=0; i<3; i++){
for(j=0; j<3; j++){
printf("%d ", arr1[i][j]);
}
printf("\n");
}
return 0;
}
2개의 3 x 3 행렬을 입력받은 후 행렬의 덧셈 결과를 출력하는 프로그램이다.
두 번째 행렬을 입력받는 동시에 연산을 진행하였다.
#include <stdio.h>
void pascal(int n){
int arr[n][n]; // declaring array size n * n
int i, j=0;
for(i=0; i<n; i++){
for(j=0; j<=i; j++){
if(j == 0 || j == i){
arr[i][j] = 1; // setting first and last column of array
continue; // going to next column
}
arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; // setting values in array
}
}
// printing output
for(i=0; i<n; i++){
for(j=0; j<=i; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
return;
}
int main(){
int n, i;
// Entering number of rows
printf("Enter number of rows(max 100) : ");
scanf("%d", &n);
// if entered number is not in range
if(n < 1 || n > 100){
printf("Invalid size!!");
return 0; // end program
}
pascal(n); // sending value into function
return 0;
}
파스칼의 삼각형을 구현하는 문제다.
과제에서는 공백까지 고려했지만 이번 시험에서는 공백 없이 그냥 출력만 하면 되었기게 큰 어려움은 없었다.
#include <stdio.h>
#include <stdlib.h>
int main(){
// initiating variables
int n, i;
double largest;
// Entering total size that will be entered
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
// if size is not in range
if(n < 1 || n > 100){
printf("Invalid value");
return 0; // end program
}
// declaring array with allocation in memory
double* arr = (double*)malloc(sizeof(double) * n);
// Entering element
for(i=0; i<n; i++){
printf("Enter element %d : ", (i+1));
scanf("%lf", (arr+i));
if(i==0) // initiating largest value into first element
largest = *(arr);
if(largest < *(arr+i)) // setting largest element
largest = *(arr+i);
}
//printing output
printf("Largest Element = %.2lf", largest);
return 0;
}
double형 포인터를 통해 다수의 값을 입력받고 그 중에서 가장 큰 수를 출력하는 프로그램이다.
아마 조건에 동적 할당과 포인터를 활용하라고 했어서 이렇게 코드를 작성했던 것 같다.
이것도 과제에 나왔던 내용이라 큰 어려움은 없었다.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
// declaring vunfcions
void Add();
void Display();
void Swap();
void Exit();
// declaring structure
typedef struct{
int id;
int point;
} Person;
// initiating structure as global structure
Person* arr[MAX];
static int cnt = 0; // declaring global static variable for counting
int main(){
int sel = 0;
while(1){
// printing main menu
printf("1: Add\n");
printf("2: Display\n");
printf("3: Swap\n");
printf("4: Exit\n");
scanf("%d", &sel); // inputs value from user
switch(sel){
case 1:{
Add();
break;
}
case 2:{
Display();
break;
}
case 3:{
Swap();
break;
}
case 4:{
Exit();
return 0; // finishing program
break;
}
default:{
// when entered number is not in range
printf("Enter correct number!(1 ~ 4)\n\n");
break;
}
}
}
}
void Add(){
int i=0;
// allocating memory
arr[cnt] = (Person*)malloc(sizeof(Person*));
// inputing id and point from user
printf("Enter ID(only integer): ");
scanf("%d", &arr[cnt]->id);
printf("Enter Point: ");
scanf("%d", &arr[cnt]->point);
cnt++; // increaing total count of array
printf("\n");
return; // back to main menu
}
void Swap(){
int id, i=0;
// entering id for swap point
printf("ID: ");
scanf("%d", &id);
for(i=0; i<cnt; i++){
// searching id
if(arr[i]->id == id){ // if found
printf("Point: ");
scanf("%d", &arr[i]->point); // entering new point matching same id
printf("\n");
return; // back to main menu
}
}
printf("There is no matching ID\n\n");
return;
}
void Display(){
int i=0;
// printing entered id and point
printf("ID-Point\n");
for(i=0; i<cnt; i++)
printf("%d-%d\n", arr[i]->id, arr[i]->point);
printf("\n");
return; // back to main menu
}
void Exit(){
int i=0;
for(i=0; i<cnt; i++){
free(arr[i]); // de-allocating memory
}
printf("bye!"); // printing output
return; // back to main menu
}
작은 프로젝트성 문제였다.
이전 과제와 상당히 유사한 문제였다. 이것도 구조체와 공적할당, 포인터를 적절하게 사용해야 풀 수 있는 문제였다.
#include <stdio.h>
// declaring node
struct node{
int value;
struct node* next;
};
// declaring functions
void Insert();
void Display();
void Exit();
// declaring chain node
struct node mynode[100];
static int cnt = 0; // counting having element
int main(){
int sel = 0;
while(1){
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Exit\n");
scanf("%d", &sel); // entering value for selecting what to do
switch(sel){
case 1:{
Insert();
break;
}
case 2:{
Display();
break;
}
case 3:{
printf("Bye!\n");
return 0;
break;
}
}
}
}
void Insert(){
int i;
printf("Enter value want to insert : "); // enteing value
scanf("%d", &mynode[cnt].value); // saving value into node
cnt++;
if(cnt == 1){
for(i=0; i<cnt-1; i++){
mynode[i].next = &mynode[i+1]; // pointing next node with pointer(implementing chain node)
}
}
mynode[cnt-1].next = &mynode[0]; // end of node points first node
printf("\n");
}
void Display(){
int i=0;
printf("List items are: ");
for(i=0; i<cnt; i++){
printf("%d ", mynode[i].next->value); // printing elements with linked list
}
printf("\n");
}
이건 개인적으로 가장 어렵게(?) 느껴졌던 문제였다.
자료구조의 linked list를 설명하라는 문제였던 것 같은데....... 질문하니까 문제에 관해서는 답을 할 수 없다고 했었기에 그냥 linked list를 구현하려고 했다. 그런데 이 문제는 아마 틀렸지 않았나 싶다.
Linked List, 즉 연결 리스트라 한다면 각 구조체로 나타나는 노드는 하나의 int형 변수와 다음 노드를 가리키는 포인터 변수로 구성되어 있고 꼬리를 물듯이 형성되어 있다.
마치 이 그림처럼 나타나 있는데..... 시간이 부족해서인지, 시험이라 긴장해서인지 계속 첫 원하는 아웃풋이 나오지 않아 굉장히 짜증이 났던 기억이 난다...ㅋㅋ
지난 Data Structure를 수강할 때에도 개념은 알고 있었지만 소스코드 작성에서 꽤나 애를 먹었는데;;;;
아무래도 이번주에는 data structure를 한 번 더 공부하라는 의미로 받아들이는 것이 좋을 것 같다.
'전공 > C' 카테고리의 다른 글
과제11 - final (2) (0) | 2021.06.22 |
---|---|
과제11 - final (1) (0) | 2021.06.22 |
과제10 - StructUnion (1 ~ 7) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (6 ~ 9) (0) | 2021.06.22 |
과제9 - Pointer Dynamic (1 ~ 5) (0) | 2021.06.22 |