Click here to Skip to main content
12,405,543 members (59,272 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++
Hi

I want to create an array in the program. I'm getting the array and the size of the user. Then I add it to the array.
How can I do it?

I actually want to do this operation!
int i,j;
cin>>i>>j;
int a[i][j];
If want this example to work correctly, what should I do?
if array 2 element ?
Posted 13-Nov-12 2:23am
Updated 20-Nov-12 21:09pm
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You may use the new operator, the documentation[^] provides sample code.

You could also use a STL container, like the std::vector (see, for instance here[^]).
  Permalink  
Comments
sajad abbasi 13-Nov-12 16:33pm
   
Thanks for the help.
CPallini 13-Nov-12 16:48pm
   
You are welcome.
Albert Holguin 14-Nov-12 17:32pm
   
For beginners... I'd recommend learning to use the heap (new/delete) first before moving on to containers. +5
CPallini 15-Nov-12 3:23am
   
I agree with you, beginners should learn to master new/delete.
By the way, thank you.
Espen Harlinn 15-Nov-12 10:59am
   
a 5, std::vector became immensely more useful after data() was added to the interface, removing the need for a separate dynamic_array container - which was discussed at some point.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Perhaps this snippet can help:

    vector<int> x;
 
    int size;
    cin >> size;
 
    while (size--)
    {
        int temp; cin >> temp; x.push_back(temp);
    }
/*
    Better is, but the compiler dislikes it:
    while (size--)
        cin >> x.push_back();
    or
    while (size--)
        x.push_back(cin>>);
*/ 
 
    // processing...

    for (vector<int>::iterator it = x.begin(); it != x.end(); it++)
        cout << *it << endl;
 
    x.clear();
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
 
int main()
{
	int size;
	scanf("%d",&size);
	int* p = (int *)malloc(sizeof(int)*size);
	if(NULL == p)
		exit(-1);
	for(int i = 0;i < size;++ i)
		scanf("%d",&p[i]);
	for(int i = 0;i < size;++ i)
		printf("%3d",p[i]);
	return 0;
}</stdlib.h></malloc.h></stdio.h>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

int main()
{
  int size;
  int *ptr_arr;          //pointer delaration
  cin>>size;             //input size from user
  ptr=new int[size];     //array declared in heap or dynamic memory
  for(int i=0;i<size;i++)>
     cin>>ptr_arr[i];        //takes input in dynamic array
  for(int i=0;i<size;i++)>
     cout<<ptr_arr[i];
 
return 0;
}
I hope it helps
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

The only way to make a two dimension without the use of the advanced C++ features is the use an array of pointers, note the two for loops, one is for initialization, and the second is for cleanup purposes.

Example:
#include <iostream>
using namespace std;
 
int main()
{
	int **a;
	int i = 0, j = 0;
	int k;
 
	cout << "input dimension 1:";
	cin >> i;
	cout << "input dimension 2:";
	cin >> j;
 
	a = new int *[i];
	for (k = 0; k < i; k++)
	{
		a[k] = new int[j];
	}
	
	cout << "a[0][0]: ";
	cin >> a[0][0];
	
	a[0][0] = 1024;
	cout << "value: " << a[0][0] << endl;
 
	for (k = 0; k < i; k++)
	{
		delete a[k];
	}
	delete  a;
 
	return 0;
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Array declaration in C/C++ will not be success if you don't provide a constant size. For the beginners who do not familiar with pointers the following may give a good understanding.

1. Declare an array with a maximum size; when your input exceeds this limit you can always show an error message.
int x[100];

2. when you deal with an array size within the maximum size, keep the dynamic size in a variable and use the variable as the size of your array instead of the maximum size.
int main(){
   int x[100];  //maximum size is 100
   int size;
   cin>>size;   //to accept the size
   if (size>=100){
      cout<<"Array size exceeds the maximum allocation";
      return 0;
   }
   //when inputting numbers
   for(int i=0; i<size; i++){
      cin>>x[i];
   }
   //when displaying the result
   for(int i=0; i<size; i++){
      cout<<x[i]<<endl;
   }
   return 0;
}

Hope this will help.
  Permalink  
v5
Comments
Maximilien 15-Nov-12 10:49am
   
IMO, You answer will not help at all.

How is declaring a static array answering the original question? There are multiple ways, even in plain old C to allocate dynamic arrays(malloc and realloc) , and in C++ to have better and more foolproof ways of doing it (new xor std::vector).
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

have a good time dear mr sajjad
you're from iran?me too.

look you have to declare a dynamic array like above:

int n=0,*a=0;
cout<<"enter the length of array :\n";
cin>>n;
a=new int[n];

after that you can control the array with an index , like above :
int j=0;
j=a[7];

good lock and god with you
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100