-2

This is a code of standardized alorithm called as dijsktra algorithm.. But its giving me an error at calling dijsltra function as follows

error: cannot convert 'int (*)[(((sizetype)(((ssizetype)n) + -1)) + 1)]' to 'int (*)[9]' for argument '1' to 'void dijkstra(int (*)[9], int)

My code is

#include<iostream>
using namespace std;
#define inf 999;
const int V=0;
int minDistance(int dist[], bool sptSet[])
{

    int min=inf;
    int min_index;
    for (int v = 0; v < V; v++)
    {
        if ((sptSet[v] == false) &&(dist[v] <= min) )
            min = dist[v];
        min_index = v;
    }
    return min_index;
}
void dijkstra(int graph[V][V], int src)
{
    int dist[V];
    bool sptSet[V];

    for (int i = 0; i < V; i++)
    {
        dist[i] = inf;
        sptSet[i] = false;
    }
    dist[src] = 0;

    for (int count = 0; count < V-1; count++)
    {
        int u = minDistance(dist, sptSet);

        sptSet[u] = true;

        for (int v = 0; v < V; v++)

        {
            if (!sptSet[v] && graph[u][v] && dist[u] != 999 && (dist[u]+graph[u][v] < dist[v]))
            {
                dist[v] = dist[u] + graph[u][v];

            }
        }
    }
}

int  main()
{
    int size;
    int n;
    cin>>n;
    size=n;
    int matrix[n][n];
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
        {
            if((j+1==i+2)||(j+1==3*(i+1)))
                matrix[i][j]=1;
            else
                matrix[i][j]=0;
        }
    }

    dijkstra(matrix,0);
    cout<<matrix[0][n-1]<<endl; 
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
  • 1
    Your code is almost unreadable with the lack of formatting. Can you please fix your indentation? – Cory Kramer Sep 30 '16 at 11:30
  • i am sorry... new to stackoverflow .What should I format? – Harshit Jain Sep 30 '16 at 11:33
  • Probably indentation, to begin with. It is extremely hard to follow what belongs to what function etc. – Ilja Everilä Sep 30 '16 at 11:38
  • Please format according to basic Indentation rules. – Rishikesh Raje Sep 30 '16 at 11:38
  • 1
    Gotta love that error message. It might as well say `error: C++`. – Lundin Sep 30 '16 at 11:44
  • I also cannot understand how you're getting the error you pasted with the code you've presented. You have a constant int `V` with the value *0* in your code and the function `dijkstra`, declared as `void dijkstra(int graph[V][V], int src)`, which *should* mean that the 1st argument is **a (auto) pointer to 0-length array of int** (0-length array might've been undefined behaviour, but not sure), but in your error message it is a pointer to array of int of length 9. That implies that your example does not match what you're actually doing. – Ilja Everilä Sep 30 '16 at 11:45
  • 1
    The error is trying to tell you that you're trying to pass **a pointer to VLA of int (variable length array)** as 1st argument, but the function expects **a pointer to array of int of length 9**, hence the types don't match. – Ilja Everilä Sep 30 '16 at 11:46
  • @HarshitJain I edited your code to show you a example of good formatting. Please try to respect a good formatting for next time (not only for us, but also for you, it's easier to write code with correct indentation) – Garf365 Sep 30 '16 at 11:48
  • 1
    `void dijkstra(int matrix[0][0], int n)` is not valid C++ according to the (any) standard. What compiler are you using? – Martin Bonner supports Monica Sep 30 '16 at 12:05
  • 1
    ["A proper way to create a matrix in c++"](http://stackoverflow.com/questions/618511/a-proper-way-to-create-a-matrix-in-c) discusses some ways to handle matrices in C++ that rely on the std containers. – Ilja Everilä Sep 30 '16 at 12:11

1 Answers1

0

You have const int V = 0; in the beginning and you use this as parameter in dijkstra as int graph[V][V]. In your main function you call dijkstra with matrx[n][n] which is not V most likely.

I'm not an expert but I would try something like this:

#include <iostream>

void dijkstra(int** &matrix, int n)
{
    int count = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            matrix[i][j] = count++;
        }
    }
}

int main()
{
    int n;
    std::cin >> n;

    int** matrix = new int*[n];
    for (int i = 0; i<n; i++)
    {
        matrix[i] = new int[n];
    }

    dijkstra(matrix, n);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            std::cout << matrix[i][j] << std::endl;
        }
    }
}

Create the 2D array dynamically. int** is a pointer to a pointer and for each pointer (*) you create an integer array with new. Now you pass this pointer of pointer object as reference to your function (that's why there is the & in the parameters of the dijkstra function). Now you can work with your matrix as normal you can print the result in the main function at the end.

Philipp
  • 2,376
  • 5
  • 29
  • 47
  • 3
    Using a flat region as nD array with some indice math would (possibly) offer better locality and less indirection, when compared to using pointer to pointer to int and separately allocated regions. Using `int**` as 2D array is dangerously close to becoming a 3-star programmer. – Ilja Everilä Sep 30 '16 at 12:08
  • 2
    Ew! Please use std::vector rather than dynamic arrays. It makes memory management *so* much easier. (There are better solutions for matrices than vectors of vectors, but that's probably a bit complicated). `std::vector> matrix(n); for (auto& v: matrix) v.resize(n);` the argument is declared as `std::vector>& matrix`. – Martin Bonner supports Monica Sep 30 '16 at 12:09
  • I agree that using `int**` is not the nicest solution. But before going to use the STL I would prefer Ilja's suggestion with a 1D array of length n*n. But again, I'm far from being an expert so I gladly listen to others. – Philipp Sep 30 '16 at 12:14