Structs C++ - Reading Assignments

  1. Why do we need structs?

A: A struct (short for structure) allows us to group variables of mixed data types together into a single unit.

  1. What are members in a struct?

A: The variables contained in a struct are considered the members and you can use the member selection operator to initialize each member variable.

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

A: Using member selection Operator.

  1. What is a nested struct?

A: A nested struct occurs when structs is contained within another structs.

  1. Why do we need structs?
    To describe objects with multiple atributes all bundled together within a single variable.

2.What are members in a struct?
Variables
3.How do you access a member inside a struct?
struct.member
4.What is a nested struct?
A structure defined within a structure

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

#include

using namespace std;

// First we need to define our Advertising struct

struct Advertising

{

int adsShown;

double clickThroughRatePercentage;

double averageEarningsPerClick;

};

Advertising getAdvertising()

{

Advertising user;

std::cout << "How many ads were shown today? ";

std::cin >> user.adsShown;

std::cout << "What percentage of ads were clicked on by users? ";

std::cin >> user.clickThroughRatePercentage;

std::cout << "What was the average earnings per click? ";

std::cin >> user.averageEarningsPerClick;

return user;

}

void printAdvertising(Advertising ad)

{

std::cout << "Number of ads shown: " << ad.adsShown << ‘\n’;

std::cout << "Click through rate: " << ad.clickThroughRatePercentage << ‘\n’;

std::cout << “Average earnings per click: $” << ad.averageEarningsPerClick << ‘\n’;

// The following line is split up to reduce the length

// We need to divide ad.clickThroughRatePercentage by 100 because it’s a percent of 100, not a multiplier

std::cout << “Total Earnings: $” <<

(ad.adsShown * ad.clickThroughRatePercentage / 100 * ad.averageEarningsPerClick) << ‘\n’;

}

int main()

{

// Declare an Advertising struct variable

Advertising ad = getAdvertising();

printAdvertising(ad);

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

#include

using namespace std;

//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

// First we need to define our Fraction struct

struct Fraction

{

int numerator;

int denominator;

};

Fraction getFraction()

{

Fraction user;

cout << "Pick a numerator ";

cin >> user.numerator;

cout << "Pick a denominator ";

cin >> user.denominator;

cout << “\n”;

return user;

}

void multiply(Fraction f1, Fraction f2) {

// Don’t forget the static cast, otherwise the compiler will do integer division!

cout << static_cast(f1.numerator * f2.numerator) /

(f1.denominator * f2.denominator);

}

int main()

{

// Allocate our first fraction

Fraction f1 = getFraction();

Fraction f2 = getFraction();

multiply(f1, f2);

return 0;

}

  1. Why do we need structs?

Structs help to organize data in neatly compacted form. Using variables for everything would quickly create ugly code.

  1. What are members in a struct?

These are the individual variables which can also include other structs.

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

Use the period (.)

  1. What is a nested struct?

It’s when a struct is declared within another struct.

Structs seem to be a lot like records in Pascal. However unlike Pascal, in C++ it seems that structs are not fully defined and allocated in the declaration. I personally like my declarations in one place (in the header or type clause) and try to avoid forward declarations as much as possible. When they can be declared anywhere, I find that guessing scope becomes a problem. My old Pascal teachers said that it’s really easy to write rotten code in C, now experiencing it first hand.

  1. Why do we need structs?
    Structs enable us to group multiple variables of differing data types into a single structure, such as the features of a car: Model name; Max speed; Air con?; Number of doors; etc. They make the data more logical and easier to work with. For instance, it is easier to pass a single struct to a function rather than 10 individual variables.

  2. What are members in a struct?
    Members are the individual variables that make up a struct.

  3. How do you access a member inside a struct?
    Specify the struct name, followed by a period, then the struct member. The period is called the member selection operator in this context.

  4. What is a nested struct?
    This is a struct within a struct. A struct is a data type and can therefore be declared, and initiated, within another struct.

  1. We need to made it easier to store values in variables and at the same time categorize the variables better by using structs. It is easier to lookup and use structs to better organize data sets.

  2. Members in a struct is the different kinds of variables in the structs. For example age, wage and date birth.

  3. The struct is declared by a capital letter like Employee, then we can use a dot to find the member like Employee.age.

  4. nested struct is to use more than one struct in the same function like Employee and Skillset for instance.

  1. A structure allows us to group variables of mixed data types together into a single unit.
  2. A field as part of a single unit structure
  3. use the member selection operator (which is a period). I.E.: structname.member = string;
  4. When a struct has another struct(s) as its member(s).

image

  1. You can store variables of different kinds in a struct.
  2. They are the variables.
  3. You use a “.” such as name.member.
  4. It’s a struct in a struct.

Couple of Stanford youtube linked videos are now private or offline, and it seems that NEM lectures missing the last video and my status says 99% completed of the course instead of 100%.

1. Why do we need structs?
A struct (short for structure) allows us to group variables of mixed data types together into a single unit.

2. What are members in a struct?
The variables (inside of or) that are part of a struct are called members (or fields). Struct names begin with a capital letter.

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

4. What is a nested struct?
Nested structs are structs contained within other structs.

  1. to group a bunch of variables
  2. variables in struct
  3. Struct.member
  4. a struct in a struct e.g. Struct1{...} Struct2{Struct1, ...}
1 Like

A struct allows us to group variables of mixed data types together into a single unit. For example if we want to pass in 6 types of information about a person by writing it like int Age, string Name and so on, you have to copy and past it all and change the information if you want to add another person. This will easily mess up the code and you get way better structure with structs.

Variables that are part of the struct

Declare a variable of the type with the structs name and then use a member selection operation, which is a dot

When a struct contains another struct

1 Like
  1. Why do we need structs?
    A struct allows us to group variables of mixed data types together into a single unit.

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

  3. How do you access a member inside a struct?
    By using the struct name and the member, separated by a period (member selector operator).

  4. What is a nested struct?
    Nested struct is a struct inside another struct.

1 Like
  1. We need structs to help us to store a lot of different products, dates or any other piece of information into a single unit.
  2. Member in struct are what you write in between { }; of your struct.
  3. If you have struct Employee and it contains for example John to access his information you need to eg. john.wage (john.wage would be one of the member inside that struct and you can access it by declaring it like normal variable.
  4. Nested struct is struct containing other structs.
1 Like

1 so we can assign more than one variable
2 variables within a struct
3 struct.member
4 it is a struct within a struct

1 Like

1 Structs help us to group multiple individual variables together.
2 Members in a struct is all variables inside the struct.
3 struct.member
4 A nested struct is a struct inside other struct.

1 Like

Why do we need structs? Because 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 are called members (or fields).
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). For example:
Employee joe; // create an Employee struct for Joe
joe.id = 14; // assign a value to member id within struct joe
joe.age = 32; // assign a value to member age within struct joe
joe.wage = 24.15; // assign a value to member wage within struct joe
What is a nested struct? It is structs can contain other structs. For example:
struct Employee
{
short id;
int age;
double wage;
};
struct Company
{
Employee CEO; // Employee is a struct within the Company struct
int numberOfEmployees;
};
Company myCompany;

1 Like
  1. Why do we need structs?
    To group variables of mixed data types into one unit.

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

  3. How do you access a member inside a struct?
    -StructDefinition.MemberName

  4. What is a nested struct?
    A structure which contains another structure as a member.

1 Like