please help me i need a dynamic array that get the information as a matris and give me the vertex degrees (sum every row).and send it to another matrix.

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int k,sum;
    cout << "Enter the Graph vertex number: ";
    cin >> k;
    int i, j, *a, b[100];
    a = new int [i][j];
    cout << "your Gragh is" << k << " , " << k << ", enter your numbers:"<< endl;
    for (i = 0; i<k; i++)
    {
        for (j = 0; j<k; j++)
        {
            cout << "[ " << i + 1 << " , " << j + 1 << " ] = ";
            cin >> a[i*j];
        }
        cout << endl;
    }
    for (i = 0; i<k; i++)
    {
        for (j = 0; j<k; j++)
        {
        sum += a[j];
        }
        b[i] = sum;
        cout << b[i];
        sum = 0;
    }
    return 0;
}

i and j does not have the value of theri own. they are just counters. please help!

i and j does not have the value of theri own. they are just counters.

I believe that this was Moschops' point. You are using them as the sizes in the new expression before they are initialized, with the result that the sizes of the rows and columns are effectively random and most likely zero.

Now, where did you intend to get the array sizes from?

from the part i++ or j++ till it reaches the i,j<k. it is dynamic, so it goes till it stops I think.
please correct it, and send it as a reply.

This:

a = new int [i][j];

creates a 2D array of size ixj.

Tell me what size that array is. Is it a 10x10 array? Is it a 1x5 array? You're creating an array and you don't know how big it is.

int i = k, j = k, *a, b[100];

Is what I believe you want, since you are asking user for it's size.

Edited 2 Years Ago by Suzie999

I believe what you actually want is to create an isolateral matrix k rows by k columns, where k is a value entered by the user, correct? Tha would be

    int k,sum;
    cout << "Enter the Graph vertex number: ";
    cin >> k;
    int i, j, *a, b[100];
    a = new *int[k];    // create the rows

    // create the columns by row
    for (int i = 0; i < k; i++) 
    {
        a[i] = new int[k];
    }

Note that I used an array of int pointers, which I then populated with int arrays. This is because, unlike with a locally defined multi-dimensional array, the compiler has no way of determining at compile time the number of the rows, which it would need in order to compute the offsets for the columns. By using an array of arrays instead of a multi-dimensional array, you avoid the need to do a lot of extra pointer artihmetic.

This article has been dead for over six months. Start a new discussion instead.