Using vectors for map coding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mersinli
    New Member
    • Apr 2007
    • 3

    #1

    Using vectors for map coding

    Hello;
    I will use standard library vectors and implement simple map coloring algorithm. The goal of a map coloring problem is to color a map so that regions sharing a common border have different colors

    In C++ I can represent the neighboring information using a 2D vector of booleans. For example, consider the following 2D neighbor vector:
    Countries 1 2 3 4
    1 - T F T
    2 T - F F
    3 F F - T
    4 T F T -
    The countries 1 and 2 are neighbors. The countries 1 and 3 are not neighbors. Note that the 2D vector is symmetric

    I write a C++ class with a constructor that takes the number of countries and a vector of available colors represented by strings (“red”, “blue”, “green”, etc.). The constructor builds a private random neighboring 2D vector.
    I override the constructor with a function that takes a neighbor vector and a vector of available colors
    I write a member function that accepts a vector of available colors

    I write a member function that accepts a 2D neighboring vector

    I write a member function named mapColor that assigns colors to countries. If the coloring is successful, it prints the country colors. Otherwise, it gives an error message. For example, for the above neighbor vector the output would be
    Country 1 (red),
    Country 2 (blue),
    Country 3 (blue)
    Country 4 (red).

    I write some code but it is not run:S

    MY CODE :
    [code=cpp]
    #include<iostre am>// allows program to output data to the screen
    using std::cout;// program uses cout
    using std::cin;// program uses cin
    using std::endl;//program uses endl

    // contains prototypes for functions srand and rand
    #include<cstdli b>

    //contains prototypes for function time
    #include<ctime>

    #include "dosya_1.h"// include definition of class Country


    // constructor takes the number of countries and a vector of available colors represented by strings
    Country::Countr y(vector<char> &v_c_array,i nt number)
    {
    vector<vector<b ool> > v_2x_array;

    set_function_nu mber_country(nu mber);

    accept_vector_c olor(v_c_array) ;

    accept_2D_neigh boring_vector(v _2x_array);




    }//end Country constructor



    Country::Countr y(vector<char> &a,vector<vecto r<char> &b)
    {
    accept_vector_c olor();

    b(country_numbe r,v_color_array );

    for (int i=0; i<b.size(); ++i)
    for (int j=0; j<b[i].size(); ++j)
    b[i][j] = v_2d_neighbor[i][j];

    }


    void Country::accept _vector_color(v ector<char> &v_c_array)
    {

    for(int i=0; i<country_numbe r; ++i)
    v_color_array[i] = v_c_array[i];

    }

    void Country::accept _2D_neighboring _vector(vector< vector<bool> > &v_2D_neighb or)
    {
    int x;

    srand(time(0));

    for (int i=0; i<b.size(); ++i)

    for (int j=0; j<b[i].size(); ++j)
    v_2D_neighbor[i][j] = v_2d_neighbor[i][j];


    for (int i=0; i<v_2D_neighbor .size(); ++i)

    for (int j=0; j<v_2D_neighbo r[i].size(); ++j)
    {
    x = rand()%10 + 1;

    if(i == j)
    continue;

    else if(x = 1)
    v_2D_neighbor[i][j] = true;

    else
    v_2D_neighbor[i][j] = false;
    }

    }


    void Country::set_fu nction_number_c ountry(int f_number)
    {
    country_number = f_number;

    }


    void mapColor()
    {
    char country[country_number];

    for (int i=0; i<v_2d_neighbor .size(); ++i)
    {
    for (int j=0; j<v_2d_neighbo r[i].size(); ++j)
    {
    if(i == j)
    continue;

    else if(v_2d_neighbo r[i][j])
    {
    country[i] = v_color_array[0];
    country[j] = v_color_array[1];
    }

    else
    {
    country[i] = v_color_array[0];
    country[j] = v_color_array[0];
    }

    }
    }


    for(int k=0; k<country_numbe r; ++k)
    cout << "country(" << k << ") " << country[k] << "\n";


    }


    and MY HEADER :

    #include <vector>

    using std::vector;

    class Country{

    public:
    Country(vector< char>&,int);

    Country(vector< char>&,vector<v ector<bool> >&);

    void set_function_nu mber_country(in t);

    void accept_vector_c olor(vector<cha r> &);

    void accept_2D_neigh boring_vector() ;

    void mapColor();

    int find_minimum_nu mber_of_colors( );


    private:

    int country_number;
    vector<char> v_color_array(4 );
    vector<vector<b ool> > v_2d_neighbor() ;


    };
    [/code]

    Please help mee

    REGARDS..
    Last edited by sicarie; Nov 5 '07, 01:38 PM. Reason: Code tags are [code=cpp] code goes here [/code]. Please use them
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    What's the error you get when you try to run it? Can you copy and paste them?

    Comment

    • mersinli
      New Member
      • Apr 2007
      • 3

      #3
      Originally posted by sicarie
      What's the error you get when you try to run it? Can you copy and paste them?
      So I don't write main function that test all of the functions but many many error occur

      --------------------Configuration: ctry - Win32 Debug--------------------
      Compiling...
      ctry.cpp
      c:\program files\microsoft visual studio\myprojec ts\contry\dosya _1.h(27) : error C2059: syntax error : 'constant'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(24) : error C2660: 'accept_2D_neig hboring_vector' : function does not take 1 parameters
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(33) : error C2146: syntax error : missing ',' before identifier 'b'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(33) : error C2065: 'b' : undeclared identifier
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(33) : error C2143: syntax error : missing ',' before ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(34) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(39) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(39) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(40) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(40) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(43) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(47) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(49) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(49) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(52) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(55) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(60) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(60) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(62) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(62) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(66) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(66) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(68) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(68) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(69) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(80) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(82) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(86) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(89) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(93) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(96) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(96) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(97) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(98) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(98) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(99) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(104) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(107) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(110) : error C2143: syntax error : missing ';' before '{'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(113) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(115) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(116) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(119) : error C2143: syntax error : missing ')' before ';'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(119) : error C2059: syntax error : ')'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(123) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(126) : error C2143: syntax error : missing ';' before '}'
      C:\Program Files\Microsoft Visual Studio\MyProjec ts\contry\ctry. cpp(126) : fatal error C1001: INTERNAL COMPILER ERROR
      (compiler file 'YÃ', line 1)
      Please choose the Technical Support command on the Visual C++
      Help menu, or open the Technical Support help file for more information
      Error executing cl.exe.

      ctry.exe - 47 error(s), 0 warning(s)

      you can write a main function that test all of the functions and modify or change my code for that is run.
      thank you very very much:):)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You have to fix these syntax errors first.

        Start with line 27 in your dosya_1.h and see whet the problem is. By the time the compiler saw the word const, it knew there was a problem. The error will be form there or earlier in the file.

        Comment

        Working...