Structs C++ - Reading Assignments

  1. Because it results easier to group an object and all its properties in a struct rather than declaring an independent variable for each characteristic.
  2. All the variables that represent something inside that struct.
  3. Like this: struct.characteristic
  4. A struct inside another struct.

Why do we need structs?
to represent an object with many variables;

What are members in a struct?
variable

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

What is a nested struct?
struck in another struct

1.Why do we need structs?
We need structs to group variables of mixed data types together into a single unit.

2.What are members in a struct?
Members are variables that are part of the struct

3.How do you access a member inside a struct?
we use the member selection operator which is a period e.g
Employee joe;
joe.id = 14;
joe.age = 32;
joe.wage = 24.15;

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

  1. We need structs to aggregate individual information into groups.
  2. Members contain the individual information.
  3. To acces a member you use the “.” member selection operator.
  4. A nested struct is a struct defined within in a struct.

1.Why do we need structs?
We need structs in order to create “objects”, or group together different data variables into a group. In a way, structs in C++ somewhat a hybrid version of class and object (perhaps closer to class) in JavaScript. This way, we can create multiple groups that possess similar property to one another.

2.What are members in a struct?
Members are properties that the struct possesses. Each memory can be a different data variable type.

3.How do you access a member inside a struct?
We can access a member inside a struct, much like called a property of an object in JavaScript. Here is an example
MyStruct.member1

4.What is a nested struct?
A nested struct is a struct that, within it, contains another struct as one of the member values.

This was my answer for the second question of the quiz, which I found slightly more challenging as compared to the first one. Still learning how to use the static_cast function, which is not included in my code.

struct Fractions{
int numerator;
int denominator;
};

int printFraction(Fractions frac1, Fractions frac2){
cout<< "The combination is: " << frac1.numerator * frac2.numerator << "/" << frac1.denominator * frac2.denominator<<endl;
}

int main()
{
Fractions frac1;
cout << "Enter fraction 1 numerator" << endl;
cin >> frac1.numerator;
cout<< "Enter fraction 1 denominator" << endl;
cin >> frac1.denominator;

Fractions frac2;
cout << "Enter fraction 2 numerator" << endl;
cin >> frac2.numerator;
cout<< "Enter fraction 2 denominator" << endl;
cin >> frac2.denominator;

printFraction(frac1, frac2);
}
  1. A struct (short for structure) allows us to group variables of mixed data types together into a single unit. This is useful ad convenient.

  2. The variables that are part of the struct are called members (or fields).

3, In order to access the individual members, we use the member selection operator (which is a period).

  1. A struct containing another struct within itself.

Structs C++ Reading Assignment

1. Functions can only return 1 variable so if you want to return more then one variable you will need to use a struct data type. struct also allow you to group variables together.
2. Struct members are the variables that make up the struct group; the variables between { }.
3. To access the structs members you use . operator. eg StructName.member.
4. A nested struct is a struct definition that has another struct as a member variable.

Why do we need structs?
It 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.

How do you access a member inside a struct?
With the use of the member selection operator(which is a period).

What is a nested struct?
Structs can contain other structs.

1 It allows us to group variables together into a single unit (similar to JavaScript objects).

2 Individual variables inside the struct.

3 Similar to Javascript object, with a dot and the member name

struct.member

4 Structs inside of other structs. If we wanted to access a lower inside of another struct, we use member selection operator twice (the dot)

myCompany.Janitor.wage;

  1. Thanks to structs we can create single unit out of variables with different data types.
  2. Members are variables, which are part of the struct.
  3. The Member selection operator is used- nameOfStruct.membersName
  4. Struct can be part of another struct (nested inside).

Why do we need structs?
We need structs to be able to store several variables in the same place, e.g., to represent an object

What are members in a struct?
Members are the several variables of a struct

How do you access a member inside a struct?
we access a member through its name, plus a period (.), plus the member’s name

What is a nested struct?
A nested struct is a struct that is simultaneously a member of its outer struct

  • Structs are needed when grouping informations about a common subject and storing it in one variable.
  • Members in a struct aggregate are an other name for the variables declared inside it.
  • Members of a struct can be accessed by using a dot(.) after the name of the struct and its variable.
  • Nesting a struct is the action of using a struct within an other struct to extend its members.
  1. Where we need more than one variable to represent an object.

  2. Variables that are part of the struct.

  3. Use the member selection operator, e.g joe.id

  4. Structs that contain other structs.

Why do we need structs?

Structs are an aggregate data type that enables us to group multiple individual variables together. We often need many variables to represent an object and structs give us the ability to associate large numbers of individual variables relating to an object into the same data structure.

It should also be noted that in the absence of an aggregate data type such as a struct if we wanted to pass multiple variables to a function we would need to individually pass each variable to the function however when these variables are aggregated into a struct then we just pass the struct to the function.

What are members in a struct?

The members in a struct consist of the variables that make up the struct.

How do you access a member inside a struct?

In order to access the member inside a struct we specify the struct name followed by period (which is the member selection operator) followed by the member variable. In the case of nested structs, we specify each struct name (followed by a period) in the path to the member variable.

What is a nested struct?

A nested struct refers to a struct that contains one or more other structs.

At the bottom of the article you’ll see a quiz - make sure to do the quiz!

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>

using namespace std;

struct Advertisement

{

int numberOfAds;

double adClickPercentage;

double avgRevenuePerAd;

};

double dayRevenue (Advertisement X) {

double dailyEarnings;

dailyEarnings = X.numberOfAdsX.adClickPercentageX.avgRevenuePerAd;

cout << "The number of advertisements shown to readers is:" << X.numberOfAds << endl;

cout << "The percentage of advertisements clicked is:" << X.adClickPercentage << endl;

cout << "The average revenue per click is:" << X.avgRevenuePerAd << endl;

return dailyEarnings;

}

int main()

{

Advertisement MyWebCompany;

double revenue;

cout << "Enter the number of ads shown to readers: ";

cin >> MyWebCompany.numberOfAds;

cout << "Enter percentage of ads clicked by users: ";

cin >> MyWebCompany.adClickPercentage;

cout << "Enter average revenue per ad clicked: ";

cin >> MyWebCompany.avgRevenuePerAd;

revenue = dayRevenue(MyWebCompany);

cout << "Revenue is: $" << revenue << endl;

return 0;

}

  1. 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>

using namespace std;

struct Fraction

{

int numerator;

int denominator;

};

void printFractionProduct(Fraction Y, Fraction Z) {

int pnum;

int pden;

double product;

pnum = Y.numerator*Z.numerator;

pden = Y.denominator*Z.denominator;

product = static_cast<double>(pnum)/static_cast<double>(pden);

cout << "Product of fraction "<<Y.numerator<<"/"<<Y.denominator<<" and "<<Z.numerator<<"/"<<Z.denominator<<" is: "<<product<<endl;

}

int main()

{

Fraction Frac1;

Fraction Frac2;

cout << "Enter numerator for first fraction: ";

cin >> Frac1.numerator;

cout << "Enter denominator for first fraction: ";

cin >> Frac1.denominator;

cout << "Enter numerator for second fraction: ";

cin >> Frac2.numerator;

cout << "Enter denominator for second fraction: ";

cin >> Frac2.denominator;

printFractionProduct(Frac1, Frac2);

return 0;

}

  1. Why do we need structs?
    to store several collective variables in an object
  2. What are members in a struct?
    variables
  3. How do you access a member inside a struct?
    member selection operator .structNAME.memberNAME
  4. What is a nested struct?
    a structure that is defined inside another sturcture
  1. Structs are used to represents many different variables in one object. It can help organize code and make it easier to work with larger sets of data.

  2. Members are the specific properties of the struct that can have various values assigned to them. For example: struct Person { string name; int age }. In that sequence the members are name and age.

  3. It is accessed with dot notation: person.name or person.age would give the value passed to those members.

  4. A nested struct is when one struct is placed inside another.

1. Why do we need structs?

A struct is a convenient way to organise data, it is similar to an object in JavaScript. Multiple different variables called members can make up a struct.

2. What are members in a struct?

A struct member is an individual variable that makes up a struct.

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

Using the full stop "." operator access structs eg. cout << Employee.dave;

4. What is a nested struct?

A nested struct is a struct within a struct. Instead of a variable as a member of the struct another struct can also be a member.

Quiz

Part 1

#include

using namespace std;

struct Advertising {
int addNumber;
double addClickThrough;
double averageEarnedAdd;
};

Advertising inputAdvertisingInfo()
{
Advertising dayNumber;
cout << "Please enter the number of adds shown to readers today: ";
cin >> dayNumber.addNumber;
cout << "Please enter the percentage of adds clicked by users: ";
cin >> dayNumber.addClickThrough;
cout << "Please enter the amount of money earned on average from each clicked add: ";
cin >> dayNumber.averageEarnedAdd;
cout << endl;
return dayNumber;
}

void printAdvertInfo(Advertising dayNumber){
double totalEarned;
cout << "Number of adds shown to readers today: " << dayNumber.addNumber << endl;
cout << "Percentage click through from readers today: " << dayNumber.addClickThrough << “%” << endl;
cout << “Average earnings from clicked adds: $” << dayNumber.averageEarnedAdd << endl;
totalEarned = dayNumber.addNumber * (dayNumber.addClickThrough / 100) * dayNumber.averageEarnedAdd;
cout << “Total earned today is: $” << totalEarned << endl;
cout << endl;
}

int main()
{
Advertising day1 = inputAdvertisingInfo();
printAdvertInfo(day1);

Advertising day2 = inputAdvertisingInfo();
printAdvertInfo(day2);

Advertising day3 = inputAdvertisingInfo();
printAdvertInfo(day3);

return 0;

}

Part 2

#include

using namespace std;

struct Fraction {
int numerator;
int denominator;
};

Fraction readData(){ //note different function header format
Fraction x;
cout << "Please enter the numerator: ";
cin >> x.numerator;
cout << endl;
cout << "Please enter the denominator: ";
cin >> x.denominator;
cout << endl;
return x;
}

double multiply (Fraction y, Fraction z){
double result; //not below the ints need to be parsed to doubles for the devision to work
result = static_cast ((double)y.numerator / (double)y.denominator) * ((double)z.numerator / (double)z.denominator);
return result;
}

int main()
{
Fraction one = readData();
Fraction two = readData();

cout << multiply(one, two) << endl;

return 0;
}

1.There are many instances in programming where we need more than one variable in order to represent an object. Using independent variables becomes messy. A struct (short for structure) allows us to group variables of mixed data types together into a single unit.
2. The variables that are part of the struct are called members (or fields)
3. “struct name”.“member name”
4. Structs can contain other structs.

  1. structs(short for structure) allow us to group variables of mixed data types together into a single unit.
  2. The variables that are part of and define the struct.
  3. In order to access the individual variable members. we use the member selection operator(which is a period).
  4. A struct containing other structs.

Why do we need structs?

  • To group multiple individual variables with different data types together

What are members in a struct?

  • Members in a struct are the variables inside the struct.

How do you access a member inside a struct?

  • In order to access the individual members, we use the member selection operator (which is a period). e.g. joe.age

What is a nested struct?

  • A nested struct is a struct containing a struct.