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;
}

a = new int [i][j];

What's the value of i? What's the value of j?

Member Avatar for iamthwee

Don't forget to clear up the memory with new you must use delete.

Better still use a vector instead.

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.

i want an nxn array.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.