# PASS Week 14 Solutions

Dec 7th, 2021 (edited)
847
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. PROBLEM #1
2.
3. #include <stdio.h>
4. #include <stdlib.h>
5. #include <string.h>
6. #include <time.h>
7.
8. struct CAR{
9.
10.     char brand[20];
11.     int vin;
12.
13. };
14.
16. void printArray(struct CAR[], int);
17.
18. int main(){
19.
20.     srand(time(NULL));
21.
22.     int arrayLength = 4;
23.     struct CAR newCar[arrayLength];
24.
26.     printArray(newCar, arrayLength);
27.
28.     return 0;
29. }
30.
31. void loadArray(struct CAR ary[], int length){
32.     for (int i = 0; i < length; i++){
33.         printf("Enter in a brand name for car %d: ", i + 1);
34.         scanf("%s", ary[i].brand);
35.         ary[i].vin = rand() % 9000 + 1000;
36.     }
37.     printf("\n");
38.     return;
39. }
40.
41. void printArray(struct CAR ary[], int length){
42.     for (int i = 0; i < length; i++){
43.         printf("The brand name of car %d is %s.\n", i + 1, ary[i].brand);
44.         printf("The vehicle ID number of car %d is %d. \n", i + 1, ary[i].vin);
45.     }
46.     return;
47. }
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58. PROBLEM #1 Challenge (Only loadArray needs to be modified):
59.
60. void loadArray(CAR ary[], int length){
61.     for (int i = 0; i < length; i++){
62.         printf("Enter in a brand name for car %d: ", i + 1);
63.         scanf("%s", ary[i].brand);
64.         ary[i].vin = rand() % 9000 + 1000;
65.         for (int j = 0; j < length; j++){
66.             if (ary[i].vin == ary[j].vin){
67.                 ary[i].vin = rand() % 9000 + 1000;
68.             }
69.         }
70.     }
71.     printf("\n");
72.     return;
73. }
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94. PROBLEM #2
95.
96. #include <stdio.h>
97. #include <stdlib.h>
98. #include <string.h>
99. #include <time.h>
100.
101. typedef struct CAR{
102.
103.     char brand[20];
104.     int vin;
105.
106. } CAR;
107.
109. void printArray(CAR[], int);
110.
111. int main(){
112.
113.     srand(time(NULL));
114.
115.     int arrayLength = 4;
116.     CAR newCar[arrayLength];
117.
119.     printArray(newCar, arrayLength);
120.
121.     return 0;
122. }
123.
124. void loadArray(CAR ary[], int length){
125.     for (int i = 0; i < length; i++){
126.         printf("Enter in a brand name for car %d: ", i + 1);
127.         scanf("%s", ary[i].brand);
128.         ary[i].vin = rand() % 9000 + 1000;
129.     }
130.     printf("\n");
131.     return;
132. }
133.
134. void printArray(CAR ary[], int length){
135.     for (int i = 0; i < length; i++){
136.         printf("The brand name of car %d is %s.\n", i + 1, ary[i].brand);
137.         printf("The vehicle ID number of car %d is %d. \n", i + 1, ary[i].vin);
138.     }
139.     return;
140. }
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156. PROBLEM #3
157.
158. #include <stdio.h>
159. #include <math.h>
160. typedef struct{
161. int x;
162. int y;
163. int res;
164. } SAMPLE;
165.
166. void PowerUp(SAMPLE*);
167. void PowerUpWrong(SAMPLE);
168.
169. int main(void) {
170.     SAMPLE sample1 = {2, 5, 0}; //Initializes fields/members
171.     SAMPLE* samplePtr;
172.     samplePtr = &sample1; //Create a pointer to the struct
173.
174.     PowerUpWrong(sample1);
175.     printf("The incorrect result is %d\n", sample1.res);
176.
177.     PowerUp(samplePtr);
178.     printf("%d raised to the power of %d is: %d\n", sample1.x, samplePtr->y, sample1.res);
179.     //Note the use of both the indirect and direct selection operators
180.
181.     return 0;
182. }
183.
184. void PowerUpWrong(SAMPLE ptr){
185.     int result = 0;
186.     ptr.res = (int)pow(ptr.x, ptr.y);
187.
188.     return;
189. }
190.
191. void PowerUp(SAMPLE* smp){
192.     int result = 0;
193.     result = (int)pow(smp->x, smp->y);
194.     smp->res = result;
195.
196.     return;
197. }
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215. PROBLEM #4
216.
217. #include <stdio.h>
218.
219. #define N 4 //Size of vectors
220. typedef double SCALAR;
221. typedef SCALAR VECTOR[N];
222. SCALAR Dot_Product(VECTOR x, VECTOR y) {
223.   int i;
224.   SCALAR sum = 0.0;
225.   for (i = 0; i < N; i++) {
226.     sum += x[i] * y[i];
227.   }
228.   return sum;
229. }
230. void Print_Vector(VECTOR T, int sz) {
231.   int i;
232.   printf("The vector: ");
233.   for (i = 0; i < sz; i++) {
234.     printf(" %lf ", T[i]);
235.   }
236.   printf("\n");
237.   return;
238. }
239. int main(void) {
240.   SCALAR dProduct = 0.0;
241.   VECTOR x = {
242.     2,
243.     5.4,
244.     6,
245.     9
246.   };
247.   VECTOR y = {
248.     4.2,
249.     5.4,
250.     6.7,
251.     7
252.   };
253.   Print_Vector(x, N);
254.   Print_Vector(y, N);
255.   dProduct = Dot_Product(x, y);
256.   printf("The Dot Product of X and Y is: %10.2lf\n", dProduct);
257.   return 0;
258. }
RAW Paste Data