2014-10-23 1 views
0

다른 배열에 2D 배열을 전달하고 싶습니다. 나는 주된 기능을 모두 가지고있는 코드 예제를 가지고있다. 그러나 코드를 지정하려면 코드를 main, adjmatrix 및 adjlist 함수의 총 세 가지 함수로 분할해야합니다. 2D 배열의 매개 변수를 C++로

주 기능의 모든

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

int main(void) 
{ 
    ifstream in; 

    char infile[40]; 
    int c, u, v; 

    cout << "Please enter the input data file name(NO SPACES): "; 
    cin >> infile; 

    in.open(infile); 
    while(in.fail()) { 
     cout << "Please enter a CORRECT input data file name(NO SPACES): "; 
     cin >> infile; 
     in.open(infile); 
    } 

    //adj matrix 

    cout << "Adjacency Matrix" << endl; 
    in >> c; 
    int array[c][c]; 
    for(int i=0; i<c; i++) { 
     for(int j=0; j<c; j++) { 
     array[i][j] = 0; 
     } 
    } 

    while(in >> u >> v) { 
     array[u][v] = 1; 
    } 

    cout << c << endl; 
    for(int i=0;i<c;i++) { 
     cout << i << " "; 
     for(int j=0;j<c;j++){ 
     cout << array[i][j] << " "; 
     } 
     cout << endl; 
    } 
    cout << endl; 

    //adj list 

    cout << "Adjacency List" << endl; 
    cout << c << endl; 
    for(int i=0;i<c;i++) { 
     cout << i << " --> "; 
     for(int j=0;j<c;j++) { 
     if(array[i][j] == 1) { 
      cout << j << " "; 
     } 
     } 
     cout << endl; 
    } 

    in.close(); 

    return 0; 
} 

이 프로그램의 출력을 다음 다음 입력 파일

9 
2 8 
0 6 
8 5 
2 4 
3 1 
2 3 
4 1 
6 1 
2 6 
7 5 
1 7 

에서 인접 행렬과 인접리스트이다 출력

Adjacency Matrix 
9 
0 0 0 0 0 0 0 1 0 0 
1 0 0 0 0 0 0 0 1 0 
2 0 0 0 1 1 0 1 0 1 
3 0 1 0 0 0 0 0 0 0 
4 0 1 0 0 0 0 0 0 0 
5 0 0 0 0 0 0 0 0 0 
6 0 1 0 0 0 0 0 0 0 
7 0 0 0 0 0 1 0 0 0 
8 0 0 0 0 0 1 0 0 0 

Adjacency List 
9 
0 --> 6 
1 --> 7 
2 --> 3 4 6 8 
3 --> 1 
4 --> 1 
5 --> 
6 --> 1 
7 --> 5 
8 --> 5 

나는 어딘가에 2D를 전달하는 것을 읽었다. 배열에는 두 번째 차원을 입력해야합니다. 나는 또한 그것에 대해 세계적인 상수가 되어야만하는 것을 읽었다. 그래서 나는 조금 미쳤을지도 모르고, 약간의 물건을 시험해 보는 것에 미쳤던 길을 떠나 갔다. 그래서 어리 석음의 약간을 변명 해주세요. 내가 생각하는 문제는 실제 배열 크기가 파일에서 오는 것이므로 int c이 입력 파일의 첫 번째 값으로 초기화 될 때 배열의 두 번째 차원을 실제 값으로 선언 할 위치를 실제로 이해하지 못합니다. 다음은 2D 배열을 전달하는 데 실패한 시도입니다. 즐기십시오 :

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

const int c; 

void adjmatrix(istream &in, int array[][c]); 

int main(void) 
{ 
    ifstream in; 

    char infile[40]; 

    cout << "Please enter the input data file name(NO SPACES): "; 
    cin >> infile; 

    in.open(infile); 
    while(in.fail()) { 
     cout << "Please enter a CORRECT input data file name(NO SPACES): "; 
     cin >> infile; 
     in.open(infile); 
    } 

    in >> c; 
    int array[c][c]; 

    adjmatrix(in, array); 

    in.close(); 

    return 0; 
} 

void adjmatrix(istream &in, int array[][c]) 
{ 
    int u,v; 
    for(int i=0; i<c; i++) { 
     for(int j=0; j<c; j++) { 
     array[i][j] = 0; 
     } 
    } 

    while(in >> u >> v) { 
     array[u][v] = 1; 
    } 

    cout << c << endl; 

    for(int i=0; i<c; i++) { 
     cout << i << " "; 
     for(int j=0; j<c; j++) { 
     cout << array[i][j] << " "; 
     } 
     cout << endl; 
    } 
    cout << endl; 
} 
+1

를 '>> C로; int array [c] [c];'이것은 표준 C++가 아닙니다. 컴파일 타임 식으로 배열 크기를 선언해야합니다. 'std :: vector >'을 사용하거나 어떤 이유로'vector'를 사용할 수없는 경우'int **'에서 시작해서 하나를 빌드하십시오. – PaulMcKenzie

답변

0

정적으로 정의하는 대신 배열을 동적으로 할당 할 수 있습니다.

int i; 
int **array; 

in >> c; 
array = new int*[c]; 

for(i=0;i<c;i++) 
    array[i] = new int[c]; 

adjmatrix(in, array,c); 

함수 adjmatrix의 선언은

+1

'delete []'를 잊지 마세요. – Jarod42

0

adjmatrix(istream &in, int **array,int c); 도움이 될 다음 될 것이다

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

void print_adjacency_matrix(const std::vector<std::vector<int>>& mat) 
{ 
    std::cout << "Adjacency Matrix" << std::endl; 

    std::cout << mat.size() << std::endl; 
    for(std::size_t i = 0; i != mat.size(); ++i) { 
     std::cout << i << " "; 
     for(auto e : mat[i]) { 
      std::cout << e << " "; 
     } 
     std::cout << std::endl; 
    } 
    std::cout << std::endl; 
} 

void print_adjacency_list(const std::vector<std::vector<int>>& mat) 
{ 
    std::cout << "Adjacency List" << std::endl; 
    std::cout << mat.size() << std::endl; 
    for (std::size_t i = 0; i != mat.size(); ++i) { 
     std::cout << i << " --> "; 
     for(auto e : mat[i]) { 
      if (e == 1) { 
       std::cout << e << " "; 
      } 
     } 
     std::cout << std::endl; 
    } 
} 

int main() 
{ 
    std::ifstream in; 
    std::string infile; 

    std::cout << "Please enter the input data file name(NO SPACES): "; 
    std::cin >> infile; 

    in.open(infile); 
    while(in.fail()) { 
     std::cout << "Please enter a CORRECT input data file name(NO SPACES): "; 
     std::cin >> infile; 
     in.open(infile); 
    } 

    int c; 
    in >> c; 
    std::vector<std::vector<int> > array(c, std::vector<int>(c)); 

    int u, v; 
    while(in >> u >> v) { 
     array[u][v] = 1; 
    } 
    in.close(); 

    print_adjacency_matrix(array); 
    print_adjacency_list(array); 

    return 0; 
}