Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,118 people online right now. Registration is fast and FREE... Join Now!




Declaring size of array in runtime

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

Declaring size of array in runtime

jabez_jb
4 Sep, 2006 - 09:18 PM
Post #1

New D.I.C Head
*

Joined: 18 Aug, 2006
Posts: 10


My Contributions
how can we decalre a size of array in run time?? i'm working on 2D arrays, i want to give the size of my array in runtime.

cin>>size>>size1;

function(size,size1);

function(int ,int)
{
int a[size][size1];
}

its the rough idea how i want to us that array.....plzz help me.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Declaring Size Of Array In Runtime
4 Sep, 2006 - 11:15 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
That's not possible in C++ or C89. It's only valid in C99.

Why don't you use a vector in C++?

This post has been edited by Xing: 4 Sep, 2006 - 11:15 PM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Declaring Size Of Array In Runtime
4 Sep, 2006 - 11:26 PM
Post #3

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE

cin>>size>>size1;

function(size,size1);

function(int ,int)
{
int a[size][size1];
}

I'll tell u what i know. Hopefully someone's got a simpler way. I'll give u an example. Suppose ure handling matrices. U ask the user for the order and then ask him to enter each element rowwise. Im gonna use pointers.
CODE

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    int row, col; //Stores the order of the matrix
    cout<<"Enter Order Of The Matrix"<<endl<<"Rows?";
    cin>>row;
    cout<<endl<<"Columns?";
    cin>>col;

    /*Create a pointer that stores the
    address of an object which stores the
    elements of the matrix*/
    int *matrix_pointer;
    matrix_pointer = new int[row*col];

    int i,j,element_no;

    /*Stores the elements of the matrix
    rowwise*/
    for(i=0;i<row;i++)
    {
        cout<<"Enter row" <<i+1<<"(Hit Return after each element)"<<endl;
        for(j=0;j<col;j++)
        {
            element_no=i*col+j;
            cin>>matrix_pointer[element_no];
        }
    }

    /*prints the elements columnwise*/
    for(i=0;i<row;i++)
    {
        cout<<endl;
        for(j=0;j<col;j++)
        {
            element_no=i*col+j;
            cout<<setw(3)<<matrix_pointer[element_no];
        }
    }
        delete [] matrix_pointer;
    return 0;
}


In case u didnt know. Multidimensional arrays can have rows and columns. But in memory theyre stored linearly. so with pointers u cannot simply say:
CODE

int *pointer [3][3];

thats why u have:
CODE

matrix_pointer = new int[row*col]

'new' is used to dynamically allocate memory. Its somewhat similar to 'malloc' in C
Also the 'delete' keyword used in the end deallocates memory like 'free' in C.
and while storing/accessing this is what is done:
CODE

element_no=i*col+j
cin>>matrix_pointer[element_no]

where i is ure current row and j is ure current column ( remember ure starting from 0)

Hope u understand. smile.gif

Check out the C++ tutorials section for more about pointers and dynamic memory allocation in C++.

This post has been edited by Louisda16th: 5 Sep, 2006 - 12:53 AM
User is offlineProfile CardPM
+Quote Post

Xing
RE: Declaring Size Of Array In Runtime
4 Sep, 2006 - 11:31 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(Louisda16th @ 5 Sep, 2006 - 12:26 AM) *

#include<iostream.h>
#include<iomanip.h>

Please use standard headers(<iostream> and <iomanip>) from next time.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Declaring Size Of Array In Runtime
4 Sep, 2006 - 11:37 PM
Post #5

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
QUOTE(Xing @ 5 Sep, 2006 - 01:01 PM) *

Please use standard headers(<iostream> and <iomanip>) from next time.

Didnt quite get u. Is there a difference in saying <iostream> and <iostream.h>?
User is offlineProfile CardPM
+Quote Post

Xing
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 12:09 AM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(Louisda16th @ 5 Sep, 2006 - 12:37 AM) *
Didnt quite get u. Is there a difference in saying <iostream> and <iostream.h>?


<iostream.h> was never a part of standard C++ Read More
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 12:27 AM
Post #7

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
If i say iostream instead on iostream.h, my compiler gives an error. Check it out:
CODE

#include<iostream>
void main()
{
    cout<<"Hello";
}


errors:
'cout' : undeclared identifier
'<<' : illegal, right operand has type 'char [6]'
Im using visual C++ 6.0

This post has been edited by Louisda16th: 5 Sep, 2006 - 12:35 AM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 12:50 AM
Post #8

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
Ok I fixed the problem. (using namespace std;). Before i never knew why my book always used std::cout. Whenever i executed the program it used to give an error (since i used iostream.h and not iostream) Thanx Xing biggrin.gif . Ive edited the code accordingly. smile.gif

This post has been edited by Louisda16th: 5 Sep, 2006 - 01:09 AM
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 01:25 AM
Post #9

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
That's not possible in C++

God no way...C/C++ would never get so much praise if it weren't for malloc(),calloc(),free and new and delete.
User is offlineProfile CardPM
+Quote Post

Xing
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 02:18 AM
Post #10

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
QUOTE(born2c0de @ 5 Sep, 2006 - 02:25 AM) *

God no way...C/C++ would never get so much praise if it weren't for malloc(),calloc(),free and new and delete.

Did you read OP question? He wanted something like VLA's which are part of C99. There are no VLA's in standard C++.
Dynamic memory allocation which you are talking about is something different.

This post has been edited by Xing: 5 Sep, 2006 - 02:31 AM
User is offlineProfile CardPM
+Quote Post

violent_crimson
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 05:04 PM
Post #11

New D.I.C Head
*

Joined: 31 Aug, 2006
Posts: 36


My Contributions
int *a;
int size;
cin >> size;
a = new int [size]; ... i forget.. im sure that's how it works...

when finished
delete [] a;
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Declaring Size Of Array In Runtime
5 Sep, 2006 - 05:15 PM
Post #12

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
That is dynamic memory allocation, as noted above.
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 12/5/08 02:44AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month