M4ritimeSeeker

PASS Week 14: Structs and Typedef

Dec 6th, 2021 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. Here is another pastebin link that contains examples of structs!
  2. I separated the pastebins because the examples have quite a few lines
  3. (The code itself isn't long, I just have a lot of whitespace in my code)
  4.  
  5. https://pastebin.com/L0aV2Tu7
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. WARM-UP PROBLEM
  16.  
  17. Below is an incomplete program that uses a struct STUDENT that contains information about
  18. a student. The struct itself is programmed, and nothing else needs to be added. Your goal
  19. is to declare a STUDENT struct in main and assign the values already in main to the equivalent
  20. variables in the struct.
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. #define ID_SZ 10 //ID Size
  27. #define NM_SZ 25 //Name Size
  28.  
  29. struct STUDENT{
  30. char id[ID_SZ];
  31. char fName[NM_SZ];
  32. double GPA;
  33. };
  34.  
  35. int main(void) {
  36. double gpa = 98.3;
  37. char nameIn[NM_SZ] = "Larry";
  38. char idIn[ID_SZ] = "LS1234";
  39.  
  40. /* Declare Student struct here */
  41.  
  42. /* Assign ID in the struct here */
  43. /* Assign First Name in the struct here */
  44. /* Assign GPA in the struct here */
  45.  
  46. printf("Student %s ID:%s has a %.2lf GPA\n", /* struct name, ID, and GPA here */ );
  47. printf("\n");
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PROBLEM #1: Creating structs and an Array of Structs
  71.  
  72. This program will involve creating a struct called CAR with two data fields: Brand and Vehicle ID.
  73. Then create an array of CAR structs with a size of 4. The car will have a brand
  74. (Ford, Chevy, KIA, Toyota etc.) and will have a RANDOMLY GENERATED VIN or Vehicle Id Number (4 digits in length).
  75. Create a user defined function to load the array by asking the user for the input and then storing it. It returns void.
  76. Then create a second user function used to print out the array of cars and their details. It returns void.
  77.  
  78. The challenge here is how to pass the array of CARs to each function.
  79. (OPTIONAL Challenge: Make sure the vehicle IDs are UNIQUE and have no duplicates)
  80.  
  81.  
  82. SAMPLE OUTPUT:
  83.  
  84. Enter a brand: Ford
  85. Enter a brand: Honda
  86. Enter a brand: Toyota
  87. Enter a brand: Mitsubishi
  88.  
  89. Car 1 has Brand: Ford ID: 5116
  90. Car 2 has Brand: Honda ID: 9966
  91. Car 3 has Brand: Toyota ID: 6543
  92. Car 4 has Brand: Mitsubishi ID: 4452
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. PROBLEM #2: Using Typedef
  104.  
  105. Using the program from the previous problem, use typedef to make CAR a type. You can now
  106. pass the array of CAR without the struct keyword and you can use a pointer to a type now too.
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. Problem #3: Using Pointers to Structs
  123.  
  124. Previously, you saw how an array of structs can be passed to functions as a pointer because the
  125. name of an array acts like a pointer but is still an array. Therefore, we could still use the direct
  126. operator to access members in the structs in the array. Take the incomplete program given
  127. below and add the missing code to return the result of raising x to the power of y by accessing
  128. the values form the ptr to the struct SAMPLE. The function PowerUpWrong is used to
  129. demonstrate how not to do this. When dealing with a pointer to a struct, you access members
  130. using the indirect selection operator ‘->’. Look for “/*your code here*/”.
  131. Skeleton Code missing function code below:
  132.  
  133. #include <stdio.h>
  134. #include <math.h>
  135.  
  136. typedef struct{
  137. int x;
  138. int y;
  139. int res;
  140. } SAMPLE;
  141.  
  142. void PowerUp( /* your code here */ );
  143. void PowerUpWrong(SAMPLE);
  144.  
  145. int main(void) {
  146. SAMPLE sample1 = {2, 5, 0}; //Initializes fields/members
  147. SAMPLE* samplePtr;
  148. samplePtr = &sample1; //Create a pointer to the struct
  149.  
  150. PowerUpWrong(sample1);
  151. printf("The incorrect result is %d\n", sample1.res);
  152.  
  153. PowerUp(samplePtr);
  154. printf("%d raised to the power of %d is: %d\n", sample1.x, samplePtr->y, sample1.res);
  155. //Note the use of both the indirect and direct selection operators
  156.  
  157. return 0;
  158. }
  159.  
  160. void PowerUpWrong(SAMPLE ptr){
  161. int result = 0;
  162. ptr.res = (int)pow(ptr.x, ptr.y);
  163.  
  164. return;
  165. }
  166.  
  167. void PowerUp( /* your code here */ ){
  168. int result = 0;
  169. result = (int)pow( /* your code here */ );
  170. /* your code here */ = result;
  171.  
  172. return;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. PROBLEM #4
  195.  
  196. Construct a program using typedef. The term scalar is used to denote a quantity having magnitude but
  197. not any direction (ascending or descending). A vector is like a scalar but has direction. A sorted list of
  198. numbers could be referred to as a vector. The dot product or scalar product is an algebraic operation
  199. where two equally sized sequences of numbers are computed to a single number. Your program should
  200. define a value N with a value of 4. Next, declare a new type of scalar of type double and name it SCALAR.
  201. Now create a second new type pf SCALAR that is called a VECTOR and give it a size of N. In main, declare
  202. and define two new VECTORs X and Y. Initialize them with any 4 values. Create a user defined function
  203. called Dot_Product that takes two VECTORs and returns a SCALAR value back to the caller. In main call
  204. the new function and print out the computed value. Sample output is shown below
  205.  
  206. SAMPLE OUTPUT:
  207.  
  208. The vector: 2.000000 5.400000 6.000000 9.000000
  209. The vector: 4.200000 5.400000 6.700000 7.000000
  210. The Dot Product of X and Y is: 140.76
Add Comment
Please, Sign In to add comment