Question

Structures Homework 1. Write code to accomplish each of the following: a) Using t ypedef, declare a structure, Automobile, wi

in C
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Sample C program that set the values for the questions from a to l.
*/
#include<stdio.h>
#include<conio.h>
#include<string>
//a) Automobile strcuture
typedef struct
{
   char model[25];
   int year;
   int mpg;
}Automobile;

int main()
{
   //b)declare car variable of Automobile
   Automobile car;
  
   //c)Assign values
   strcpy(car.model, "Infinity");
   car.year=2012;
   car.mpg=25;

   //d) declare array vehicle of 500 Automobile
   Automobile vehicle[500];

   //e)assign car variable to 5 the element in vehicle
   vehicle[4]=car;

   //f) output vechicle at 5 the location
   printf("Model : %s\n",vehicle[4].model);
   printf("year : %d\n",vehicle[4].year);
   printf("Mpg : %d\n",vehicle[4].mpg);

   //g) Declare pointer p of the 5the array location
   Automobile *p;

   //h) assign 5th element to p
   p=&vehicle[4];

   //i)Output values in pointer, p
   printf("Model : %s\n",p->model);
   printf("year : %d\n",p->year);
   printf("Mpg : %d\n",p->mpg);

   //j) Declare pointer p of the 5the array location
   Automobile *q;

   //k) Assigh to variabel,q the address of fifth array location
   /*
   Note : Since at first location is not assigned any values of vehicle array,
   we cannot print the values of the pointer,q in the l) question part
   That is why we set 5th location address to the q pointer.
   */
   q=&vehicle[4];

   //l) Output the values at 5th location
   printf("Model : %s\n",q->model);
   printf("year : %d\n",q->year);
   printf("Mpg : %d\n",q->mpg);

   getch();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
in C Structures Homework 1. Write code to accomplish each of the following: a) Using t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using visual studio 2) For each of the following, write C++ statements that perform the specified...

    using visual studio 2) For each of the following, write C++ statements that perform the specified task.. (Ref: classwork named pointer2) a) Declare a built-in array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. b) Declare a pointer vPtr that points to a variable of type unsigned int. c) Write two separate statements that assign the starting address of array values to pointer variable vPtr. d) Use...

  • Using Structures in C language programming

    Write code to accomplish each of the following:Define a structure called Dove containing int variable dovNo and char array dovArr with values that may be as long as 25 characters (including the terminating null character).Define dove to be a synonym for the type struct Dove.Use Dove to declare variable a to be of type struct Dove, array b[ 10 ] to be of type struct Dove and variable ptr to be of type pointer to struct Dove.Read a dovNo and a dovArr from the keyboard into the individual members of variable a.Assign the member values of variable a to element 3 of array b.Assign the address...

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Pointer tasks: ( In C++ ) 1. Write a program to store the address of a...

    Pointer tasks: ( In C++ ) 1. Write a program to store the address of a character variable and a float variable. • Print the address of both variables. • Print the values of variable using dereference operator. • Add 2 in float value using pointer. • Print address of character variable using reference operator. 2. Write a program that takes 5 elements in an array and print them in reverse order using pointers. 3. Write to program that takes...

  • please answer all the questions. 2. (35 pts, 5 each) Consider the following array a. Write...

    please answer all the questions. 2. (35 pts, 5 each) Consider the following array a. Write single-line statements to do the following double a[8] {0.0, = 10.0, 20.0, 30.0, 40.0), *p , ,g: Set the pointer p to point to the first element of the array - I ?-K-U) Set pointer q to point to the element with value 20.0 in the array as afn] with the correct n value. Use p above to triple the value stored in a[1]...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • I NEED HELP WITH C PROGRAMMING. Answer each of the following. Assume that unsigned integers are...

    I NEED HELP WITH C PROGRAMMING. Answer each of the following. Assume that unsigned integers are stored in 4 bytes and that the starting address of the array is at location 200 600 in memory. i)What address is referenced by vPtr + 5? What value is stored at that location? j) Assuming vPtr points to values[6] , what address is referenced by vPtr -= 4? What value is stored at that location? Also ..... For each of the following, write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT