Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Structs and Enums Struct variables Create a header file capacitor.h Protect yo

ID: 3694419 • Letter: C

Question

C Structs and Enums Struct variables Create a header file capacitor.h Protect your header using #indef directive Define a struct with tag Capacitor that holds the following information about a capacitor: Model number (e.g. 11-123U), capacitance (e.g. 1000 uf), voltage (e.g. 2.5 V), cost ($6.50) Create your main program file capacitorslnfo.c that uses the header file you just declared Declare two variables of type struct Capacitor and populate them with the following values: First variable: model is 11-123U, capacitance is 100, voltage is 25 and cost is $6.00 Second variable: model is 65T91a, capacitance is 22000, voltage is 20 and cost is $25.00 Print the the model number of the first capacitor and the voltage of the second Create a makefile to build your program. Make sure it compiles and runs without errors. Remove your print statements. Struct Array Add a third Capacitor variable and initialize it from values entered by the user. Use fgets when reading the model number to make sure you don't exceed the size of your model number array Print to the standard output the members of the third capacitor and to make sure the capacitor is populated correctly. Declare an array of Capacitors of size 4 and copy the three capacitors you created to the array Populate the fourth element of the array from user input. Manipulation with Functions Add to capacitor.h the prototype of function displayCapacitorinfo that takes a capacitor as input parameter and prints its details in the following format Capacitor 11-123U Capacitance 100 uF *Voltage: 25 V * Cost: $25.00 Add the function definition in a corresponding C file capacitor.c. Call your function in the main file on all

Explanation / Answer

capacitor.h

#ifndef HEADER_GUARD
#define HEADER_GUARD2

void displayCapacitorInfo(struct capacitor capacitors);

struct capacitor
{
char modelnumber[20];
int capacitance;
float voltage;
float cost;
};

void displayCapacitorInfo(struct capacitor capacitors)

{

printf("Model Number:%s",capacitors.modelnumber);

printf("Capacitance:%d",capacitors.capacitance);

printf("Voltage :%f",capacitors.voltage);

printf("Cost:%f",capacitor.cost);

}

#endif

capacitorsinfo.c

#include<stdio.h>
#include "capacitor.h"

void displayCapacitorInfo(struct capacitor capacitors);

int main()
{
struct capacitor capacitor1;
struct capacitor capacitor2;
capacitor1.modelnumber="11-123U";
capacitor1.capacitance=1000;
capacitor1.voltage=2.5;
capacitor1.cost=6.5;
  
capacitor2.modelnumber="65T91a";
capacitor2.capacitance=22000;
capacitor2.voltage=20;
capacitor2.cost=25.0;
  
printf("Model Number of capacitor 1: %s",capacitor1.modelnumber);
printf("Voltage of second capasitor :%f",capacitor2.voltage);
  
struct capacitor capacitor3;

printf("Enter capacitor model number: ");

scanf("%s",&capacitor3.modelnumber);

printf("Enter capacitor capacitance");

scanf("%d",&capacitor3.capacitance);

printf("Enter capacitor volatage");

scanf("%f",&capacitor3.voltage);

printf("Enter capacitor cost");

scanf("%f",&capacitor3.cost);

struct capacitor caparray[4];

caparray[0]=capacitor1;

caparray[1]=capacitor2;

caparray[2]=capacitor3;

printf("Enter capacitor model number: ");

scanf("%s",&caparray[3].modelnumber);

printf("Enter capacitor capacitance");

scanf("%d",&caparray[3].capacitance);

printf("Enter capacitor volatage");

scanf("%f",&caparray[3].voltage);

printf("Enter capacitor cost");

scanf("%f",&caparray[3].cost);

return 0;

}

Using typedef:

typedef struct capacitor

{

// same declarations

} capacitors;

// Accessing values is same as struct data type