Write code to accomplish each of the following: Using typedef, declare a structu
ID: 3768986 • Letter: W
Question
Write code to accomplish each of the following: Using typedef, declare a structure, Automobile, with the following members: model - string of 25 characters maximum year - integer mpg - integer Declare variable car to be of type Automobile. Assign the information below to the appropriate fields of variable car: 2012 Infiniti gets 25 mpg. Declare an array, vehicle, of 500 Automobiles. Assign variable car to the 5th element of the array. Output the Automobile information located in the 5^th array location. Show the output. Declare variable p to be a pointer to an Automobile structure. Assign to variable p the address of the 5^th array location. Using variable p, output the Automobile information located in the 5^th array location. Show the output. Declare variable q to be a pointer to an Automobile structure. Assign to variable q the address of the first array location. Using variable q, output the Automobile information located in the 5^th array location. Show the output.Explanation / Answer
#include <stdio.h>
typedef struct Automobile
{
char model[25];
int year;
int mpg;
}Automobile;
void display(Automobile a)
{
printf("%i %s gets %i mpg. ",a.year, a.model, a.mpg);
}
void displayWithPointer(Automobile *a)
{
printf("%i %s gets %i mpg. ",a->year, a->model, a->mpg);
}
int main()
{
Automobile car = {"Infiniti", 2012, 25};
Automobile vehicle[500];
vehicle[4] = car;
display(vehicle[4]);
Automobile *p;
p = &vehicle[4];
printf("This time using pointer: ");
displayWithPointer(p);
Automobile *q;
q = vehicle;
printf("%i %s gets %i mpg. ",q+4->year, q+4->model, q+4->mpg);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.