-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmopaap.cpp
83 lines (79 loc) · 1.75 KB
/
vmopaap.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//Various methods of paasing array as parameter -
#include<iostream>
using namespace std;
void passingarrayaspointer(int *ar, int n)
{
for(int i=0;i<n;i++)
{
cout<<"\n#"<<i;
cout<<"\nEnter the element: ";
cin>>ar[i];
}
}
void passingarrayaspointertoanarray(int ar[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\n#"<<i;
cout<<"\nEnter the element: ";
cin>>ar[i];
}
}
int * creatingarrayonfunction(int n)
{
int * p;
//malloc in 'c' is new in 'c++'
p=new int[n];
for(int i=0;i<n;i++)
{
cout<<"\n#"<<i;
cout<<"\nEnter the element: ";
cin>>p[i];
}
return (p);
}
int main()
{
int n, ch;
cout<<"\nEnter the size of an array: ";
cin>>n;
int array[n],*a;
cout<<"\nEnter how you want the array to be created -\n1. by pointer\n2. by pointer to an array\n3. by creating array inside a function\n4. Exit\n:- ";
cin>>ch;
switch(ch)
{
case 1:{
passingarrayaspointer(array, n);
cout<<"\nThe array: ";
for(int i=0;i<n;i++)
{
cout<<" "<<array[i];
}
break;
}
case 2:{
passingarrayaspointertoanarray(array,n);
cout<<"\nThe array: ";
for(int i=0;i<n;i++)
{
cout<<" "<<array[i];
}
break;
}
case 3:{
a=creatingarrayonfunction(n);
cout<<"\nThe array: ";
for(int i=0;i<n;i++)
{
cout<<" "<<a[i];
}
break;
}
case 4: exit(0);
default: {
cout<<"\nInvalid input.";
exit(0);
}
}
return 0;
}