Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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!
C++
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
Updated 20-Nov-12 21:09pm
v4

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[^]).
 
Share this answer
 
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.
Perhaps this snippet can help:

C++
    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();
 
Share this answer
 
v2
C++
# 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>
 
Share this answer
 
C++
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
 
Share this answer
 
v3
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:
C++
#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;
}
 
Share this answer
 
v2
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.
C++
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.
 
Share this answer
 
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).
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900