게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
2차 동적 배열 할당.. 처음엔 되는데 두번째는 안되요..ㅠ 왜이런가요.
게시물ID : programmer_14405짧은주소 복사하기
작성자 : 복전곰탱이
추천 : 0
조회수 : 324회
댓글수 : 1개
등록시간 : 2015/11/13 05:38:43
일단.. 프로그램은.. 경유지의 숫자를 입력하고, tour에 경유지(캐릭터형 대문자)를 입력한 뒤, 
파일에 저장된 (가로24, 세로 30)파일에서 (1,1)이면 1에 A가, (1,3)이면 1에 C가.. 이런식으로 해서 1인것들만 넣어서
tourBus로 동적 배열을 만들었습니다..

place 배열은 tour의 문자가 tourBus에 있으면 그 인덱스를 저장하는 배열입니다.
따라서 place배열은 세로 크기는 number(=tour의 크기)이고, 가로 크기는 각각 다 다르게 나옵니다..

그런데!!  빨간색 부분인 place배열의 가로 크기가.. 이상하게 다 4로 나옵니다..ㅠ
초록색인 count는 그에 맞게 값이 나오는데.. 무조건 place의 가로 크기가 4에요..ㅠ
왜 이러는건가요..ㅠ?


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {
int number;
printf("가고 싶은 경유지의 숫자를 입력해주세요.\n 경유지의 수 : ");
scanf("%d", &number);
char* tour = (char*)malloc(sizeof(char)*(number));
printf("경유지를 입력해주세요.\n");
for(int i = 0; i < number; i++) {
printf("경유지 : ");
cin >> tour[i];
}
for(int i = 0; i< number; i++)
printf("%c ", tour[i]);
printf("\n");

char f = 'A';
int temp[24];
int count = 0; // tourBus를 만들기 위한 변수
ifstream infile;
int index = 0;
infile.open("input.txt");
char** tourBus = (char**)calloc(30,sizeof(char*));
for(int i = 0; i < 30; i++) {
for(int j = 0; j < 24; j++) {
infile >> temp[j];
if(temp[j] == 1) {
count++;
}
}
tourBus[i] = (char*)calloc(count,sizeof(char));
for(int j = 0; j < 24; j++) {
if(temp[j] == 1) {
tourBus[i][index] = 'A'+ j;
index++;
}
}
index = 0;
count = 0;
}

for(int i = 0; i < 30; i++) { 
for(int j = 0; j < sizeof(tourBus[i]); j++) {
printf("%c", tourBus[i][j]);
}
printf("\n");
}

int** place = (int**)calloc(number,sizeof(int*));
for(int i = 0; i < number; i++) {
for(int j = 0; j < 30; j++) {
for(int k = 0; k < sizeof(tourBus[j]); k++) {
if(tour[i] == tourBus[j][k]) {
count++;
break;
}
}
}
place[i] = (int*)calloc(count,sizeof(int));
for(int j = 0; j < 30; j++) {
for(int k = 0; k < sizeof(tourBus[j]); k++) {
if(tour[i] == tourBus[j][k]) {
place[i][index] = j;
index++;
break;
}
}
}
count = 0;
index = 0;
}
for(int i = 0; i < number; i++) {
for(int j = 0; j < sizeof(place[i]); j++)
printf("%d ", place[i][j]);
printf("\n");
}
int ab;
scanf("%d",&ab);
}


input.txt
1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0
0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0
0 0 1 0 1 1 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1
0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0

입력을 
A B C 
했을 때 결과입니다..
원래는 
0 5 13 25 26
1 2 10 20 26
6 12 15
가 나와야합니다..ㅠ

제목 없음.png

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호