-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.cpp
More file actions
150 lines (144 loc) · 3.92 KB
/
pointers.cpp
File metadata and controls
150 lines (144 loc) · 3.92 KB
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
// compile: g++ -std=c++11 -o pointers pointers.cpp
// Bernadette Dehnert
// https://github.com/bmcecilia3/OSAssignment1
#include <iostream>
#include <string>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
typedef struct Student {
int id;
char *f_name;
char *l_name;
int n_assignments;
double *grades;
} Student;
void calculateStudentAverage(void *object, double *avg);
int main(int argc, char **argv)
{
Student student;
int flag = 0;
char *first;
char *last;
first = (char *)malloc(sizeof(char) * 128);
last = (char *)malloc(sizeof(char) * 128);
// Sequence of user input -> store in fields of `student`
while(flag == 0)
{
char idTry[9];
printf("Please enter the student's id number: ");
std::cin >> idTry;
if(string(to_string(atoi(idTry))).length() < string(idTry).length() || idTry == NULL || !isdigit(idTry[0]))
{
printf("Sorry, I cannot understand your answer\n");
}
else{
student.id = atoi(idTry);
flag = 1;
}
}
flag = 0;
while(flag == 0)
{
//char first[128];
printf("Please enter the student's first name: ");
std::cin >> first;
if(first == NULL)
{
printf("Sorry, I cannot understand your answer\n");
}
else{
student.f_name = first;
flag = 1;
}
}
flag = 0;
while(flag == 0)
{
//char last[128];
printf("Please enter the student's last name: ");
std::cin >> last;
if(last == NULL)
{
printf("Sorry, I cannot understand your answer\n");
}
else{
student.l_name = last;
flag = 1;
}
}
flag = 0;
while(flag == 0)
{
char assignment[10];
printf("Please enter how many assignments were graded: ");
std::cin >> assignment;
if(string(to_string(atoi(assignment))).length() < string(assignment).length() || assignment == NULL || !isdigit(assignment[0]))
{
printf("Sorry, I cannot understand your answer\n");
}
else{
student.n_assignments = atoi(assignment);
flag = 1;
}
}
int i;
printf("\n");
double allGrades[student.n_assignments];
for(i = 0; i < student.n_assignments; i++)
{
flag = 0;
while(flag == 0)
{
char grade[10];
printf("Please enter grade for assignment %d: ", i);
std::cin >> grade;
if(string(grade).find(".") == std::string::npos)
{
if(string(to_string(atoi(grade))).length() < string(grade).length() || grade == NULL || !isdigit(grade[0]))
{
printf("Sorry, I cannot understand your answer\n");
}
else{
allGrades[i] = atof(grade);
flag = 1;
}
}
else{
string g = string(grade);
size_t dot = g.find(".");
string after = g.substr(dot + 1);
string before = g.substr(0, dot);
if(string(to_string(atoi(before.c_str()))).length() < string(before.c_str()).length() || before.c_str() == NULL || !isdigit(before.c_str()[0]))
{
printf("Sorry, I cannot understand your answer\n");
}
else if(string(to_string(atoi(after.c_str()))).length() < string(after.c_str()).length() || after.c_str() == NULL || !isdigit(after.c_str()[0]))
{
printf("Sorry, I cannot understand your answer\n");
}
else{
allGrades[i] = atof(grade);
flag = 1;
}
}
}
}
// Call `CalculateStudentAverage(???, ???)`
calculateStudentAverage(&student, allGrades);
// Output `average`
printf("\n");
printf("Student: %s %s [%d]\n\tAverage grade: %f\n", student.f_name, student.l_name, student.id, allGrades[0]);
return 0;
}
void calculateStudentAverage(void *object, double *avg)
{
int i;
double sum = 0;
Student student = *(Student*)(object);
for(i = 0; i < student.n_assignments; i++)
{
sum = sum + avg[i];
}
avg[0] = (sum / ((Student*)object)->n_assignments);
}