Structs C++ - Reading Assignments

#21

Why do we need structs? structs allows to storage differents types of veriables
What are members in a struct? The variables declared into the struct
How do you access a member inside a struct? Employee joe; // create an Employee struct for Joe
What is a nested struct? Structs can contain other struct

struct Employee
{
short id;
int age;
double wage;
};

struct Company
{
Employee CEO; // Employee is a struct within the Company struct
int numberOfEmployees;
};

#22

1. Why do we need structs?
To group together a number of related variables.

2. What are members in a struct?
Variables of different data types.

3. How do you access a member inside a struct?
Using period which is called member selection operator.

4. What is a nested struct?
Struct within a Struct.

#23
1. Structs allow us to group variables of mixed data types into one unit.
2. Members of a struct are the variables of which are inside of the struct.
3. We use the member selector operator “.” Ex. joe.age.
4. A nested struct is simply a struct within a struct.

#24

1. Why do we need structs? A struct allows us to group variables of mixed data together into a single unit, which is useful when we need more than one variable to represent an object.

2. What are members in a struct? Variables that are part of a struct are called members.

3. How do you access a member inside a struct? To access individual members, we use the member selection operator, which is a period.

4. What is a nested struct? A nested struct is a struct within another struct.

#25
1. There are many instances in programming where we need more than one variable in order to represent an object.

2. The variables that are part of the struct are called members.

3. We use the member selection operator (which is a period).

4. Structs containing other structs.

#26
1. We need it for aggregated data type. We need it to group multiple individual variables together
2. Variables that are part of the struct are called members
3. In order to access the individual members, we use the member selection operator.
4. Structs that contain other structs

#27

1. Why do we structs?
Because is more efficient passing groups of multiple variables together to be processed. This is named an aggregate data type .
2. What are members in a struct?
They are the variables grouped in the struct.
3. How do you access a member inside a struct?
With the name of the struct definition and a point followed by the member name.
4. What is a nested struct?
It is a struct which contains another struct as member.
Quiz
1.

``````#include <iostream>

using namespace std;

{
double earns;
};

main()
{
double earnsMonth;

mySite.earns = 4000;

cout << "You have earned : " << earnsMonth << "\n";

return 0;
}

{
cout << "Number of ads : " << adSite.ads << "\n";
cout << "Persentage of clicked ads : " << adSite.persAds * 100 << "\n";
cout << "Earnings by adverstising : " << adSite.ads << "\n";

}
``````
``````#include <iostream>

using namespace std;

struct Fraction
{
int numerator;
int denominator;
};

Fraction multiply (const Fraction& n, const Fraction& d);

main()
{
Fraction a, b, result;

a.numerator = 0;
b.numerator = 0;
a.denominator = 1;
b.denominator = 1;

cout << "Type A fraction numerator [space] denominator : ";
cin >> a.numerator >> a.denominator;

cout << "Type B fraction numerator [space] denominator : ";
cin >> b.numerator >> b.denominator;

result = multiply(a,b);

cout << "The result is : " << result.numerator << "/" << result.denominator << "\n";

return 0;
}

Fraction multiply (const Fraction& a, const Fraction& b)
{
Fraction result;

result.numerator = a.numerator * b.numerator;
result.denominator = a.denominator * b.denominator;

return result;
}``````

#28

Structs C++

1. If you want to organize multiple variables (of multiple datatypes) in order to represent an object (user defined variable) eg. a person.

2. Members are the individual variables declared in a struct. (also called fields)

3. we use the member selection operator, a period (dot) like joe.age = 16; when defining a variable to the user defined struct, it refers to the entire struct.

4. Structs that contain other structs.

#29
1. Why do we need structs?

Structs allow us to group variables of different data types in a single object.

1. What are members in a struct?

The members of a struct are the variables contained in it.

1. How do you access a member inside a struct?

The dot operator allows to access a member inside of a struct. Assuming we declare a struct:

MyStruct {
int member1;
int member2;
double member3;
};

Then for MyStruct thisStruct we would access the third member as thisStruct.member3.

1. What is a nested struct?

A nested struct is a struct used as a member of another struct.

#30
1. A struct allows us to group variables of mixed data types together into a single unit.
2. Members in a struct are variables inside of that struct.
3. We access member inside of a struct with member selection operator (.)
4. A nested struct is a struct within another struct.

#31
1. In case we need multiple variables to represent an object, we need structs in order to easily group these variables (which also can be of different data types).
2. Members in a struct are the variables that are part of the struct.
3. To access a member inside a struct you use the member selection operator.
4. A nested struct is a struct within a struct.

#32
1. Structs allow us to store data in an aggregate manner. This makes it easier to manage data about a group, rather than managing it’s individual parts.
2. Members are the data contents of structs.
3. Using the dot operator, i.e. `structName.memberName`
4. A struct in a struct is known as a nested struct

#33
1. We need them so that we can work with more than one variable in an object.
2. Members are variables stored in struct.
3. We add a dot at the end of sturct object and a name of a variable in struct.
4. It’s a structure that is defined inside another sturcture.

#34

1. Why do we need structs?
We need structs to store multiple data type
2. What are members in a struct?
Variables inside the structure are called the members
3. How do you access a member inside a struct?
By using member selector operator
4. What is a nested struct?
Structs within a struct are nested struct

#35

Why do we need structs?
allows us to group variables of mixed data types together into a single unit.
What are members in a struct?
variables that are part of the struct are called members (or fields)
How do you access a member inside a struct?
structure_name.member_name
What is a nested struct?
Structs can contain other structs

#36

1. Why do we need structs?
To group more than a variable together in order to represent an object.

2. What are members in a struct?
Fields (the variables).

3. How do you access a member inside a struct?
Use the member selection operator (which is a period).

4. What is a nested struct?
A struct can contain other structs.

#37

Why do we need structs?
it is much easier to use structs to deal with situations with multiple and repetitive variables

What are members in a struct?
variables in a struct

How do you access a member inside a struct?
structInstantiation.member

What is a nested struct?
when a struct is a member of another struct

Quiz

1) You are running a website, and you are trying to keep track of how much money you make per day from advertising. Declare an advertising struct that keeps track of how many ads you’ve shown to readers, what percentage of ads were clicked on by users, and how much you earned on average from each ad that was clicked. Read in values for each of these fields from the user. Pass the advertising struct to a function that prints each of the values, and then calculates how much you made for that day (multiply all 3 fields together).

``````#include <iostream>
#include <iomanip>

using namespace std;

{
};

cout << "Ads shown: " << month.adsShown << endl;
cout << "Percentage of ads clicked: " << month.adsClickedPercentage << endl;
cout << "Average earnings per ad: " << month.averageEarningPerAd << endl;
cout << "Profit " << month.averageEarningPerAd * month.adsClickedPercentage * month.adsShown << endl;
}

int main()
{

cout << "Enter ads shown." << endl;

cout << "Enter percentage of ads clicked." << endl;

cout << "Enter average earnings per ad." << endl;

}
``````

2) Create a struct to hold a fraction. The struct should have an integer numerator and an integer denominator member. Declare 2 fraction variables and read them in from the user. Write a function called multiply that takes both fractions, multiplies them together, and prints the result out as a decimal number. You do not need to reduce the fraction to its lowest terms.

``````#include <iostream>
#include <iomanip>

using namespace std;

struct Fraction
{
int denominator=1;
int numerator=1;
};

void multiply(Fraction fraction1, Fraction fraction2) {

double total = 0;
double first = (static_cast<double>(fraction1.numerator)/static_cast<double>(fraction1.denominator));
double second = (static_cast<double>(fraction2.numerator)/static_cast<double>(fraction2.denominator));
total = first*second;
cout << "Fraction1 * fraction2 = " << total << endl;
}

int main()
{
Fraction one;

cout << "Enter numerator for fraction1" << endl;
cin >> one.numerator;

cout << "Enter denominator for fraction1" << endl;
cin >> one.denominator;

Fraction two;

cout << "Enter numerator for fraction2" << endl;
cin >> two.numerator;

cout << "Enter denominator for fraction2" << endl;
cin >> two.denominator;

multiply(one, two);
}``````

#38
1. ``````Why do we need structs?
``````

To be able to assign multiple variables to a single variable

1. ``````What are members in a struct?
``````

the variables within the struct

1. ``````How do you access a member inside a struct?
``````

struct.variable - using the dot(.)

1. ``````What is a nested struct?
``````

when you have a struct within another stuct

Problems

#include

using namespace std;

double clickPercent;
double earnPerClick;
};

// fraction stuct
struct Fraction{
int numerator;
int denominator;
};

// multiply the values of the Advertising struct
x.clickPercent *
x.earnPerClick;
}

double multiply(Fraction x, Fraction y){
// we have to use a static cast because the values of
// the fraction are using an int but we want a double (decimal) in return
double z = static_cast(x.numerator * y.numerator) / (x.denominator * y.denominator);
return z;
}

int main()
{

``````Advertising ad;

cout << "How many ads did you show today?"<< endl;
cout << "What is the average click percentage? in decimal from 0.0 to 1.0" << endl;
cout << "What is the average earn per click? in decimal ammount" << endl;
cout << "computing..." << endl;
cout << "Earnings for the day is equal to \$" << dailyReturn(ad) << endl;

// fraction problem
Fraction fracX;
Fraction fracY;

// get the data for the first fraction
cout << "Please provide the numerator for the first fraction" << endl;
cin >> fracX.numerator;
cout << "Please provide the denominator for the first fraction" << endl;
cin >> fracX.denominator;

// get the data for the second fraction
cout << "Please provide the numerator for the second fraction" << endl;
cin >> fracY.numerator;
cout << "Please provide the denominator for the second fraction" << endl;
cin >> fracY.denominator;

cout << "Let's see what the multiplication of these fractions is" << endl;
cout << "computing...." << endl;

cout << "fraction 1 multiplied by fraction 2 is equal to: " << multiply(fracX, fracY) << endl;

return 0;
``````

}

#39

Why do we need structs?
To group some variables if they belong to the same “subject” so we can prevent to declare lots of variables
What are members in a struct?
Members inside a struct are the “fields”. Variables that contain more details about the defined struct
How do you access a member inside a struct?
by using the referencename of the struct and the variable inside the strut like this “.”
What is a nested struct?
A struct withing a struct

#40

1. Why do we need structs?
To store multiple variables in one object.
2. What are members in a struct?
They are variables that the struct consists of.
3. How do you access a member inside a struct?
with a . example: struct.member
4. What is a nested struct?
Is a struct inside another struct - so one struct is a member of another.