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;
}
Re: Help,C++: What is wrong with my dynamic arrays 80 80

a = new int [i][j];

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

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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

Better still use a vector instead.

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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?

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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.

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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.

Re: Help,C++: What is wrong with my dynamic arrays 80 80
int i = k, j = k, *a, b[100];

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

Re: Help,C++: What is wrong with my dynamic arrays 80 80

i want an nxn array.

Re: Help,C++: What is wrong with my dynamic arrays 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.