Create three integer variables and initialize them to whichever value you\'d lik
ID: 440134 • Letter: C
Question
Create three integer variables and initialize them to whichever value you'd like. ? Create three functions: ? A function that accepts two integers as parameters and subtracts the second from the first. This function then prints whether the result is positive, negative, or zero to the console. It does not return any value. ? A function that accepts two integers as parameters and returns whether the sum is even or odd. ? Hint: Modulus ? A function that accepts one integer as a parameter and returns whether or not that integer is exactly 7. ? In setup(), call all of these functions and make sure the results of the last two functions get printed. ? Only the first function may have a print statement in itExplanation / Answer
The term structure in C++ is a bit ambiguous; it means both a user-defined type which is a grouping of variables as well as meaning a variable based on a user-defined structure type. For the purpose of distinction I will refer to the user-defined type side as structure definition and the variable side as structure variable. A structure definition is a user-defined variable type which is a grouping of one or more variables. The type itself has a name, just like ‘int’, ‘double’, or ‘char’ but it is defined by the user and follows the normal rules of identifiers. Once the type has been defined through the C++ ‘struct’ keyword, you can create variables from it just like you would any other type. Since a structure definition is a grouping of several types: it is a group of one or more variables. These are known as elements or member variables as they are members of the structure definition they are part of. It is like you, your friend ‘Bob’, and your brother ‘Mike’ being members of a Girl Watching Club. That single group name would be the structure definition that refers to all of you, whom are members. Following through with our hinted example, a structure definition could be a ‘date’ which might be made up of three ‘int’ member variables: ‘day’, ‘month’, and ‘year’. Before creating a structure variable you must create a structure definition. This is a blue print for the compiler that is used each time you create a structure variable of this type. The structure definition is a listing of all member variables with their types and names. When you create a structure variable based on a structure definition, all of the member variables names are retained. The only name you have to give is that of the new structure variable. The element names within that variable will be the same as in the structure type. If you create two structure variables from ‘data’, both will have all three member variables with the same name in both: ‘day’, ‘month’, and ‘year’. Member variables are distinguished by the structure variable they are part of. You wouldn’t simply be able to use ‘day’ by itself; instead you’d have to refer to both the structure variable’s name (that you gave when you created it) as well as ‘day’. Defining Structures Defining a structure is giving the compiler a blue print for creating your type. Think of it as a list of people invited to a party. When you create a variable based on the structure definition, all of the member variables are created automatically and grouped under the name you gave. Note that when you create a variable of any kind, you must give it a unique name that is different than its type. Below is the syntax for a structure definition: struct tag { members; }; Writing a structure definition begins with the word ‘struct’ followed by the type-to-be, and ended with a structure block that is ultimately terminated with a semi-colon. Do not forget this trailing semi-colon when defining a structure! The name of a structure definition is known as the structure tag. This will be the name of the type that you create, like ‘int’ or ‘float’. It is the type that you will specify when creating a structure variable. This structure block is remarkably similar to a statement block since it starts and ends with curly braces. But don’t forget that it ultimately ends with a semi-colon. Within the structure block you declare all the member variables you want associated with that type. Declare them as you would normal variables, but do not try to initialize them. This is simply a data blue print, it is not logic or instructions and the compiler does not execute it. Remember the party member list I told you about earlier. The people on the list aren’t actually in the list; it is simply mentioning that they will be there when the party happens. Likewise the data members (synonym for member variables) of a structure won’t actually be created until a variable based on the structure is created. Technically an ‘int’ is just this as well. It’s a description of a storage unit. That storage unit isn’t reserved until you create a variable with it. Date Definition The follow defines a structure called ‘date’ which contains three ‘int’ member variables: ‘day’, ‘month’, and ‘year’: struct date { int day; int month; int year; }; If you’ve read the variables chapter, you’d know we can shorten this definition to the following: struct date { int day, month, year; }; Remember that you cannot initialize member variables in a structure definition. The following is wrong and will not compile: struct date { int day = 24, month = 10, year = 2001; }; Definition Scope Now we know how to define a structure, but where should you do it? A structure definition has the same type of scoping as a variable. If you define a structure in a function, you will only be able to use it there. If you define it in a nested statement block, you will only be able to use it inside there and any statement blocks nested within it. But the most common place is defining it globally, as in outside of any functions or blocks. Typically, you’ll want to be able to create variables of the defined structure anywhere in your program, so the definition will go at the top. The following is an empty program that defines a ‘date’ structure globally: 01 #include 02 03 struct date 04 { 05 int day, month, year; 06 }; 07 08 int main() 09 { 10 return 0; 11 } Both structure types and variables follow the same scope as normal variables, as do all identifiers. If you define a structure within a function, then you can only use it within that function. Likewise if you define a structure outside of any function then it can be used in any place throughout your program. Creating Structure Variables Once you have defined a structure you can create a variable from it just as you would any other variable. In the case of ‘date’ we could create a structure variable of this type by specifying that followed by a name: date birthday; C Note: In C you would have to specify ‘struct’ before a type that was based on a structure. Luckily, they got rid of this nuisance in C++. The above would create a variable called ‘birthday’ whose type is the structure ‘date’. The variable contains three parts: ‘day’, ‘month’, and ‘year’. What this actually does is set aside a whole block of memory that can contain all of the member variables. Each member variable then occupies a chunk of it for their individual storage units. The member variables are not actually created one at a time. Storage for member variables exist at some offset from the beginning of the glob of memory reserved by the entire structure. For example, in our ‘date’ structure variable the first member variable is ‘day’ so it exists at offset 0. The next member variable, ‘month’ in this case’, will exist at the next available offset. If ‘day’ uses 4 bytes (32 bits), then ‘month’ will be at offset 4: This is an important concept to remember as you will see later on. Initializing Structure Variables You cannot initialize member variables in the structure definition. This is because that definition is only a map, or plan, of what a variable based on this type will be made of. You can, however, initialize the member variables of a structure variable. That is, when you create a variable based on your structure definition you can pass each member variable an initializer. To initialize a structure variable’s members, you follow the original declaration, minus the semi-colon, with the assignment operator (=). Next you define an initialization block which is a list of initializers separated by commas and enclosed in curly braces. Lastly, you end it with a semi-colon. These values are assigned to member variables in the order that they occur. Let’s look at an example: date nco_birthday = { 19, 8, 1979 }; This creates a variable called ‘nco_birthday’ and initializes it to a list of values. The values are assigned to the member variables in the order they are declared in the structure definition. Remember what I mentioned about each member variable having an offset. The same order in which each member is given an offset is the order in which each is assigned a value in an initialization. So the first initializer is used to initialize the first member variable in the structure, next is the second, and so on and so forth. This order of initialization continues until the values run out. So, in our structure ‘date’, the member variable ‘day’ is first. Therefore the left-most value, ‘19’, will be assigned to ‘day’. Likewise ‘8’ is assigned to ‘month’ and ‘1979’ is assigned to ‘year’. If you try to assign more values than are member variables, you will get a compiler error. However, you can assign fewer values than there are member variables. If there are no more values to be assigned, the assignment will simply end. For example, if we had omitted the last value, ‘1979’, then no value would be assigned to ‘year’. So far I’ve only used numeric literals as initializers. It is possible to use any expression that you normally would. But remember that the expression must result in a value. Here is an example of initialization with things other than literals: int myday = 19; int mymonth = 5; date nco_birthday = { myday, mymonth + 3, 2001 - 22 }; Although you can assign a value to a variable in the same way you initialize it, the same is not true with structures. So while this works: int x; x = 0; This doesn’t: date nco_birthday; nco_birthday = { 19, 8, 1979 }; Assigning values to multiple members of a structure variable is only possible when that variable is first created. Once a structure variable has been declared, you must access each member individually. Accessing Members There’s not much you can do with the structure itself; much of your time with structure variables will be spent using its members. You can use a member variable in any place you’d use a normal variable, but you must specify it by the structure variable’s name as well as the member variable’s name using the member operator. C Note: The official is structure member operator in C, but that part was removed in C++ because structures aren’t the only types with members (or that use this operator). Consider the situation where you have two structure variables created from one definition; like ‘nco_birthday’ and ‘mdp_birthday’ both created from ‘date’. How do you use the member variable ‘day’? What if there is already a local variable called ‘day’; how are the two distinguished? The answer is of course by specifying the structure variable as well as its member. You must specify that which possesses what you want and the language assumes nothing. To specify that you want a member of a specific structure variable, you use the structure member operator which is the period (also known as a “dot”). Simply use the structure’s name, follow with the period, and end with the member: structure.member The following creates ‘nco_birthday’ from ‘date’ and then assigns each member a value: date nco_birthday; nco_birthday.day = 19; nco_birthday.month = 8; nco_birthday.year = 1979; In this way a member variable can be used in any place that a normal variable is, including mathematic operations, user input, and output. Consider the following program: 01 #include 02 03 struct date 04 { 05 int day, month, year; 06 }; 07 08 int main() 09 { 10 date birth; 11 cout birth.day; 18 19 coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.