aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/LAB_8_CODE_RUN and Answers for 11.7.txt
blob: 1ba962d9d555d83169d5c573a9317e284e63dd52 (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
Lab 8  Exercises                                               Benjamin Schroeder

######################################################################################################################
13a
11.7 Exercises
p 317
4 pts #5-6
Submit: Statements for 5, Judgements for 6

###########################################
#5
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios; 

const int RECORDS = 100;
const int MAX = 4;

int main()
/*
{
    string records;
    int num_records = 0;
    
    ifstream input_file;    //define input file
    ofstream output_file;   //define output file
    char lname[21], id[5];
    int age;

    input_file.open("Sample.txt");     // open the input file
    output_file.open("Report.txt");    // open the output file


    if (input_file.is_open() && output_file.is_open())
    {
        input_file >> lname[num_records]<<id[num_records]<<age[num_records];  //priming read

        while (!input_file.eof())
        {
            num_records++;
            input_file>> lname[num_records]<<id[num_records]<<age[num_records];
        }
        input_file.close();   //close input file
    else
        cout<<"Error: Unable to open input_file." << endl;



    }

    input_file.close();   // close input file
    output_file.close();  // close output file


    return 0;
    }





##############################################
#6
a) False
b) True
c) False
d) False
e) True






#############################################################################################################################
13b
11.9 Learn by Doing Exercises
p 323
10 pts #1: write a full program to call the function
Submit: code & run
////////////////////////////////////////////////  CODE   /////////////////////////////////////////

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <stdio.h>
using namespace std;
const int RECORDS = 100;
const int MAX = 4;
int main()
{
    int records[RECORDS]{};
    int num_records = 0;
    ifstream inFile;
    FILE* inPtr;
    // open the file
    fopen_s(&inPtr, "C:\\Users\\Lenovo\\source\\repos\\cst115-lab8-BensProgramma\\CST116F2021-Lab8\\11_13_Average_Data.txt", "r");
    int inInt;
    // read the file
    if (inPtr != 0)
    {
        if (inPtr != NULL)
        {
            int j = 0;
            while (fscanf_s(inPtr, "%i", &inInt) >= 0)
            {
                records[num_records] = inInt;
                num_records++;
                j++;
            }
        }
    }
    else
        cout << "Error:   The File can not be opened";

    // close the file
    fclose(inPtr);

    cout << "Given List:\n";
    for (int r = 0; r < num_records; r++)
    {
        cout << records[r] << " ";
    }
    // Sort the numbers smallest to largest
    int j = 0, k = 0, temp = 0;
    for (j = 0; j < num_records; j++)
    {
        for (k = 0; k < num_records - 1; k++)
        {
            if (records[k] > records[k + 1])
            {
                temp = records[k];
                records[k] = records[k + 1];
                records[k + 1] = temp;
            }
        }
    }
    cout << "\nSorted List:\n";
    for (int r = 0; r < num_records; r++)
    {
        cout << records[r] << " ";
    }


    // Find the median of the list
    float median = 0;
    // if number of elements are even
    if (num_records % 2 == 0)
        median = (records[(num_records - 1) / 2] + records[num_records / 2]) / 2.0;
    // if number of elements are odd
    else
        median = records[num_records / 2];
    cout << "\nMedian: " << median << "\n";
}


////////////////////////////////////////////////   RUN  /////////////////////////////////////////
Given List:
15 25 12 14 26 32 52 12 27 44 2 32
Sorted List:
2 12 12 14 15 25 26 27 32 32 44 52
Median: 25.5

C:\Users\Lenovo\Source\Repos\cst115-lab8-BensProgramma\x64\Debug\CST116F2021-Lab8.exe (process 21852) 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 . . .



##############################################################################################################################
13c
11.13 Debugging Exercises
pp 333-336
10 pts 
Submit: code & runs




###############################################################################################################################
13d
11.14 Programming Exercises
pp. 336-337
10 pts #1
Submit: code & run

34 pts