0

this is probably a very beginner-like question.

However, I have an algorithm that works with graphs, and what I currently have is just the pre-entered values. I want to make it so a user would be able to enter the edges of the graph and a multidimensional array would be filled up with the values.

Here are some bits of the code

#include <stdbool.h>
#include <stdio.h>
#include <iostream>
using namespace std;

#define vertex_count 4
int main()
{

   // int vertex_count;

    bool graph[vertex_count][vertex_count] =
    {
        { 0, 1, 1, 1 },
        { 1, 0, 1, 0 },
        { 1, 1, 0, 1 },
        { 1, 0, 1, 0 },
    };
    int used_colors = 3;
    graphColor(graph, used_colors);

    return 0;
}

I am assuming that I would have to ask the user to enter how many vertices and edges there are and when the user enters the edges, I would be putting them in the array one by one.

However, I run into a problem that when the vertex count is not defined but is entered, functions say that it is not declared, and so on.

Does anyone have an idea on the best way to do this?

Thank you in advance!

Quadro
  • 23
  • 1
  • 6
  • 2
    Unrelated: Only include `#include ` in C programs. `bool` is a distinct type in C++ so `bool` is always available. Also, include `` instead of `stdio.h`. – Ted Lyngmo May 18 '21 at 23:57
  • 1
    If the size of the graph isn't know at compile time, you could use `std::vector graph(vertex_count, std::vector(vertex_count));` instead. – Ted Lyngmo May 19 '21 at 00:01
  • Leverage what Ted mentioned above. As for the user, take a look at https://stackoverflow.com/a/10464355/11229351 – Patrick Le May 19 '21 at 00:20
  • Does this answer your question? [Parsing Command Line Arguments in C++?](https://stackoverflow.com/questions/865668/parsing-command-line-arguments-in-c) – Fantastic Mr Fox May 19 '21 at 00:44
  • Note that `vector` is a bit of a weird-o. Enough to get its own [documentation page to cover the differences between it and a regular `vector`](https://en.cppreference.com/w/cpp/container/vector_bool) – user4581301 May 19 '21 at 00:45
  • Your title says "the values which would be placed in multidimensional [array]", but your question seems to be about values that would be **the sizes of** your multi-dimensional array. Is that accurate? (Since you did not show the code that produces the error -- nor the exact error message -- it's a bit unclear what exactly is the focus of this question.) – JaMiT May 19 '21 at 03:03

3 Answers3

2

A technique you could use to collect a variable amount of data would be to first ask the user how many vertices they require, and then std::cin in a for loop to collect the actual data.

std::vector<bool> graph;
int vertex_count = 0;
std::cout << "How many vertices?\n";
std::cin >> vertex_count;
std::cout << "Enter the graph:\n";
int answer = 0;
for (int i = 0; i < vertex_count * vertex_count; ++i) {
    std::cin >> answer;
    graph.push_back(answer);
}

I recommend a std::vector<bool> to hold the values because it is a variable sized array. To access values in this one dimensional array as though it were two dimensional, use the expression graph[y*vertex_count+x].

When running the program, the data can be entered like:

How many vertices?
4
Enter the graph:
1 0 1 0
1 1 0 1
0 0 0 0
1 1 0 0

because std::cin deliminates on all whitespace, not just \n.

Camwin
  • 413
  • 3
  • 10
0

I am assuming that I would have to ask the user to enter how many vertices and edges there are

I think this is a bad idea. Asking the user to count edges and vertices is very tedious. Assuming that the user will perform this task perfectly is just going to lead to frustration when your program crashes because the counts are wrong. Once the nodes and links counts get into the thousands, which they do in real world problems, the counting task becomes just about impossible.

It is better to let the input reading code do the counting for itself.

Here is the algorithm I suggest ( and always use for this myself )

LOOP over input file specifying edges one at a time until file ends
   check if first vertex is already present in graph.  If not then add new vertex.
   repeat for second vertex
   add edge between vertices

You can check out the C++ code that implements this at https://github.com/JamesBremner/PathFinder

Here is an example

    std::string line;
    while (std::getline(inf, line))
    {
        std::cout << line << "\n";
        auto token = ParseSpaceDelimited(line);
        if (!token.size())
            continue;
        switch (token[0][0])
        {

        case 'l':
            if (token.size() != 4)
                throw std::runtime_error("cPathFinder::read bad link line");
            addLink(
                findoradd(token[1]),
                findoradd(token[2]),
                atof(token[3].c_str()));
            break;

        case 's':
            if (token.size() != 2)
                throw std::runtime_error("cPathFinder::read bad start line");
            myStart = find(token[1]);
            break;

        case 'e':
            if (token.size() != 2)
                throw std::runtime_error("cPathFinder::read bad end line");
            myEnd = find(token[1]);
            break;
        }
    }
}

Documentation of the file format this code is reading is at https://github.com/JamesBremner/PathFinder/wiki/Costs

ravenspoint
  • 19,093
  • 6
  • 57
  • 103
-2

So like the others have said you probably want to use a Vector and while I don't want to admit I'm new, (I'm still kinda new), so IDk how to use vectors yet, I think that's next semester. still I can answer your question as we like literally just learned this.

You want to create a pointer to the start of your array and then make your array with new.

    using namespace std;
    int main(){
        //making pointer
        int **Dynamic2D;
        int x, y;
        cout << "Enter x, then y, separated by a space"<<endl;
        cin>>x>>y;
        cout<<endl;

        Dynamic2D = new int* [x];
        for(int i=0;i<x;i++){
            //making pointer to each y
            Dynamic2D[i]= new int [y];
            }
         //filling array
         for (int i=0; i<x;i++){
            for (int j=0; j<y; j++){
                cout <<"Enter position"<< i << j <<": "<<endl;
                cin>>Dynamic2D[i][j];
                }
            }
        //printing array
        for (int i=0; i<x; i++){
            for (int j=0; j<y; j++){
                cout << Dynamic2D[i][j];
                }
           }
     }

So there might be a typo in there i gtg but that should be a simple dynamic 2d int array.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • i did now, Im busy... it runs fine anyway – EuclideanDreams May 19 '21 at 02:09
  • Did you learn about cleaning up after allocating memory? While it is recommended to avoid using `new` in most code, if you do use `new`, there should be a corresponding `delete`. – JaMiT May 21 '21 at 00:57