More actions
imported>Unknown No edit summary |
(Repair batch-0007 pages from live compare) |
||
| Line 19: | Line 19: | ||
#define MATRIX_SIZE 4 | #define MATRIX_SIZE 4 | ||
void search_max(int matrix | void search_max(int matrix[][MATRIX_SIZE]); | ||
void print_matrix(int matrix | void print_matrix(int matrix[][MATRIX_SIZE]); | ||
void change(int *, int *); | void change(int *, int *); | ||
| Line 26: | Line 26: | ||
void main() | void main() | ||
{ | { | ||
int matrix | int matrix[MATRIX_SIZE][MATRIX_SIZE] = {{0,}}; | ||
//3 * 3 입력 . | //3 * 3 입력 . | ||
for(i = 0; i < MATRIX_SIZE - 1; i++){ | for(i = 0; i < MATRIX_SIZE - 1; i++){ | ||
for(j = 0; j < MATRIX_SIZE -1; j++){ | for(j = 0; j < MATRIX_SIZE -1; j++){ | ||
printf("matrix | printf("matrix[%d][%d] = ", i, j); | ||
scanf("%d", &matrix | scanf("%d", &matrix[i][j]); | ||
} | } | ||
} | } | ||
| Line 40: | Line 40: | ||
} | } | ||
void search_max(int matrix | void search_max(int matrix[][MATRIX_SIZE]) | ||
{ | { | ||
int temp_matrix | int temp_matrix[4][4], a, b; | ||
for(i = 0; i < MATRIX_SIZE; i++){ | for(i = 0; i < MATRIX_SIZE; i++){ | ||
for(j = 0; j < MATRIX_SIZE; j++){ | for(j = 0; j < MATRIX_SIZE; j++){ | ||
temp_matrix | temp_matrix[i][j] = matrix[i][j]; | ||
} | } | ||
} | } | ||
| Line 51: | Line 51: | ||
for(i = 0; i < MATRIX_SIZE-1; i++){ | for(i = 0; i < MATRIX_SIZE-1; i++){ | ||
for(j = 0; j < MATRIX_SIZE-2; j++){ | for(j = 0; j < MATRIX_SIZE-2; j++){ | ||
if(temp_matrix | if(temp_matrix[i][j] > temp_matrix[i][j+1]){ | ||
change(&temp_matrix | change(&temp_matrix[i][j], &temp_matrix[i][j+1]); | ||
} | } | ||
} | } | ||
matrix | matrix[i][MATRIX_SIZE-1] = temp_matrix[i][MATRIX_SIZE-2]; | ||
} | } | ||
for(i = 0; i < MATRIX_SIZE; i++){ | for(i = 0; i < MATRIX_SIZE; i++){ | ||
for(j = 0; j < MATRIX_SIZE; j++){ | for(j = 0; j < MATRIX_SIZE; j++){ | ||
temp_matrix | temp_matrix[i][j] = matrix[i][j]; | ||
} | } | ||
} | } | ||
| Line 65: | Line 65: | ||
for(i = 0; i < MATRIX_SIZE-1; i++){ | for(i = 0; i < MATRIX_SIZE-1; i++){ | ||
for(j = 0; j < MATRIX_SIZE-2; j++){ | for(j = 0; j < MATRIX_SIZE-2; j++){ | ||
if(temp_matrix | if(temp_matrix[j][i] > temp_matrix[j+1][i]){ | ||
change(&temp_matrix | change(&temp_matrix[j][i], &temp_matrix[j+1][i]); | ||
} | } | ||
} | } | ||
matrix | matrix[MATRIX_SIZE-1][i] = temp_matrix[MATRIX_SIZE-2][i]; | ||
} | } | ||
//찾아 넣기 마지막 | //찾아 넣기 마지막 | ||
for(i = 0; i < MATRIX_SIZE-2; i++){ | for(i = 0; i < MATRIX_SIZE-2; i++){ | ||
if(temp_matrix | if(temp_matrix[MATRIX_SIZE-1][i] > temp_matrix[MATRIX_SIZE-1][i+1]){ | ||
change(&temp_matrix | change(&temp_matrix[MATRIX_SIZE-1][i], &temp_matrix[MATRIX_SIZE-1][i+1]); | ||
} | } | ||
if(temp_matrix | if(temp_matrix[i][MATRIX_SIZE-1] > temp_matrix[i+1][MATRIX_SIZE-1]){ | ||
change(&temp_matrix | change(&temp_matrix[i][MATRIX_SIZE-1], &temp_matrix[i+1][MATRIX_SIZE-1]); | ||
} | } | ||
} | } | ||
a = temp_matrix | a = temp_matrix[MATRIX_SIZE-1][MATRIX_SIZE-2]; | ||
b = temp_matrix | b = temp_matrix[MATRIX_SIZE-2][MATRIX_SIZE-1]; | ||
matrix | matrix[MATRIX_SIZE-1][MATRIX_SIZE-1] = a > b ? a : b; | ||
} | } | ||
void print_matrix(int matrix | void print_matrix(int matrix[][MATRIX_SIZE]) | ||
{ | { | ||
for(i = 0; i < MATRIX_SIZE; i++){ | for(i = 0; i < MATRIX_SIZE; i++){ | ||
for(j = 0; j < MATRIX_SIZE; j++){ | for(j = 0; j < MATRIX_SIZE; j++){ | ||
printf("%5d", matrix | printf("%5d", matrix[i][j]); | ||
} | } | ||
if(j == MATRIX_SIZE) | if(j == MATRIX_SIZE) | ||
| Line 121: | Line 121: | ||
---- | ---- | ||
[[LittleAOI]] [[큰수찾아저장하기]] | [[LittleAOI]] [[큰수찾아저장하기]] | ||
Latest revision as of 01:32, 27 March 2026
소 감
기존의 나열된 자료와는 다른 , 이차배열의 각각의 소트.. 생각보다 어려웠다. 행과 열이 같이 for속에 들어가게 되면 값이 틀려져서 따로 처리하다보니 소스가 길어졌다. 더 좋은 방법이 있을까??
물론 이런 생각도 해 보았다. matrix를 temp1,2 matrix를 만들어서 행과 열을 따로 계산한다.. 하지만 이번에 내 생각에 변수 낭비될 것 같고 해서 그냥 matrix 복사를 한번 더 했다.
- 나름대로 sort에 대해 여러가지 생각을 하게 된 계기가 된 것 같다.
연습장에 연필로 끄적끄적거려 보기도 하고.. ^^
추가: 시간이 많이 걸린 이유 - 역시나 배열을 함수화 하는 것에서, 새로운 방법이 없을까 궁리하다가,
또 더 좀더 효율적인 프로그램을 만들기 위해선 어떻게 해야할까 ? 그리고 C언어의 장점을 살리는 방법이 뭘까? 하고 생각해 봤다. 그래서 생각하면서 프로그래밍 한 것, 또 자초해서 해 버린 소트 때문에 시간이 많이 걸린 것 같다.
소 스
#include <stdio.h>
#define MATRIX_SIZE 4
void search_max(int matrix[][MATRIX_SIZE]);
void print_matrix(int matrix[][MATRIX_SIZE]);
void change(int *, int *);
int i, j;
void main()
{
int matrix[MATRIX_SIZE][MATRIX_SIZE] = Template:0,;
//3 * 3 입력 .
for(i = 0; i < MATRIX_SIZE - 1; i++){
for(j = 0; j < MATRIX_SIZE -1; j++){
printf("matrix[%d][%d] = ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// 가장 큰 수 찾기.
search_max(matrix);
//출력
print_matrix(matrix);
}
void search_max(int matrix[][MATRIX_SIZE])
{
int temp_matrix[4][4], a, b;
for(i = 0; i < MATRIX_SIZE; i++){
for(j = 0; j < MATRIX_SIZE; j++){
temp_matrix[i][j] = matrix[i][j];
}
}
//찾아 넣기 행
for(i = 0; i < MATRIX_SIZE-1; i++){
for(j = 0; j < MATRIX_SIZE-2; j++){
if(temp_matrix[i][j] > temp_matrix[i][j+1]){
change(&temp_matrix[i][j], &temp_matrix[i][j+1]);
}
}
matrix[i][MATRIX_SIZE-1] = temp_matrix[i][MATRIX_SIZE-2];
}
for(i = 0; i < MATRIX_SIZE; i++){
for(j = 0; j < MATRIX_SIZE; j++){
temp_matrix[i][j] = matrix[i][j];
}
}
//찾아 넣기 열
for(i = 0; i < MATRIX_SIZE-1; i++){
for(j = 0; j < MATRIX_SIZE-2; j++){
if(temp_matrix[j][i] > temp_matrix[j+1][i]){
change(&temp_matrix[j][i], &temp_matrix[j+1][i]);
}
}
matrix[MATRIX_SIZE-1][i] = temp_matrix[MATRIX_SIZE-2][i];
}
//찾아 넣기 마지막
for(i = 0; i < MATRIX_SIZE-2; i++){
if(temp_matrix[MATRIX_SIZE-1][i] > temp_matrix[MATRIX_SIZE-1][i+1]){
change(&temp_matrix[MATRIX_SIZE-1][i], &temp_matrix[MATRIX_SIZE-1][i+1]);
}
if(temp_matrix[i][MATRIX_SIZE-1] > temp_matrix[i+1][MATRIX_SIZE-1]){
change(&temp_matrix[i][MATRIX_SIZE-1], &temp_matrix[i+1][MATRIX_SIZE-1]);
}
}
a = temp_matrix[MATRIX_SIZE-1][MATRIX_SIZE-2];
b = temp_matrix[MATRIX_SIZE-2][MATRIX_SIZE-1];
matrix[MATRIX_SIZE-1][MATRIX_SIZE-1] = a > b ? a : b;
}
void print_matrix(int matrix[][MATRIX_SIZE])
{
for(i = 0; i < MATRIX_SIZE; i++){
for(j = 0; j < MATRIX_SIZE; j++){
printf("%5d", matrix[i][j]);
}
if(j == MATRIX_SIZE)
printf("\n");
}
}
void change(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
나한테 할 말
각 기능별로 함수로 나누는건 좋은 생각인데 말야..^^ 그게 오히려 문제가 된것 같은 느낌이..ㅎㅎ 다해서 한페이지 안에 들어갈 정도로 간단하다면 그냥 짜넣어도 문제는 없을것 같아..^^ -조현태
그러고 보니 소스가 소트를 하네? 문제에는 정렬하란말이 없었는데..;; 음.. 설마 깔끔하게 출력하란말이 그런뜻인거야??;;;;; 설마아~ - 조현태
일부러 소트 했어 ~ 쓰고 싶더라고 ㅋㅋ , 나름대로 다 하고나서 희열을 느꼈는데..^^;; --아영 으음... 이거 가로먼저 소트하는거랑 세로먼저 소트하는거랑 답이 다르게 나올텐데..ㅎㅎ - 조현태
소트한건 쓰레기배열이니 문제없는거같은데 -_-;; 원래배열은 고대로 있잖아 --정수민
아..두번이었네..ㅎㅎ 한번만 복사하는줄 알았으..ㅎㅎ - 조현태 소감좀 읽어달라구 ㅋㅋ 너희들이 얘기한 것은 소감에 다 있는데 말이야 ㅋㅋ --아영 분명히 열심히 읽는데 말이야..ㅎㅎㅎ 문제는 내 머리의 메모리가 작아서 몇몇 부분은 기억을 잘 못한다고...ㅎㅎㅎ 그러니 다 적힌내용으로 삽질하고 있어도 너그러운 아량으로 이해해주시길..ㅎㅎㅎ - 조현태