array

hi, i need creat two array and join them into one (third array) To creat a third array have erore. Help me please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>
#include<iomanip>
using namespace std;
int main()
{
   int size,size1,size2;
   cout<<"enter array"<<endl;
   cin>>size;
   int*parr1=new int[size];
   for(int i=0;i<size;i++)
   {
	   parr1[i]=rand()%50;
	   cout<<parr1[i]<<" "<<endl;
   }
   cout<<"enter array 1"<<endl;
   cin>>size1;
   int*parr2=new int[size1];
   for(int i=0;i<size1;i++)
   {
	   parr2[i]=rand()%70;
	   cout<<parr2[i]<<" "<<endl;
   }
   int*parr3=new int[size2];
     for(int i=0;i<size;i++)
	 
		 parr3[i]=parr1[i];
	 
	 for(int i=0;i<size2;i++)
	 
		 parr3[size1+i]=parr2[i];
	 cout<<"new array "<<" "<<parr3[i];
	 
	 

	return 0;
}
You say that there is an error. What error? The compiler must say something. Be exact.


How many elements are in the third array? How many it should have?

The answer to the first question is on line 23. size2 elements.

What is the value of size2? Line 6 tells that we have no idea. Uninitialized variable.


How many elements are in the second array? size1
How many elements of the second array do you access on the lines 28-30 loop? size2
What is the largest element of parr3 that you write to on line 30? size1+size2-1


PS. One must deallocate appropriately every dynamically allocated block of memory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
   int size1,size2,size3;
   cout<<"enter size of array 1: ";
   cin >> size1;
   int *parr1=new int[size1];
   for(int i=0;i<size1;i++)
   {
	   parr1[i]=rand()%50;
	   cout<<parr1[i]<<" ";
   }
   cout<<"\nEnter size of array 2: ";
   cin >> size2;
   int *parr2 = new int[size2];
   for(int i=0;i<size2;i++)
   {
	   parr2[i]=rand()%70;
	   cout<<parr2[i]<<" ";
   }
   size3 = size1 + size2;
   int*parr3=new int[size3];
   for(int i=0;i<size1; i++)
	 	 parr3[i]=parr1[i];
	 
   cout << "\n\nMerged arrays: ";
	 for(int i = 0; i < size3;i++)
	 {
		 parr3[size1 + i] = parr2[i];
	   cout<< parr3[i] << " ";
   }
	 cout << "\n\n";
	 
  delete [] parr1;
  delete [] parr2;
  delete [] parr3;

  system("pause");
	return 0;
}


Output ---
1
2
3
4
5
6
7
8
enter size of array 1: 4
41 17 34 0
Enter size of array 2: 4
59 44 68 28

Merged arrays: 41 17 34 0 59 44 68 28

Press any key to continue . . .
Topic archived. No new replies allowed.