-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-1_array_in_fun.cpp
84 lines (74 loc) · 1.38 KB
/
11-1_array_in_fun.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
84
// passing array in function
// method 1 -> if size is known
/*
#include <iostream>
using namespace std;
void array(int a[]) // or int a[5]
{
for (int i = 0; i < 5; i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int arr[5] = {10,20,30,40,50};
array(arr); // name of array acts as pointer, if we pass a[2] -> it means add of a[2] will pass in function
return 0;
}
*/
//method 2 -> taking size from user
#include <iostream>
using namespace std;
void array(int a[], int size) // or int a[5]
{
for (int i = 0; i < size; i++)
{
cin>>a[i];
}
for (int i = 0; i < size; i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int n ;
cout<<"enter size of array \n";
cin>>n;
int arr[n] ;
cout<<"enter elements of array \n";
array(arr , n); // name of array acts as pointer and size of array
return 0;
}
// passing array as function in class
/*
#include<iostream>
using namespace std ;
class sum
{
int arr[6];
public :
void getarray()
{
for (int i = 0; i < 6; i++)
{
cin>>arr[i];
}
}
void printarray()
{
for (int i = 0; i < 6; i++)
{
cout<<arr[i]<<" ";
}
}
};
int main ()
{
sum s1;
s1.getarray();
s1.printarray();
return 0 ;
}
*/