-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 7.3 (Completed)
214 lines (165 loc) · 5.89 KB
/
Exercise 7.3 (Completed)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
1. (Practice) The following declarations were used to create the grades array:
const int NUMGRADES = 500;
double grades[NUMGRADES];
Write two different function headers for a function named sortArray() that accepts the
grades array as a parameter named inArray and returns no value.
void sortArray(double inArray [500])
void sortArray(double inArray [])
2. (Practice) The following declarations were used to create the keys array:
const int NUMKEYS = 256;
char keys[NUMKEYS];
Write two different function headers for a function named findKey() that accepts the keys
array as a parameter named select and returns a character.
char findKey(char select [256])
char findKey(char select [])
3. (Practice) The following declarations were used to create the rates array:
const int NUMRATES = 256;
double rates[NUMRATES];
Write two different function headers for a function named prime() that accepts the rates
array as an argument named rates and returns a double-precision number.
double prime (double [256])
double prime (double [])
4. (Modify) a. Modify the findMax() function in Program 7.4 to locate the minimum value of
the passed array.
int findMax(int vals[MAXELS])
{
int i, max = vals[0];
for (i = 1; i < MAXELS; i++)
if (max > vals[i])
max = vals[i];
return max;
}
b. Include the function written in Exercise 4a in a complete program and run the program.
#include <iostream>
using namespace std;
const int MAXELS = 5;
int findMax(int[MAXELS]); // function prototype
int main()
{
int nums[MAXELS] = { 2, 18, 1, 27, 16 };
cout << "The minimum value is " << findMax(nums) << endl;
system("PAUSE");
return 0;
}
// find the maximum value
int findMax(int vals[MAXELS])
{
int i, max = vals[0];
for (i = 1; i < MAXELS; i++)
if (max > vals[i])
max = vals[i];
return max;
}
5. (Program) Write, compile, and run a C++ program that has a declaration in main() to store
the following numbers in an array named rates: 6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8, and 10.0.
There should be a function call to show() that accepts the rates array as a parameter named
rates and then displays the numbers in the array.
#include <iostream>
using namespace std;
void show(double * rates, double size){
for (int i = 0; i < size; i++){
cout << *(rates + i) << " ";
}
return;
}
int main(){
double rates[] = { 6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8, 10.0 };
show(rates, 9);
system("PAUSE");
return 0;
}
6. (Program) a. Write, compile, and run a C++ program that has a declaration in main() to store
the string “Vacation is near” in an array named message. There should be a function call to
display() that accepts message in a parameter named strng and then displays the message.
#include <iostream>
using namespace std;
void display(char *strng, double size){
for (int i = 0; i < size; i++){
cout << *(strng + i) << " ";
}
return;
}
int main(){
char message[] = "Vacation is near";
display(message, 16);
system("PAUSE");
return 0;
}
b. Modify the display() function written in Exercise 6a to display the first eight elements
of the message array.
#include <iostream>
using namespace std;
void display(char *strng, double size){
for (int i = 0; i < size; i++){
cout << *(strng + i) << " ";
}
return;
}
int main(){
char message[] = "Vacation is near";
display(message, 8);
system("PAUSE");
return 0;
}
7. (Program) Write, compile, and run a C++ program that declares three one-dimensional arrays
named price, quantity, and amount. Each array should be declared in main() and be
capable of holding 10 double-precision numbers. The numbers to store in price are 10.62,
14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, and 3.98. The numbers to store in quantity
are 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, and 4.8. Your program should pass these three arrays to
a function named extend(), which should calculate elements in the amount array as the
product of the corresponding elements in the price and quantity arrays (for example,
amount[1] = price[1] * quantity[1]). After extend() has passed values to the amount
array, the values in the array should be displayed from within main().
#include <iostream>
using namespace std;
void extend(double *price, double *quantity){
const int NUM = 10;
double amount[NUM];
for (int i = 0; i < 10; i++){
amount[i] = price[i] * quantity[i];
cout << amount[i]<< endl;
}
return ;
}
int main(){
const int NUM = 10;
double price[] = { 10.62, 14.89,13.21,16.55,18.62,9.47,6.58,18.32,12.15,3.98 };
double quantity[] = {4, 8.5,6,7.35,9,15.3,3,5.4,2.9,4.8};
extend(price, quantity);
system("PAUSE");
return 0;
}
8. (Program) Write, compile, and run a C++ program that includes two functions named
calcavg() and variance(). The calcavg() function should calculate and return the average
of values stored in an array named testvals. The array should be declared in main() and
include the values 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, and 73. The variance()
function should calculate and return the variance of the data. The variance is obtained by
subtracting the average from each value in testvals, squaring the values obtained, adding
them, and dividing by the number of elements in testvals. The values returned from
calcavg() and variance() should be displayed by using cout statements in main().
#include <iostream>
using namespace std;
double calcavg(int *testvals){
int total = 0;
int average;
for (int i = 0; i < 14; i++){
total = total + testvals[i];
}
average = (total / 14);
return average;
}
double variance(int *testvals, double a){
int total = 0;
for (int i = 0; i < 14; i++){
total = total + ((testvals[i] - a)*(testvals[i] - a));
}
return (total/14);
}
int main(){
int testvals[] = { 89, 95, 72, 83, 99, 54, 86, 75, 92, 73, 79, 75, 82, 73 };
double a = calcavg(testvals);
cout << "Your average is " << calcavg(testvals) << endl;
cout << "Your Variance is " << variance(testvals, a)<< endl;
system("PAUSE");
return 0;
}