#include<stdio.h>
void main() { struct Song { char Name[25]; float Length; };
struct Song Title1 = {"Marvelous Grace", 2.50}, Title2 = {"I Surrender All", 3.0}, Title3;
Title3 = Title1; Title1 = Title2; Title2 = Title3;
printf("1: Title - %s, Length - %.2f\n", Title1.Name, Title1.Length); printf("2: Title - %s, Length - %.2f\n", Title2.Name, Title2.Length); }
|
|
#include<stdio.h> #include<string.h>
// start of structure definition struct Song { char Name[25]; float Length; }; // end of structure definition
// function prototype, receives and returns structure data type struct Song ZeroOut(struct Song);
void main() { struct Song Title1 = {"White room", 2.50}, Title2; Title2 = ZeroOut(Title1); printf("1: %s, %.2f\n", Title1.Name, Title1.Length); printf("2: %s, %.2f\n", Title2.Name, Title2.Length); }
// beginning of function definition struct Song ZeroOut(struct Song x) { struct Song y = {"Have Mercy On Me", 3.20F}; strcpy_s(x.Name, 25, " "); x.Length = 0; return y; } // end of function definition
|
|
#include<stdio.h> #include<string.h>
struct Song { char Name[25]; float Length; };
void main() { struct Song Title[3] = {{"Lone Prairie", 2.50F}, {"Merry Christmas", 3.35F}};
int i;
puts("Enter a new song:"); gets_s(Title[2].Name, 25); puts("Enter its time:"); scanf_s("%f", &Title[2].Length, 4);
for(i = 0; i <= 2; ++i) printf("%d: %s, %.2f\n", i + 1, Title[i].Name, Title[i].Length); }
|
----------------------------------------------------------------------
|
#include<stdio.h>
struct Song { char Name[25]; char Singer[3][25]; float Length; };
void main(void) { int i; struct Song Title1 = {"Seek and destroy", "Run for your life", "Beautiful Jasmine","Make my day", 2.50}, Title2;
puts("Enter a new song:"); gets_s(Title2.Name, 25); puts("Enter its 3 singers"); scanf_s("%s", &Title2.Singer[0], 25); scanf_s("%s", &Title2.Singer[1], 25); scanf_s("%s", &Title2.Singer[2], 25); printf("%s\t%s\n", Title1.Name, Title2.Name); printf("\n"); for(i = 0; i <= 2; ++i) printf("%s, %s\n", Title1.Singer[i], Title2.Singer[i]); }
|
|
More Structure Questions
For the following questions, use the definitions of the given structure.
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; };
| |
| struct Date { int yy, mm, dd; };
void main(void) { struct Date Date1;
Date1.dd = 25; Date1.mm = 2; Date1.yy = 1957;
printf("The date = %d, %d, %d\n", Date1.mm, Date1.dd, Date1.yy); }
|
| #include <stdio.h>
struct Date { int yy, mm, dd; };
struct Emp { char EmpName[25]; float Salary; struct Date hired; };
void main(void) { struct Emp Person1 = {"Roger"};
Person1.Salary = 50000; Person1.hired.dd = 10; Person1.hired.yy = 2001;
printf("Hired Date = %d, %d, %d\n", Person1.hired.mm, Person1.hired.dd, Person1.hired.yy); printf("Employee Name = %s\n", Person1.EmpName); printf("Salary = $%.2f\n", Person1.Salary); }
|
| #include <stdio.h>
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) { // note the order of the arrangement // {EmpName,Salary,{yy,mm,dd}},{{EmpName[0], Salary,{yy,mm,dd}}, //{EmpName[1],Salary,{yy,mm,dd}},..., {EmpName[24],Salary,{yy,mm,dd}}}, //{Profits}} struct Dep Toys = {{"Roger"}, {{"Mohave"},{"Hunter"}}};
Toys.Profits = 80000; Toys.worker[0].hired.mm = 4; Toys.worker[0].hired.dd = 20; Toys.worker[0].hired.yy = 2002; Toys.worker[0].Salary = 10000;
Toys.worker[1].hired.mm = 6; Toys.worker[1].hired.dd = 12; Toys.worker[1].hired.yy = 2000; Toys.worker[1].Salary = 8000;
printf("%s is a manager\n", Toys.manager.EmpName); printf("Company profit is %.2f\n", Toys.Profits); printf("Hired Date for %s is %d, %d, %d\n", Toys.worker[0].EmpName, Toys.worker[0].hired.mm = 4, Toys.worker[0].hired.dd = 20, Toys.worker[0].hired.yy = 2002); printf("Salary = $%.2f\n", Toys.worker[0].Salary); printf("Hired Date for %s is %d, %d, %d\n", Toys.worker[1].EmpName, Toys.worker[1].hired.mm = 6, Toys.worker[1].hired.dd = 12, Toys.worker[1].hired.yy = 2000); printf("Salary = $%.2f\n", Toys.worker[1].Salary); }
|
| See #1 |
See #2 | |
| |
| #include <stdio.h>
void PrintEmp(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) { // note the order of the arrangement // {EmpName,Salary,{yy,mm,dd}} // Let make it something like this for easiness // struct Emp Names0 = {"Roger"}; // struct Emp Names1 = {"Mohave", 10000, {4, 20, 2002}}; // struct Emp Names2 = {"Hunter", 8000, {6,12,2000}}; // Then convert it to an array struct Emp Names[3] = {{"Roger"}, {"Mohave", 10000, {2002, 4, 20}}, {"Hunter", 8000, {2000,6,12}}}; struct Dep Toys = {{"Roger"}, {{"Mohave"},{"Hunter"}}};
PrintEmp(Names[0]); printf("\n"); PrintEmp(Names[1]); printf("\n"); PrintEmp(Names[2]); printf("\n");
Toys.Profits = 80000; Toys.worker[0].hired.mm = 4; Toys.worker[0].hired.dd = 20; Toys.worker[0].hired.yy = 2002; Toys.worker[0].Salary = 10000;
Toys.worker[1].hired.mm = 6; Toys.worker[1].hired.dd = 12; Toys.worker[1].hired.yy = 2000; Toys.worker[1].Salary = 8000;
printf("%s is a manager\n", Toys.manager.EmpName); printf("Company profit is %.2f\n", Toys.Profits); printf("Hired Date for %s is %d, %d, %d\n", Toys.worker[0].EmpName, Toys.worker[0].hired.mm = 4, Toys.worker[0].hired.dd = 20, Toys.worker[0].hired.yy = 2002); printf("Salary = $%.2f\n", Toys.worker[0].Salary); printf("Hired Date for %s is %d, %d, %d\n", Toys.worker[1].EmpName, Toys.worker[1].hired.mm = 6, Toys.worker[1].hired.dd = 12, Toys.worker[1].hired.yy = 2000); printf("Salary = $%.2f\n", Toys.worker[1].Salary); }
void PrintEmp(struct Emp Names) { printf("Employee name is %s\n", Names.EmpName); printf("Date Hired is %d, %d, %d\n", Names.hired.mm, Names.hired.dd, Names.hired.yy); printf("Salary is $%.2f\n", Names.Salary); }
|
| |
Do A Structure Programming
Use the same structure definitions as given in the question section above.
| #include <stdio.h>
struct Date GetDate();
struct Date { int yy, mm, dd; };
void main(void) { struct Date newDate;
newDate = GetDate(); printf("Year: %d, Month: %d and Day: %d\n", newDate.yy, newDate.mm, newDate.dd); }
struct Date GetDate() { struct Date myDate; puts("Enter year (yyyy), month (mm) and day (dd):"); scanf_s("%d%d%d", &myDate.yy, &myDate.mm, &myDate.dd, 4, 2, 2);
return myDate; }
|
| #include <stdio.h>
struct Emp GetData();
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 myEmp;
myEmp = GetData(); printf("\nEmployee name is %s.\nDate Hired is: %d/%d/%d.\nSalary is $%.2f\n", myEmp.EmpName, myEmp.hired.dd, myEmp.hired.mm, myEmp.hired.yy, myEmp.Salary); }
struct Emp GetData() { struct Emp askEmp;
// prompt user for inputs and store them in structure askEmp puts("Enter employee name:"); gets_s(askEmp.EmpName, 25); puts("Enter salary: "); scanf_s("%f", &askEmp.Salary, sizeof(float)); puts("Enter hired date (yyyy) (mm) (dd):"); scanf_s("%d%d%d", &askEmp.hired.yy, &askEmp.hired.mm, &askEmp.hired.dd, 1,1,1); // return a structure with the stored data return askEmp; }
|
| #include <stdio.h>
void PrintData(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 myEmp = {"Jodie Foster"};
// fill in the myEmp structure with data myEmp.hired.dd = 27; myEmp.hired.mm = 11; myEmp.hired.yy = 2008; myEmp.Salary = 77123;
// send the structure to PrintData() PrintData(myEmp); }
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);
|