-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA31pointerstostructure.cpp
95 lines (72 loc) · 1.97 KB
/
A31pointerstostructure.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
85
86
87
88
89
90
91
92
93
94
95
// Pointers to Structure
// By Emily Dayanghirang, Alina Corpora, Sarra Osman
#include <iostream>
#include <iomanip>
using namespace std;
struct NumList {
float *list; // list of values
int len; // count of values in list
float min, // the minimal value in list
max, // the maximal value in list
avg; // the mean of the numbers
};
int computeStats(NumList *);
int main()
{
NumList *num = new NumList;
int size, error;
float number;
cout << "\nHow many numbers are you inputting: ";
cin >> size;
num->list = new float[size];
for(int i = 0; i < size; i++)
{
cout << "Enter #" << i+1 << ": ";
cin >> number;
*(num->list+i) = number;
}
num->len = size;
cout << "\nThe length of the list is " << num->len << ".\n";
error = computeStats(num);
cout<<"\nThe error code returned was "<<error<<"."<<endl;
delete [] num->list;
num->list = nullptr;
delete num;
return 0;
}
int computeStats(NumList *num)
{
if(num == nullptr)
return -1;
if(num->list == nullptr)
return -2;
if(num->len<1)
return -3;
else
{
float min = *num->list;
float max = *num->list;
float total = 0;
for(int i = 0; i < num->len; i++)
{
if(min > *(num->list+i))
min = *(num->list+i);
}
cout << "\nThe minimum number is " << min << ".\n";
for(int i = 0; i < num->len; i++)
{
if(max < *(num->list+i))
max = *(num->list+i);
}
cout << "\nThe maximum number is " << max << ".\n";
for(int i = 0; i < num->len; i++)
{
total += *(num->list+i);
}
cout << "\nThe total of the number is " << total << ".\n";
num->avg = total / num->len;
cout << setprecision(2) << fixed
<< "\nThe average of the list is " << num->avg << ".\n";
return 0;
}
}