help me with this simple program

This is a discussion on help me with this simple program within the C Programming forums, part of the General Programming Boards category; so I am asked to create a program with the following features: the program is so that I can connect ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    help me with this simple program

    so I am asked to create a program with the following features:

    the program is so that I can connect and disconnect countries. So say that I want to connect two countries, I input 2 integers that represent the countries. Say that country 1 and country 2 and 3 are adjacent/connected to each other and country 3 and 4 are connected. When I print the result it gives me:

    Country 01 : 02 03
    Country 02 : 01
    Country 03 : 01 04
    Country 04 : 03

    My problem is that I need to store these connected countries in an array and the requirements are there can be a max of 100 countries?? So how can I do this without possibly having to declare 100 array variables for each country?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to be more specific (both to us and, I think, to yourself) about what you mean by "store these connected countries in an array". My Hat of Guessing says you want to store the adjacency matrix, which is a 2-D array.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    Well a 2d array of size [100][100] could work, however isn't this the same as declaring 100 single array? I would also want so that I can disconnect the countries which is adjacent/connected easily. So in the example above, say that I want to disconnect country 1 and 2. Then it will be:

    Country 01 : 03
    Country 03 : 01 04
    Country 04 : 03

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    Well a 2d array of size [100][100] could work, however isn't this the same as declaring 100 single array?
    Maybe? I suppose it depends on what you think "100 single array" means. I would declare it like so:
    Code:
    #define MAXSIZE 100
    int connections[MAXSIZE][MAXSIZE] = { {0} };
    were it I doing the work. Then if country 1 was connected to country 3, I would set both connections[1][3] and connections[3][1] to 1.

    Quote Originally Posted by -EquinoX- View Post
    I would also want so that I can disconnect the countries which is adjacent/connected easily. So in the example above, say that I want to disconnect country 1 and 2. Then it will be:

    Country 01 : 03
    Country 03 : 01 04
    Country 04 : 03
    You would unhook in very much the same way.

    Edit to add: I suppose I should mention that I'm assuming country numbering is the same as array numbering. If my countries are instead numbered 1 through 100, I would subtract 1 before using the country number as an array index.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    well that makes sense though, thanks. However, I have a concern, the instruction says that it has to be in an increasing order, so that that country 1 is connected to country 3 and 10

    The output should be:
    Country 01: 3 10

    not

    Country 01: 10 3

    and there can't be duplicates also, so that's why I have to check for each row in the 2d array if that country that is going to be connected already exists or not and if it's smaller/larger than the corresponding row and column value. That's why I can't just do a [1][3] or the other way around

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    well that makes sense though, thanks. However, I have a concern, the instruction says that it has to be in an increasing order, so that that country 1 is connected to country 3 and 10

    The output should be:
    Country 01: 3 10

    not

    Country 01: 10 3

    and there can't be duplicates also, so that's why I have to check for each row in the 2d array if that country that is going to be connected already exists or not and if it's smaller/larger than the corresponding row and column value. That's why I can't just do a [1][3] or the other way around
    None of what you just said makes any sense. If connections[1][3] is 0 (=false), there is no connection between countries 1 and 3; if connections[1][3] is 1 (=true), then there is. Order has nothing to do with it.

    AND, you'll want to set connections[3][1] also, so that when you run through country 3, you will find that country 1 is connected to it.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so what you're trying to say here is that you just set the array values to 0 or 1?? is that right??

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by -EquinoX- View Post
    so what you're trying to say here is that you just set the array values to 0 or 1?? is that right??
    Yes. You should look up "adjacency matrix", since that's what you're being asked to build, but we're just keeping track of where the edges are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 05:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21