Struct 1: Struct Semi (String Name Int Age Char Gender )

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

(1) Discuss how each program runs as struct

STRUCT 1
The structure is a collection of parameters of different data types such as int, char, double, and
many more under a single name. You can easily identify a program that follows a struct function
when you can notice a struct data type in the program.
As for the sample program 1, it follows a struct function due to the fact that it used struct
function followed by an identifier (“struct semi”). Then inside the curly braces of the struct
function, there are three data types such as string, int, and char used for the declaration of the
parameters. The parameters are considered as the members of the function. The structure-
function can only hold information that is similar to the used data types. For example, the
string can only be used when the user needs to input a sequence of letters, an int can only be
used when the user needs to input a whole number and char can only be used when the user
needs to input a single character. For a better understanding, here is the portion of the code
that verifies my statement:
struct semi
{
string name;
int age;
char gender;
};

Inside the main function, you can notice that s and s2 are declared by the identifier semi. This
indicates that the program will proceed to the struct function and the three member's data can
be used to let the program enter the user’s name, age, and gender. In continuation, the
following are stated in the code of the program:
s.name="DM";
s.age=19;
s.gender='M';

For the complete access of the three members in struct function, a dot (.) operator must be
used. s.name = “DM”, s.age =19, and s.gender =’M’; indicates that the user should enter his/her
name, age, and gender with this format. It does not necessarily mean that the user should input
the exact letters or numbers stated in the initializer.Since the program used a string data type,
the predefined library/header function such as <string> must be included in the code for the
program to run.
STRUCT 2
As for sample program 2, It is almost similar to the sample program 1 but unlike the first one,
the second program uses a function which is an array. An array can store multiple data at the
same type.

it follows a struct function due to the fact that it used struct function followed by an identifier
(“struct record”). Then inside the curly braces of the struct function, there are three data types
such as string, int, and char used for the declaration of the parameters. The parameters are
considered as the members of the function. Also, one of the string data types uses an array
with a size of 3 (“hobbies 3’]”), therefore the program will allow the user to enter his/her
hobbies thrice. The structure-function can only hold information that is similar to the used data
types. For a better understanding, here is the portion of the code that verifies my statement:
struct record
{
string name;
int age;
string hobbies[3];
char g;
};
Inside the main function, you can notice that an array “r” with a size of 3 is declared by the
identifier record. This indicates that the program will proceed to the struct function and the
four member's data can be used to let the program enter the user’s name, age, gender, and
hobbies three times. This program may allow one to three users since it uses an array with a
size of 3. In continuation, the following are stated in the code of the program: for(int
i=0;i<3;i++)
{
cout <<"Enter name:";
cin >> r[i].name;
cout <<"Enter age:";
cin >> r[i].age;
cout <<"Enter gender:";
cin >> r[i].g;

for(int k=0;k<3;k++)
{
cout<<"Enter hobby:";
cin>>r[i].hobbies[k];
}
}
for(int j=0;j<3;j++)
{
cout << r[j].name<<endl;
cout << r[j].age<<endl;
cout << r[j].g<<endl;
for(int k=0;k<3;k++)
{
cout<< r[j].hobbies[k];
}
}

Multiple for loop statements are used in the program. In the first main for loop statement, a
parameter “i” is declared by integer data type and a condition of i<3 is written inside an open
and close parenthesis. This condition means that the program will allow the user to input three
times. Another condition is i++. This condition means that the program will run until the first
condition “i<3” is satisfied. The first main for loop statement will allow the user to input his/her
name, age, gender three times. Inside the first main for loop, another for loop statement is
used in the main for loop statement. A parameter “k” is declared by integer data type and a
condition of k<3 is written inside an open and close parenthesis. This condition means that the
program will allow the user to input three times. On the other hand, the k++ condition means
that the program will run until the condition, “k<3” is satisfied. This for loop statement will
allow the user to input his/her hobbies three times per batch.
For the second for loop statement, a parameter “j’ is declared by integer data type and has the
same conditions as i and k. For the complete access of the three members in struct function, a
dot (.) operator must be used. Lastly, you can notice in the code that the last output stream
(cout<< r[j].hobbies[k];) does not contain an endl , therefore you must expect that when you
run the program, the hobbies will have no spaces.
(2) Give an example, scenario, or problem where struct can be applied

Like the two sample programs provided above. Structure-function can be used to gather and
store personal information such as name, age, gender, contact number, hobbies, and many
more. This means that it can be used to collect such information that will serve as a material to
create a data list. This will simplify the work of a person.
The sample scenario is the company. A company is made up of several employees and of
course, they must have a list of their employees and their data. Another application is that the
company can also use the structure-function for giving their salary and for their inventory.
These scenarios are possible with the use of appropriate data types such as string or char for
the name, a double type for the rates per day, and an integer for the days, age, etc.
(3) Show screenshots of the two (2) running programs

STRUCT 1
STRUCT 2

You might also like