Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here is another pastebin link that contains examples of structs!
- I separated the pastebins because the examples have quite a few lines
- (The code itself isn't long, I just have a lot of whitespace in my code)
- https://pastebin.com/L0aV2Tu7
- WARM-UP PROBLEM
- Below is an incomplete program that uses a struct STUDENT that contains information about
- a student. The struct itself is programmed, and nothing else needs to be added. Your goal
- is to declare a STUDENT struct in main and assign the values already in main to the equivalent
- variables in the struct.
- #include <stdio.h>
- #include <string.h>
- #define ID_SZ 10 //ID Size
- #define NM_SZ 25 //Name Size
- struct STUDENT{
- char id[ID_SZ];
- char fName[NM_SZ];
- double GPA;
- };
- int main(void) {
- double gpa = 98.3;
- char nameIn[NM_SZ] = "Larry";
- char idIn[ID_SZ] = "LS1234";
- /* Declare Student struct here */
- /* Assign ID in the struct here */
- /* Assign First Name in the struct here */
- /* Assign GPA in the struct here */
- printf("Student %s ID:%s has a %.2lf GPA\n", /* struct name, ID, and GPA here */ );
- printf("\n");
- return 0;
- }
- PROBLEM #1: Creating structs and an Array of Structs
- This program will involve creating a struct called CAR with two data fields: Brand and Vehicle ID.
- Then create an array of CAR structs with a size of 4. The car will have a brand
- (Ford, Chevy, KIA, Toyota etc.) and will have a RANDOMLY GENERATED VIN or Vehicle Id Number (4 digits in length).
- Create a user defined function to load the array by asking the user for the input and then storing it. It returns void.
- Then create a second user function used to print out the array of cars and their details. It returns void.
- The challenge here is how to pass the array of CARs to each function.
- (OPTIONAL Challenge: Make sure the vehicle IDs are UNIQUE and have no duplicates)
- SAMPLE OUTPUT:
- Enter a brand: Ford
- Enter a brand: Honda
- Enter a brand: Toyota
- Enter a brand: Mitsubishi
- Car 1 has Brand: Ford ID: 5116
- Car 2 has Brand: Honda ID: 9966
- Car 3 has Brand: Toyota ID: 6543
- Car 4 has Brand: Mitsubishi ID: 4452
- PROBLEM #2: Using Typedef
- Using the program from the previous problem, use typedef to make CAR a type. You can now
- pass the array of CAR without the struct keyword and you can use a pointer to a type now too.
- Problem #3: Using Pointers to Structs
- Previously, you saw how an array of structs can be passed to functions as a pointer because the
- name of an array acts like a pointer but is still an array. Therefore, we could still use the direct
- operator to access members in the structs in the array. Take the incomplete program given
- below and add the missing code to return the result of raising x to the power of y by accessing
- the values form the ptr to the struct SAMPLE. The function PowerUpWrong is used to
- demonstrate how not to do this. When dealing with a pointer to a struct, you access members
- using the indirect selection operator ‘->’. Look for “/*your code here*/”.
- Skeleton Code missing function code below:
- #include <stdio.h>
- #include <math.h>
- typedef struct{
- int x;
- int y;
- int res;
- } SAMPLE;
- void PowerUp( /* your code here */ );
- void PowerUpWrong(SAMPLE);
- int main(void) {
- SAMPLE sample1 = {2, 5, 0}; //Initializes fields/members
- SAMPLE* samplePtr;
- samplePtr = &sample1; //Create a pointer to the struct
- PowerUpWrong(sample1);
- printf("The incorrect result is %d\n", sample1.res);
- PowerUp(samplePtr);
- printf("%d raised to the power of %d is: %d\n", sample1.x, samplePtr->y, sample1.res);
- //Note the use of both the indirect and direct selection operators
- return 0;
- }
- void PowerUpWrong(SAMPLE ptr){
- int result = 0;
- ptr.res = (int)pow(ptr.x, ptr.y);
- return;
- }
- void PowerUp( /* your code here */ ){
- int result = 0;
- result = (int)pow( /* your code here */ );
- /* your code here */ = result;
- return;
- }
- PROBLEM #4
- Construct a program using typedef. The term scalar is used to denote a quantity having magnitude but
- not any direction (ascending or descending). A vector is like a scalar but has direction. A sorted list of
- numbers could be referred to as a vector. The dot product or scalar product is an algebraic operation
- where two equally sized sequences of numbers are computed to a single number. Your program should
- define a value N with a value of 4. Next, declare a new type of scalar of type double and name it SCALAR.
- Now create a second new type pf SCALAR that is called a VECTOR and give it a size of N. In main, declare
- and define two new VECTORs X and Y. Initialize them with any 4 values. Create a user defined function
- called Dot_Product that takes two VECTORs and returns a SCALAR value back to the caller. In main call
- the new function and print out the computed value. Sample output is shown below
- SAMPLE OUTPUT:
- The vector: 2.000000 5.400000 6.000000 9.000000
- The vector: 4.200000 5.400000 6.700000 7.000000
- The Dot Product of X and Y is: 140.76
Add Comment
Please, Sign In to add comment