-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq64.cpp
41 lines (35 loc) · 920 Bytes
/
q64.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
/*
* WAP to calculate the average of n numbers of any type using the concept of
* templates.
*/
#include <iostream>
using namespace std;
template <typename T>
double average(T *arr, int length) {
double avg = 0;
for (int i = 0; i < length; i++) {
avg += arr[i];
}
avg /= length;
return avg;
}
int main() {
int n;
int * int_arr;
double * double_arr;
cout << "How many elements in array?: ";
cin >> n;
int_arr = new int[n];
double_arr = new double[n];
cout << "Enter integers for int array: ";
for (int i = 0; i < n; i++) {
cin >> int_arr[i];
}
cout << "\nEnter double numbers for double array: ";
for (int i = 0; i < n; i++) {
cin >> double_arr[i];
}
cout << "Average of integer array: " << average(int_arr, n) << '\n';
cout << "Average of double array: " << average(double_arr, n) << '\n';
return 0;
}