-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 7.4 (Completed)
688 lines (552 loc) · 17.7 KB
/
Exercise 7.4 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
1. (Practice) Write specification statements for the following:
a. An array of integers with 6 rows and 10 columns
const int NUMROWS = 6;
const int NUMCOLS = 10;
int val[NUMROWS][NUMCOLS];
b. An array of integers with 2 rows and 5 columns
const int NUMROWS = 2;
const int NUMCOLS = 5;
int val[NUMROWS][NUMCOLS];
c. An array of characters with 7 rows and 12 columns
const int NUMROWS = 7;
const int NUMCOLS = 12;
char val[NUMROWS][NUMCOLS];
d. An array of characters with 15 rows and 7 columns
const int NUMROWS = 15;
const int NUMCOLS = 7;
char val[NUMROWS][NUMCOLS];
e. An array of double-precision numbers with 10 rows and 25 columns
const int NUMROWS = 10;
const int NUMCOLS =7;
char val[NUMROWS][NUMCOLS];
f. An array of double-precision numbers with 16 rows and 8 columns
const int NUMROWS = 16;
const int NUMCOLS = 8;
char val[NUMROWS][NUMCOLS];
2. (Desk check) Determine the output produced by the following program:
#include <iostream>
using namespace std;
int main()
{
int i, j, val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
cout << val[i][j] << “ “;
return 0;
}
The output is as follows:
8 16 9 52 3 15 27 6 14 25 2 10
3. (Program) a. Write, compile, and run a C++ program that adds the values of all elements in
the val array used in Exercise 2 and displays the total.
#include <iostream>
using namespace std;
int main(){
int i, j, val[3][4] = { 8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10 };
int total = 0;
for (i = 0; i < 3; i++){
for (j = 0; j < 4; j++){
cout << val[i][j] << " " << endl;
total = total + val[i][j];
}
}
cout << "The total is " << total << endl;
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 3a to display the total of each row separately.
#include <iostream>
using namespace std;
int main(){
int i, j, val[3][4] = { 8, 16, 9, 52, 3, 15, 27, 6, 14, 25, 2, 10 };
int total = 0;
for (i = 0; i < 3; i++){
for (j = 0; j < 4; j++){
total = total + val[i][j];
cout << val[i][j] << " " <<"The new total is " << total << endl;
}
}
system("PAUSE");
return 0;
}
4. (Program) Write, compile, and run a C++ program that adds equivalent elements of the twodimensional
arrays named first and second. Both arrays should have two rows and three columns.
For example, element [1][2] of the resulting array should be the sum of first[1][2]
and second[1][2]. The first and second arrays should be initialized as follows:
first second
16 18 23 24 52 77
54 91 11 16 19 59
#include <iostream>
using namespace std;
int main(){
int i, j, first[2][3] = { 16,18,23,54,91,11 };
int second[2][3] = { 24, 52, 77, 16, 19, 59 };
int total = 0;
for (i = 0; i < 2; i++){
for (j = 0; j < 3; j++){
total = second[i][j] + first[i][j];
cout << total << endl;
}
}
system("PAUSE");
5. (Program) a. Write, compile, and run a C++ program that finds and displays the maximum value
in a two-dimensional array of integers. The array should be declared as a 4-by-5 array of integers
and initialized with the data 16, 22, 99, 4, 18, -258, 4, 101, 5, 98, 105, 6, 15, 2, 45, 33, 88, 72, 16, and 3.
#include <iostream>
using namespace std;
int main(){
double arr[4][5] = { 16, 22, 99, 4, 18,
-258, 4, 101, 5, 98,
105, 6, 15, 2, 45,
33, 88, 72, 16, 3 };
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (arr[i][j] > arr[0][0]){
arr[0][0] = arr[i][j];
}
}
}
cout << arr[0][0] << endl;
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 5a so that it also displays the maximum value’s row
and column subscript values.
#include <iostream>
using namespace std;
int main(){
double arr[4][5] = { 16, 22, 99, 4, 18,
-258, 4, 101, 5, 98,
105, 6, 15, 2, 45,
33, 88, 72, 16, 3 };
int a = 0; int b = 0;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
if (arr[i][j] > arr[0][0]){
arr[0][0] = arr[i][j];
a = i;
b = j;
}
}
}
cout << arr[0][0] << setw(10) << a << setw(10)<< b << endl;
system("PAUSE");
return 0;
}
6. (Program) Write, compile, and run a C++ program that selects the values in a 4-by-5 array of
positive integers in increasing order and stores the selected values in a one-dimensional array
named sort. Use the data given in Exercise 5a to initialize the two-dimensional array.
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
const int SIZE = 20;
int arr[4][5] = { 16, 22, 99, 4, 18,
-258, 4, 101, 5, 98,
105, 6, 15, 2, 45,
33, 88, 72, 16, 3 };
int *sort = (int*)arr;
int temp;
std::sort(sort, sort + SIZE);
for (int k = 0; k<20; k++) {
cout << sort[k] <<endl;
//printf("arr[%d] = %2d\n", k, sort[k]);
}
system("PAUSE");
return 0;
}
7. (Program) a. A professor has constructed a 3-by-5 two-dimensional array of grades. This array
contains the test grades of students in the professor’s advanced compiler design class. Write,
compile, and run a C++ program that reads 15 array values and then determines the total number
of grades in these ranges: less than 60, greater than or equal to 60 and less than 70, greater
than or equal to 70 and less than 80, greater than or equal to 80 and less than 90, and greater
than or equal to 90.
#include <iostream>
using namespace std;
int main() {
const int SIZE = 15;
int arr[3][5];
int *arr2 = (int*)arr;
int count = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0;
for(int j =0; int j=15; j++){
for(int k =0; int k = 15; j++){
cin << arr[j][k]<< endl;
}
}
for (int i = 0; i < 15; i++){
if (arr2[i] < 60){
count++;
}
else if (arr2[i] >= 60 && arr2[i] <= 70){
count2++;
}
else if (arr2[i] >= 70 && arr2[i] <= 80){
count3++;
}
else if (arr2[i] >= 80 && arr2[i] <= 90){
count4++;
}
else if (arr2[i] >= 90){
count5++;
}
} cout << "Less than 60 " << count << endl;
cout << "Greater than or equal to 60 and less than 70 " << count2 << endl;
cout << "Greater than or equal to 70 and less than 80 " << count3 << endl;
cout << "Greater than or equal to 80 and less than 90 " << count4 << endl;
cout << "Greater than or equal to 90 " << count5 << endl;
system("PAUSE");
return 0;
}
b. Entering 15 grades each time you run the program written for Exercise 7a is cumbersome.
What method can be used for initializing the array during the testing phase?
int arr[3][5] = { 100, 22, 99,40,90, 89,78, 50, 98,100, 61, 94,95, 67 ,100};
c. How might the program you wrote for Exercise 7a be modified to include the case of no
grade being input? That is, what grade could be used to indicate an invalid grade, and how
would your program have to be modified to exclude counting an invalid grade?
if(arr2[i] == NULL){cin.ignore}
//if the input is invalid than the value that was entered can be ignored.
8. (Program) a. Create a two-dimensional list of integer part numbers and quantities of each
part in stock, and write a function that displays data in the array in decreasing quantity order.
No more than 100 different parts are being tracked. Test your program with the following data:
Part No. Quantity
1001 62
949 85
1050 33
867 125
346 59
1025 105
#include <iostream>
using namespace std;
int main() {
int a[][6] = {
{ 62, 85, 33,125, 59,105 }, // Quantity
{ 1001,949,1050,867,346,1025 } // Part No.
};
int i, j, k, temp, temp1;
//Bubble sorting is applieed on one first row while the other row is swapped
for (j = 1; j<6; j++)
{
for (i = 0; i<5; i++)
{
if (a[0][i]<a[0][i + 1])
{
temp = a[0][i];
a[0][i] = a[0][i + 1];
a[0][i + 1] = temp;
temp1 = a[1][i];
a[1][i] = a[1][i + 1];
a[1][i + 1] = temp1;
}
}
}
printf("\n\nArray after sorting:\n");
for (i = 0; i <2; i++)
{
for (j = 0; j<6; j++)
{
printf("%d\t", a[i][j]); //printing sorted array
}
printf("\n");
}
system("PAUSE");
return 0;
}
b. Modify the function written in Exercise 8a to display the data in part number order.
#include <iostream>
using namespace std;
int main() {
int a[][6] = {
{ 1001, 949, 1050, 867, 346, 1025 }, // Part No.
{ 62, 85, 33, 125, 59, 105 } // Quantity
};
int i, j, k, temp, temp1;
//Bubble sorting is applieed on one first row while the other row is swapped
for (j = 1; j<6; j++)
{
for (i = 0; i<5; i++)
{
if (a[0][i]<a[0][i + 1])
{
temp = a[0][i];
a[0][i] = a[0][i + 1];
a[0][i + 1] = temp;
temp1 = a[1][i];
a[1][i] = a[1][i + 1];
a[1][i + 1] = temp1;
}
}
}
printf("\n\nArray after sorting:\n");
for (i = 0; i <2; i++)
{
for (j = 0; j<6; j++)
{
printf("%d\t", a[i][j]); //printing sorted array
}
printf("\n");
}
system("PAUSE");
return 0;
}
9. (Program) a. Your professor has asked you to write a C++ program that determines grades for
five students at the end of the semester. For each student, identified by an integer number, four
exam grades must be kept, and two final grade averages must be computed. The first grade
average is simply the average of all four grades. The second grade average is computed by
weighting the four grades as follows: The first grade gets a weight of 0.2, the second grade gets
a weight of 0.3, the third grade gets a weight of 0.3, and the fourth grade gets a weight of 0.2.
That is, the final grade is computed as follows:
0.2 * grade1 0.3 * grade2 0.3 * grade3 0.2 * grade4
Using this information, construct a 5-by-6 two-dimensional array, in which the first column is
used for the student number, the next four columns for the grades, and the last two columns
for the computed final grades. The program’s output should be a display of the data in the
completed array. For testing purposes, the professor has provided the following data:
Student Grade 1 Grade2 Grade 3 Grade 4
1 100 100 100 100
2 100 0 100 0
3 82 94 73 86
4 64 74 84 94
5 94 84 74 64
Regular Final Grade:
Weight Final Grade:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
double grade[5][6] = { { 1, 100, 100, 100, 100 },
{ 2, 100, 0, 100, 0 },
{ 3, 82, 94, 73, 86 },
{ 4, 64, 74, 84, 94 },
{ 5, 94, 84, 74, 64 },
};
for (int i = 0; i < 5; i++){
for (int j = 0; j < 6; j++){
grade[i][6] = (grade[i][1] * .2) + (grade[i][2] * .3) + (grade[i][2] * .3) + (grade[i][4] * .2);
grade[i][5] = (grade[i][1] + grade[i][2] + grade[i][3] + grade[i][4]) / 4;
}
}
for (int i = 0; i < 5; i++){
cout << "This is the regualar average for Student " << (i + 1) << endl;
cout << grade[i][5] << endl;
}
for (int k = 0; k < 5; k++){
cout << "This is the weighed final grade for Student " << (k+1)<< endl;
cout << grade[k][6] << endl;
}
system("PAUSE");
return 0;
}
b. What modifications would you need to make to your program so that it can handle 60 students
rather than 5?
Increasing the range of the variable i would also increase the number of students that can be handled.
Ex. for(int i = 0; i < 60; i++)
c. Modify the program written for Exercise 9a by adding an eighth column to the array. The
grade in the eighth column should be calculated by computing the average of the top three
grades only.
#include <iostream>
using namespace std;
int main() {
int total = 0,lowest=0;
int grade[5][7] = { { 1, 100, 100, 100, 100 },
{ 2, 100, 0, 100, 0 },
{ 3, 82, 94, 73, 86 },
{ 4, 64, 74, 84, 94 },
{ 5, 94, 84, 74, 64 },
};
for (int i = 0; i < 5; i++){
for (int j = 0; j < 7; j++){
grade[i][6] = (grade[i][1] * .2) + (grade[i][2] * .3) + (grade[i][2] * .3) + (grade[i][4] * .2);
grade[i][5] = (grade[i][1] + grade[i][2] + grade[i][3] + grade[i][4]) / 4;
total = grade[i][1] + grade[i][2] + grade[i][3] + grade[i][4];
if (grade[i][7] <= grade[i][lowest])
lowest = j;
total -= grade[lowest][7];
grade[i][7] = (total / 3);
}
}
for (int i = 0; i < 5; i++){
cout << "This is the regualar average for Student " << (i + 1) << endl;
cout << grade[i][5] << endl;
}
for (int k = 0; k < 5; k++){
cout << "This is the weighed final grade for Student " << (k+1)<< endl;
cout << grade[k][6] << endl;
}
for (int m = 0; m < 5; m++){
cout << "The best grades final grade solution of student " << (m + 1) << endl;
cout << grade[m][7] << endl;
}
system("PAUSE");
return 0;
}
10. (Program) The answers to a true-false test are as follows: T T F F T. Given a two-dimensional
answer array, in which each row corresponds to the answers provided on one test, write a function
that accepts the two-dimensional array and number of tests as parameters and returns a
one-dimensional array containing the grades for each test. (Each question is worth 5 points so
that the maximum possible grade is 25.) Test your function with the following data:
Test 1: T F T T T
Test 2: T T T T T
Test 3: T T F F T
Test 4: F T F F F
Test 5: F F F F F
Test 6: T T F T F
#include <iostream>
using namespace std;
int *getMarks(const char answers[][5], size_t ntests);
int main()
{
char tests[6][5] = {
{ 'T', 'F', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'F', 'F', 'T' },
{ 'F', 'T', 'F', 'F', 'F' },
{ 'F', 'F', 'F', 'F', 'F' },
{ 'T', 'T', 'F', 'T', 'F' },
};
int *marks = getMarks(tests, 6);
for (size_t j = 0; j < 6; j++)
std::cout << marks[j] << " ";
system("PAUSE");
return 0;
}
int *getMarks(const char answers[][5], size_t ntests)
{
const char answer[5] = { 'T', 'T', 'F', 'F', 'T' };
int *marks = new int[ntests]();
const int mark = 5;
for (size_t j = 0; j < ntests; j++)
for (size_t i = 0; i < 5; i++)
if (answers[j][i] == answer[i])
marks[j] += mark;
return marks;
}
11. (Modify) Modify the function you wrote for Exercise 10 so that each test is stored in column
order rather than row order.
#include <iostream>
using namespace std;
int *getMarks(const char answers[][5], size_t ntests);
int main()
{
char tests[6][5] = {
{ 'T', 'F', 'T', 'T', 'T' },
{ 'T', 'T', 'T', 'T', 'T' },
{ 'T', 'T', 'F', 'F', 'T' },
{ 'F', 'T', 'F', 'F', 'F' },
{ 'F', 'F', 'F', 'F', 'F' },
{ 'T', 'T', 'F', 'T', 'F' },
};
int *marks = getMarks(tests, 6);
for (size_t j = 0; j < 6; j++)
std::cout <<"The score of Test "<< j+1 <<" is "<< marks[j] << " "<< endl;
system("PAUSE");
return 0;
}
int *getMarks(const char answers[][5], size_t ntests)
{
const char answer[5] = { 'T', 'T', 'F', 'F', 'T' };
int *marks = new int[ntests]();
const int mark = 5;
for (size_t j = 0; j < ntests; j++)
for (size_t i = 0; i < 5; i++)
if (answers[j][i] == answer[i])
marks[j] += mark;
return marks;
}
12. (Program) A three-dimensional weather array for the months July and August 2011 has columns
labeled by the month numbers 7 and 8. In each column, there are rows numbered 1
through 31, representing the days, and for each day, there are two ranks labeled H and L,
representing the day’s high and low temperatures. Use this information to write a C++ program
that assigns the high and low temperatures for each element of the arrays. Then allow
the user to request the following:
• Any day’s high and low temperatures
• Average high and low temperatures for a given month
• Month and day with the highest temperature
• Month and day with the lowest temperature
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
const int ROW = 31;
const int COLUMN = 2;
const int RANK = 2;
int arr[ROW][COLUMN][RANK];
int averageJuly, averageAugust, total1, total2,highJuly,lowJuly,highAug,lowAug;
int dayhighAug, daylowAug,dayhighJuly,daylowJuly;
char month;
int i;
for (int i = 0; i < 31; i++){
for (int j = 0; j < 2;j++){
for (int k = 0; k < 2; k++){
cout << "Enter high temeratures of the month" << j+7<<" for " << i+1 << "day" << endl;
std::cin >> arr[i][j][0];
cout << "Enter low temeratures of the month" << j+7<< " for " << i+1 << "day" << endl;
std::cin >> arr[i][j][1];
total1 = +arr[i][j][0];
total2 = +arr[i][j][1];
averageJuly = total1 / 31;
averageAugust = total2 / 31;
if (arr[i][0][0] > arr[0][0][0])
{
arr[0][0][0] = arr[i][0][1];
highJuly = arr[0][0][0];
}
if (arr[i][0][0] < arr[0][0][0])
{
arr[0][0][0] = arr[i][0][1];
lowJuly = arr[0][0][0];
}
if (arr[i][1][1] > arr[0][0][0])
{
arr[0][0][0] = arr[i][1][1];
highAug = arr[0][0][0];
}
if (arr[i][1][1] < arr[0][0][0])
{
arr[0][0][0] = arr[i][1][1];
lowAug = arr[0][0][0];
}
dayhighAug = arr[i][0][0];
daylowAug = arr[i][0][1];
dayhighJuly = arr[i][1][0];
daylowJuly = arr[i][1][1];
}
}
}
cout << "Any day's high and low temperatures" << endl;
std::cin >> month;
if (month == 'A'){
cout << "Enter day" << endl;
std::cin >> i;
cout << dayhighAug << endl;
cout << daylowAug << endl;
}
else if (month == 'J'){
cout << "Enter day" << endl;
std::cin >> i;
cout << dayhighJuly << endl;
cout << daylowJuly << endl;
}
cout << "Average high and low temperatures for a given month" << endl;
std::cin >> month;
if (month == 'A'){
cout << averageAugust << endl;
}
else if (month == 'J'){
cout << averageJuly << endl;
}
cout << "Month and day with the highest temperature" << endl;
std::cin >> month;
if (month == 'A'){
cout << highAug << endl;
}
else if (month == 'J'){
cout << highJuly << endl;
}
cout << "Month and day with the lowest teperature" << endl;
std:: cin >> month;
if (month == 'A'){
cout << lowAug << endl;
}
else if (month == 'J'){
cout << lowJuly << endl;
}
system("PAUSE");
return 0;
}