Question

In: Computer Science

Create a program that uses typedef to declare a new type vec2df which is a fixed...

Create a program that uses typedef to declare a new type vec2df which is a fixed double array of size 2. Next, create another type using typedef to declare a struct which has a fixed double array of size 2. Name this type vec2d. In main, declare two variables which use the types created above. Using stdlib.h, print the size of each variable on a new line and observe the similarity or difference. Finally, create two functions which take as input the types declared above as a single argument. For example, the first function takes vec2df as an argument and the second takes vec2d. In each function, print the size of the argument. Observe the difference in size between each instance. Save your code as prob5.c.

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>

typedef double vec2df[2];
typedef struct{double x[2];} vec2d;

void fun1(vec2df v1){
    printf("sizeof of v1: %lu\n", (int) sizeof(v1)/ sizeof(v1[0]));   //  8/8  = 2

}
void fun2(vec2d  v2){
    printf("sizeof of v2: %lu\n", (int) sizeof(v2)/ sizeof(v2.x[0]));   //  16/8  = 2
}
int main() {
    vec2df v1;
    vec2d  v2;

    printf("Size Inside main\n");
    printf("sizeof of v1: %lu\n", (int) sizeof(v1)/ sizeof(v1[0]));   //  16/8  = 2
    printf("sizeof of v2: %lu\n", (int) sizeof(v2)/ sizeof(v2.x[0]));   //  16/8  = 2

    printf("\nSize when passed as argument to external function \n");
    fun1(v1);//8
    fun2(v2);//16

    return 0;
}

/*NOTE :
 * We can use both the method to declare array using typedef
 * typedef double vec2df[2];
 *              :However, this is probably a very bad idea, because the resulting type is an array type,
 *               but users of it won't see that it's an array type. If used as a function argument,
 *               it will be passed by reference, not by value, and the sizeof for it will then be wrong
 *
 * typedef struct{double x[2];} vec2d;
 *              : This is a better solution as it use pass by value.
 *
 * That's the reason why we see different size of the same type when used within the the scope of its declaration
 * as compared to when we pass it as an argument.
 */

Related Solutions

declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
Create a program (or set of programs) which accomplish the following for each complex data type...
Create a program (or set of programs) which accomplish the following for each complex data type (list,tuple,set,frozenset, dictionary): create the item with at least 4 elements Append an element Remove an element Insert an element in the middle somewhere Append another array of the same data type Append another array of a different data type (for example, if you have a dictionary, append a set or tuples And do the following: Output the results after each step. Report and explain...
Create “New Class…” named Book to store the details of a book Declare 3 fields for...
Create “New Class…” named Book to store the details of a book Declare 3 fields for the details of the book: field named author of type String field named title of type String field named callNumber of type String Add overloaded constructors with the following headers and make sure each constructor has only 1 statement in the body that makes an internal method call to setBookDetails: public Book(String author, String title, String callNumber) public Book(String author, String title) HINT: Initialize...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the array.   Here are the requirements: Write a program that contains an enumerated data type named Letters.    In the declaration, include the following three enumerators: ALPHA, BETA, DELTA. Then, create an array of integers three elements long. The array should be initialized to 0 using a 1-element initialization list. Instead of using integers as subscripts, use the enumerators from your enumerated data type to assign...
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT