| #include <stdio.h>
void GetData(struct Emp);
struct Date { int yy, mm, dd; };
struct Emp { char EmpName[25]; float Salary; struct Date hired; };
struct Dep { struct Emp manager; struct Emp worker[25]; float Profits; };
void main(void) { struct Emp LocalVar = {"Jane Seymour"};
// fill in the myEmp structure with data LocalVar.hired.dd = 12; LocalVar.hired.mm = 10; LocalVar.hired.yy = 2007; LocalVar.Salary = 99999;
// send the structure to PrintData() GetData(LocalVar); }
void GetData(struct Emp myData) { // print all the data from the copy, copied into myData printf("Mrs. %s was hired on %d/%d/%d\nwith $%.2f salary per month.\n", myData.EmpName, myData.hired.dd, myData.hired.mm,myData.hired.yy,myData.Salary); }
|
| #include <stdio.h>
// function prototypes struct Emp GetData(); void PrintData(struct Emp);
// structure definition struct Date { int yy, mm, dd; };
struct Emp { char EmpName[25]; float Salary; struct Date hired; };
struct Dep { struct Emp manager; struct Emp worker[25]; float Profits; };
void main(void) { // declare an array of structure variable struct Emp a[5]; // normal variable, a counter int i; // send the structure to GetData(), to get data from user for(i=0;i<=4;i++) a[i] = GetData(); printf("\n"); // print all the data stored in the structure reversely for(i=4;i>=0;i--) PrintData(a[i]); }
struct Emp GetData() { struct Emp askEmp; // prompt user for inputs and store them in structure askEmp printf("Enter employee name: "); scanf_s(" %s", &askEmp.EmpName, 25); printf("Enter salary: "); scanf_s("%f", &askEmp.Salary, sizeof(float)); printf("Enter hired date (dd) (mm) (yyyy): "); scanf_s("%d%d%d", &askEmp.hired.dd, &askEmp.hired.mm, &askEmp.hired.yy, 1,1,1); // return (pointer to) a structure with the stored data return askEmp; }
void PrintData(struct Emp myData) { // print all the data from the copy, copied into myData printf("Mr. %s was hired on %d/%d/%d with $%.2f per month\n", myData.EmpName, myData.hired.dd, myData.hired.mm,myData.hired.yy,myData.Salary); }
Can you correct the employee's title using if-else statement? |
| #include <stdio.h> #define SIZE 3
// function prototypes struct Emp GetData(); double GetAverage(struct Emp [], int);
// structure definition struct Date { int yy, mm, dd; };
struct Emp { char EmpName[25]; float Salary; struct Date hired; };
struct Dep { struct Emp manager; struct Emp worker[25]; float Profits; };
void main(void) { // declare an array of structure variable struct Emp a[SIZE]; // normal variable, a counter int i; // hold the returned average value double Average; // send the structure to GetData() // to get data from user for(i=0;i<SIZE;i++) a[i] = GetData(); printf("\n"); // get the salary average Average = GetAverage(a,SIZE); printf("The average salary is = %.2f\n",Average); }
struct Emp GetData() { struct Emp askEmp; // prompt user for inputs // and store them in structure askEmp printf("Enter employee name: "); scanf_s(" %s", &askEmp.EmpName, 25); printf("Enter salary: "); scanf_s("%f", &askEmp.Salary, sizeof(float)); printf("Enter hired date (dd) (mm) (yyyy): "); scanf_s("%d%d%d", &askEmp.hired.dd, &askEmp.hired.mm, &askEmp.hired.yy, 1,1,1); // return (pointer to) a structure with the stored data return askEmp; }
double GetAverage(struct Emp myData[], int arrSize) { int i; double sum = 0.00, Avg =0.00;
// sum up all the salary values for(i=0;i<arrSize;i++) sum = sum + myData[i].Salary;
// find the average Avg = sum/arrSize; // return the average return Avg; }
|
More Structure Exercises
|
|
| #include <stdio.h> #include <string.h>
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem Part; // strcpy(Part.Number, "SM0001"); strcpy_s(Part.Number, 10, "SM0001"); Part.Price = 0.35F; Part.Qty = 20; }
|
| struct PartItem Part = {"SMS0001", 0.35, 20}; |
------Output--------- What is the part number? OLS001 The price? 0.80 And the quantity? 30 | #include <stdio.h>
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem OldPart;
printf("What is the part number? "); // gets(OldPart.Number); gets_s(OldPart.Number,10); printf("The price? "); // scanf("%f", &OldPart.Price); scanf_s("%f", &OldPart.Price, sizeof(float)); printf("And the quantity? "); // scanf("%d", &OldPart.Qty); scanf_s("%d", &OldPart.Qty, sizeof(int)); }
|
------Output------- SMS0001 0.35 20 7.00 OLS0001 0,80 30 24.00 | #include <stdio.h>
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem OldPart, Part = {"SMS0001", 0.35f, 20};
printf("What is the part number? "); // gets(OldPart.Number); gets_s(OldPart.Number,10); printf("The price? "); // scanf("%f", &OldPart.Price); scanf_s("%f", &OldPart.Price, sizeof(float)); printf("And the quantity? "); // scanf("%d", &OldPart.Qty); scanf_s("%d", &OldPart.Qty, sizeof(int));
printf("%s\t%.2f\t%d\t%.2f\n", Part.Number, Part.Price, Part.Qty, Part.Price*Part.Qty); printf("%s\t%.2f\t%d\t%.2f\n", OldPart.Number, OldPart.Price, OldPart.Qty, OldPart.Price*OldPart.Qty); }
|
| #include <stdio.h>
void PrintInv(struct PartItem);
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem OldPart, Part = {"SMS0001", 0.35f, 20};
printf("What is the part number? "); // gets(OldPart.Number); gets_s(OldPart.Number,10); printf("The price? "); // scanf("%f", &OldPart.Price); scanf_s("%f", &OldPart.Price, sizeof(float)); printf("And the quantity? "); // scanf("%d", &OldPart.Qty); scanf_s("%d", &OldPart.Qty, sizeof(int));
PrintInv(Part); PrintInv(OldPart); }
void PrintInv(struct PartItem Prt) { printf("%s\t%.2f\t%d\t%.2f\n", Prt.Number, Prt.Price, Prt.Qty, Prt.Price*Prt.Qty); }
|
| #include <stdio.h>
void PrintInv(struct PartItem); float FindInv(struct PartItem);
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem OldPart, Part = {"SMS0001", 0.35f, 20};
printf("What is the part number? "); // gets(OldPart.Number); gets_s(OldPart.Number,10); printf("The price? "); // scanf("%f", &OldPart.Price); scanf_s("%f", &OldPart.Price, sizeof(float)); printf("And the quantity? "); // scanf("%d", &OldPart.Qty); scanf_s("%d", &OldPart.Qty, sizeof(int));
PrintInv(Part); PrintInv(OldPart); }
void PrintInv(struct PartItem Prt) { float Inventory;
Inventory = FindInv(Prt); printf("%s\t%.2f\t%d\t%.2f\n", Prt.Number, Prt.Price, Prt.Qty, Inventory); }
float FindInv(struct PartItem AnotherPrt) { return (AnotherPrt.Price*AnotherPrt.Qty); }
|
| #include <stdio.h>
float PrintInv(struct PartItem); float FindInv(struct PartItem);
// structure definition struct PartItem { char Number[10]; float Price; int Qty; };
void main(void) { struct PartItem OldPart, Part = {"SMS0001", 0.35f, 20}; float Sum = 0.0;
printf("What is the part number? "); // gets(OldPart.Number); gets_s(OldPart.Number,10); printf("The price? "); // scanf("%f", &OldPart.Price); scanf_s("%f", &OldPart.Price, sizeof(float)); printf("And the quantity? "); // scanf("%d", &OldPart.Qty); scanf_s("%d", &OldPart.Qty, sizeof(int));
Sum = PrintInv(Part); Sum = Sum + PrintInv(OldPart);
printf("The total inventory is %.2f\n", Sum); }
float PrintInv(struct PartItem Prt) { float Inventory;
Inventory = FindInv(Prt); printf("%s\t%.2f\t%d\t%.2f\n", Prt.Number, Prt.Price, Prt.Qty, Inventory); return Inventory; }
float FindInv(struct PartItem AnotherPrt) { return (AnotherPrt.Price*AnotherPrt.Qty); }
|
| struct PartItem PartA[3] = { {"SMS0001", 0.35, 20}, {"OLS0001", 0.60, 30}, {"VVU0001", 0.25, 50} }; |
|
|
|
|
|
|
| #include <stdio.h> #include <string.h>
// structure definitions struct PartItem { char Number[10]; float Price; int Qty; };
struct Assembly { char Name[10]; struct PartItem PartA[3]; };
void main(void) { struct Assembly Motor;
// strcpy(Motor.PartA[0].Number, "SMR0001"); strcpy_s(Motor.PartA[0].Number, 10, "SMR0001"); Motor.PartA[0].Price = 3.57f; Motor.PartA[0].Qty = 7;
printf("Enter a part's name, price and quantity\n");
// scanf("%s", &Motor.Name); scanf_s("%s", &Motor.Name, sizeof(Motor.Name)); // scanf("%f", &Motor.PartA[1].Price); scanf_s("%f", &Motor.PartA[1].Price, sizeof(Motor.PartA[1].Price)); // scanf("%d", &Motor.PartA[1].Qty); scanf_s("%d", &Motor.PartA[1].Qty, sizeof(Motor.PartA[1].Qty)); }
|