aboutsummaryrefslogtreecommitdiff
path: root/LAB5-Ansari-Answers.txt
blob: b2740570c2889011fddfa495e7c6f21d178e8233 (plain) (blame)
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
9a
10.5 Learn by Doing Exercises
p 247
10 pts #1
Submit: code & run

#include <iostream>
using namespace std;
const int NUMSTUDENTS = 10;
const int NUMGRA = 5;

int main()
{
    float scoreArray[NUMSTUDENTS] = { 0.0 };
    string gradeArray[NUMSTUDENTS];
    int numGradeArray[NUMGRA] = { 0 };
    float avg = 0.0;
    for (int i = 0; i < NUMSTUDENTS; i++) {

        cout << "Enter score " << i + 1 << ": " << endl;
        cin >> scoreArray[i];
        avg += scoreArray[i];



    }

    for (int n = 0; n < NUMSTUDENTS; n++) {

        if (scoreArray[n] >= 92) {

            gradeArray[n] = "A";
            numGradeArray[0] += 1;

        }

        else if (scoreArray[n] >= 84) {

            gradeArray[n] = "B";
            numGradeArray[1] += 1;

        }

        else if (scoreArray[n] >= 75) {

            gradeArray[n] = "C";
            numGradeArray[2] += 1;

        }
        else if (scoreArray[n] >= 65) {

            gradeArray[n] = "D";
            numGradeArray[3] += 1;

        }

        else {


            gradeArray[n] = "F";
            numGradeArray[4] += 1;

        }

    }

    for (int x = 0; x < 10; x++) {

        cout << scoreArray[x] << " = " << gradeArray[x] << endl;

    }

    cout << "Average is: " << avg / 10 << endl;

    cout << "The amount of people that got an A, B, C, D, and F in order are listed below: " << endl;

    for (int y = 0; y < 5; y++) {

        cout << numGradeArray[y] << endl;

    }


}

OUTPUT:

Enter score 1:
65
Enter score 2:
87
Enter score 3:
98
Enter score 4:
98
Enter score 5:
98
Enter score 6:
23
Enter score 7:
12
Enter score 8:
65
Enter score 9:
78
Enter score 10:
89
65 = D
87 = B
98 = A
98 = A
98 = A
23 = F
12 = F
65 = D
78 = C
89 = B
Average is: 71.3
The amount of people that got an A, B, C, D, and F in order are listed below:
3
2
1
2
2

C:\Users\ansar\source\repos\OIT Brushup\Debug\OIT Brushup.exe (process 13084) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

--------------------------------------------------------------------------------------------------------------
9b
10.6 Learn by Doing Exercises
p 253
Break into 3 files: the main() file, the functions .cpp file and the .h file.
10 pts #1
Submit: code & runs

File 1: (Containing Main)

// LAB5 - ANSARI.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "CommonInfo.h"


int main()
{
    float scoreArray[NUMSTUDENTS] = { 0.0 };
    string gradeArray[NUMSTUDENTS];
    int numGradeArray[NUMGRA] = { 0 };
    float avg = 0.0;

    readInput(scoreArray, avg);
    calcGrade(scoreArray, gradeArray, numGradeArray);
    printInfo(scoreArray, gradeArray, numGradeArray, avg);


}


File 2: (Containing Functions)

#include "CommonInfo.h"

void readInput(float arr1[], float& avg) {

    for (int i = 0; i < NUMSTUDENTS; i++) {

        cout << "Enter score " << i + 1 << ": " << endl;
        cin >> arr1[i];
        avg += arr1[i];



    }


}

void calcGrade(float arr1[], string arr2[], int arr3[]) {

    for (int n = 0; n < NUMSTUDENTS; n++) {

        if (arr1[n] >= 92) {

            arr2[n] = "A";
            arr3[0] += 1;

        }

        else if (arr1[n] >= 84) {

            arr2[n] = "B";
            arr3[1] += 1;

        }

        else if (arr1[n] >= 75) {

            arr2[n] = "C";
            arr3[2] += 1;

        }
        else if (arr1[n] >= 65) {

            arr2[n] = "D";
            arr3[3] += 1;

        }

        else {


            arr2[n] = "F";
            arr3[4] += 1;

        }

    }


}


void printInfo(float arr1[], string arr2[], int arr3[], float& avg) {

    for (int x = 0; x < 10; x++) {

        cout << arr1[x] << " = " << arr2[x] << endl;

    }



    cout << "Students that got an A: " << arr3[0] << endl;
    cout << "Students that got an B: " << arr3[1] << endl;
    cout << "Students that got an C: " << arr3[2] << endl;
    cout << "Students that got an D: " << arr3[3] << endl;
    cout << "Students that got an F: " << arr3[4] << endl;





    cout << "Average is: " << avg / 10;


    


}

File 3: (containing Header file)

#include <iostream>
using namespace std;
const int NUMSTUDENTS = 10;
const int NUMGRA = 5;
void readInput(float[], float& avg);
void calcGrade(float[], string[], int[]);
void printInfo(float[], string[], int[], float& avg);


OUTPUT:

Enter score 1:
12
Enter score 2:
23
Enter score 3:
4
Enter score 4:
56
Enter score 5:
76
Enter score 6:
587
Enter score 7:
89
Enter score 8:
67
Enter score 9:
90
Enter score 10:
57
12 = F
23 = F
4 = F
56 = F
76 = C
587 = A
89 = B
67 = D
90 = B
57 = F
Students that got an A: 1
Students that got an B: 2
Students that got an C: 1
Students that got an D: 1
Students that got an F: 5
Average is: 106.1
C:\Users\ansar\source\repos\LAB5 - Ansari - FIXED\Debug\LAB5 - Ansari - FIXED.exe (process 19884) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

-------------------------------------------------------------------------------------------------------------------

10a
10.7 Learn by Doing Exercises
p 260
Break into at least 3 files as in 9b.
10 pts #2
Submit: code & runs

CODE:

FILE 1 (Main):

// CST116F2021-Lab5.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "Header.h"


int main()
{
    //std::cout << "Hello World!\n";

    char lastName[30]{};
    char firstName[30]{};
    char fullName[60]{};

    
    printResults(lastName, firstName, fullName);

}

FILE 2(Function)

#include "Header.h"


void printResults(char lastName[30], char firstName[30], char fullName[60]) {

    cout << "Enter Last name: ";

    cin.getline(lastName, 30);

    cout << endl;

    cout << "Enter first name: ";

    cin.getline(firstName, 30);

    int x = 0;

    while (firstName[x] != '\0') {

        fullName[x] = firstName[x];
        x++;


    }


    //cout << x;

    fullName[x] = ',';
    x++;

    fullName[x] = ' ';

    x++;

    int y = 0;

    while (lastName[y] != '\0') {

        fullName[x] = lastName[y];
        y++;
        x++;
    }

    cout << fullName;


}

FILE 3(Header file)

#include <iostream>
using namespace std;
void printResults(char lastName[30], char firstName[30], char fullName[60]);


OUTPUT:

Enter Last name: Ansari

Enter first name: Rayyan
Rayyan, Ansari
C:\Users\ansar\source\repos\OIT Brushup\Debug\OIT Brushup.exe (process 2456) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

---------------------------------------------------------------------------------------

10b
10.8 Exercises
p 273
Break into at least 3 files as in 9b.
10 pts #7 Write a full program to do this.
Submit: code & runs

NOTE: For this question, the books says chech the first 6 characters of string one. Since "Frank" is only 5, I am assuming they want the 6th
character to be checked as the carrige return. Hence why when Frank is compared to Franklyn, it comes as false because the program compares
Frank(\0) to Frankl

FILE 1 (MAIN)

#include "CommonInfo.h"




int main()
{
    char string1[] = {"Frank"};
    char string2[] = { "Franklyn" };

    

    

    if (checkIfEqual(string1, string2)) {
        
        cout << "Same";

    
    }
    else {
    
        cout << "Different";
    
    }


}

FILE 2 (Functions)

#include "CommonInfo.h"

bool checkIfEqual(char string1[], char string2[]) {

    for (int x = 0; x < 6; x++) {

        if (string1[x] != string2[x]) {


            return false;

        }

    }
    return true;

}


FILE 3(Header)

#include <iostream>
using namespace std;
const int NUMSTUDENTS = 10;
const int NUMGRA = 5;
bool checkIfEqual(char string1[], char string2[]);

OUTPUT:

Different
C:\Users\ansar\source\repos\LAB5 - Ansari - FIXED\Debug\LAB5 - Ansari - FIXED.exe (process 18420) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .