Click here to Skip to main content
Sign Up to vote bad
good
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:23
Edited 20 Nov '12 - 21:09


7 solutions

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:33
Thanks for the help.
CPallini - 13 Nov '12 - 16:48
You are welcome.
Albert Holguin - 14 Nov '12 - 17:32
For beginners... I'd recommend learning to use the heap (new/delete) first before moving on to containers. +5
CPallini - 15 Nov '12 - 3:23
I agree with you, beginners should learn to master new/delete. By the way, thank you.
Espen Harlinn - 15 Nov '12 - 10:59
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:
 
 
    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  
# 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  
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  
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  
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  
Comments
Maximilien - 15 Nov '12 - 10:49
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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 393
1 OriginalGriff 335
2 Arun Vasu 315
3 Maciej Los 238
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,680
1 OriginalGriff 7,539
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid