-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptr_func_eg_1.cpp
52 lines (47 loc) · 1.01 KB
/
ptr_func_eg_1.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
#include <iostream>
using namespace std;
int input(int *, int);
void display(int *, int );
int sum(int *, int );
int main()
{
int n;
cout << "Enter the number of rows/columns of the array: ";
cin >> n;
int a[n];
int size = sizeof(a) / sizeof(a[0]);
cout << "Enter the elements of the array: " << endl;
input(a, size);
cout << "Displaying the matrix: " << endl;
display(a, size);
cout << "The sum of the elements of the array is: " << sum(a, size) << endl;
return 0;
}
int input(int *p, int size)
{
for (int i = 0; i < size; i++)
{
cout << "a[" << i << "]: ";
cin >> *(p + i);
}
return p[size];
}
void display(int *p, int size)
{
cout << "[";
for (int i = 0; i < size; i++)
{
cout << *(p + i) << " ";
}
cout << "]";
cout << endl;
}
int sum(int *p, int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += *(p + i);
}
return sum;
}