2.1
Structures:
A structure is a
collection of simple variables. The variables in a structure can be of
different types; some can be int,
some can be float, and so on. The
data items in a structure are called the members
of the structure.
Structures are one of the
two important building blocks in the understanding of objects and classes. In
fact, the syntax of a structure is almost identical to that of a class. A
structure can be called as a collection of data, while class is a collection of
both data and functions. So by learning about structures we’ll be paving the
way for an understanding of classes and objects. Structure is user-defined
type, with a structure declaration serving to define the type’s data properties.
After you define the type, you can create variables of that type.
Thus, creating a structure
is a two – part process. First, you define a structure description. It
describes and labels the different types of data that can be stored in a
structure. Then, you can create structure variables, or, more generally,
structure data objects, that follows the description’s plan.
Here’s the structure
description:
struct <structuretag> // Structure name
{
structure element 1;
structure element 2;
… … …
structure element N;
};
The keyword struct indicates that the code defines
the layout for a structure. The structuretag
is the identifier name of the structure.
For example:
struct data
{
char name[20];
int age;
char address[40];
};
for the above example; data is the name for the new type. Thus,
you now can create variables of type data.
Next, between braces, comes the list of data types to be held in the structure.
Each list item is a declaration statement. This example has char, int. each individual item in the
list is called a structure member.
// Program on a simple
structure
# include <iostream.h>
struct product {
int pnumber;
char type;
float price;
};
void main( )
{
product p1;
p1.pnumber = 1234;
p1.type = ‘A’;
p1.price = 214.50;
cout <<”\n Product number ”<< p1.pnumber;
cout <<”\n Product type “<< p1.type;
cout <<”\n Product price “<< p1.price;
}
2.2
Defining a Structure Variable:
The statement
product
p1;
defines a variable, called p1, of type structure product. This definition reserves space
in memory for p1 will hold enough
space depending upon the number of elements in the structure. In some ways we
can think of the product structure
as the specification for a new data type. The format for defining the structure
variable is the same as that for defining a built – in data type. This
similarity is not accidental. One of the aims of C + + is to make the syntax
and the operation of user-defined data types as similar as possible to that of
built – in data types.
2.3
Accessing Structure Members:
Once a structure variable
has been defined, its members can be accessed using some – thing called the dot operator. Here’s how the first
member is given value:
p1.pnumber = 1234;
The structure member is
written in three parts: the name of the structure variable (p1); the dot
operator, which consists of a period (.);
and the member name (pnumber). This means the pnumber is member
of p1. Remember that the first
component of an expression involving the dot operator is the name of the
specific structure variable (p1 in this case), not the name of the structure
specifier (product). The variable name must be used to distinguish one variable
from another when there is more than one, such as p2, p3, etc,. Structures members
are treated just like other variables. In the statement p1.pnumber = 1234; the member is given the value 1234 using a
normal assignment operator.
2.4
Combining Specifier and Definition:
In the product example we showed the structure specifier and the definition
as two separate statements. These two statements can also be combined into a
single statement,
// Program to demonstrate
structures
# include <iostream.h>
struct //no tag needed
{
int pnumber;
char type;
float price;
}p1; // definition goes here
void main( )
{
product p1;
p1.pnumber = 1234; //
give values to structure members
p1.type = ‘A’;
p1.price = 214.50; //display
structure members
cout<<”\n Product number”<<p1.pnumber;
cout <<”\n Product type “<< p1.type;
cout <<”\n Product price “<< p1.price;
}
In this program there is no
separate statement for the structure definition. Instead, the variable name p1 is placed at the end of the
specifier:
struct
{
int pnumber;
char type;
float price;
}p1;
Notice that the tag name in
the Structure specifier can be removed, as we show here, if no more variables
of this structure type will be defined. Merging the structure specification and
definition this way is a shorthand approach that can save a few program lines.
Generally it is less clear and less flexible than using separate specifier’s
and definitions.
2.5 Initializing Structure Members:
The example below shows how structure
members can be initialized when the structure is defined. It also demonstrates
that you can have more than one variable of a given structure type.
# include <iostream.h>
struct product {
int pnumber;
char type;
float price;
};
void main( )
{
product p1 = {1234, ‘A’, 217.50}; // initialize variables
product p2;
p1.pnumber = 1234;
p1.type = ‘A’;
p1.price = 214.50;
cout <<”\n Product number ”<< p1.pnumber;
cout <<”\n Product type “<< p1.type;
cout <<”\n Product price “<< p1.price;
p2 = p1;
cout <<”\n Product number ”<< p2.pnumber;
cout <<”\n Product type “<< p2.type;
cout <<”\n Product price “<< p2.price;
}
This program defines two
variables of type product: p1 and p2.
It initializes p1, prints out the values of its members, assigns p1 to p2, and prints out its
members. Here’s the output:
Product number
1234
Product type A
Product price 217.50
Product number
1234
Product type A
Product price 217.50
Not surprisingly the same
output is repeated, since one variable is made equal to the other.
2.6 Initializing Structure Variables:
The p1 structure
variable's members are initialized when the variable is defined:
product p1 = { 1234,’A’,217.50 };
The values to be assigned to
the structure members are surrounded by braces and separated by commas. The
first value in the list is assigned to the first member, to the member, and so
on.
2.7 Structure Variables in Assignment Statements:
In the above program, one
structure variable can be assigned to another:
p2 = p1;
The value of each member of
p1 is assigned to the corresponding member of p2. Since a large structure can
have dozens of members, such an assignment statement can require the computer
to do a considerable amount of work. Note that one structure variable can be
assigned to another only when they are of the same structure type. If you try
to assign a variable of one structure type to a variable of another type, the
compiler will complain.
2.8 Structures Within
Structures:
You can nest structures
within other structures. Here’s a variation on the program that shows how this
looks. In this program we want to create
a data structure that stores the dimensions of a typical room: its length and
width.
// Program to demonstrates
nested structures:
struct Distance
{
int feet;
float inches;
};
struct Room
{
Distance length;
Distance width;
};
void main( )
{
Room
r1;
r1.length.feet=15;
r1.width.inches = 6.5;
r1.length.feet=10;
r1.width.inches = 0.0;
float 1 = r1.length.feet + (r1.
length.inches) / 12;
float w= r1. width.feet + (r1.width.inches)
/ 12;
cout<< “\n Room area is”<<l *
w <<”square feet”;
}
This program defines a
single variable r1 of type Room in the line.
Accessing
Nested Structure Members: Because one structure is nested inside another, we
must apply the dot operator twice to access the structure members:
r1.length.feet =
15;
In this statement r1 is the name of the
structure variable, as before; length is the name of a member in the outer
structure (Room); and feet is the name of a member of the inner structure (Distance).
The statement means take the feet member of the length member of the variable r1
and assign it the value 13.
2.9 Functions:
A function groups a number of program
statement into a unit and gives it a name. This unit can then be invoked from
other parts of the program. The most important reason to use functions is to
aid in the conceptual organization of the program. Dividing a program into
functions is one of the major principles of structured programming. Another
reason to use functions is to reduce program size.
//Simple function program
void starline( ); // function declaration
void main( )
{
starline( ); // call function
cout<<”Data
type Range”<<endl;
starline( ); // call to function
cout <<
"char -128
to 127" << endl
<<"int -32,768 to
32,467" << endl
<<"double -2,147,483,648 to
2,147,483,647" << endl; starline( ); // call to function
)
// function definition
void starline( ) // function declarator
{
for(int j=0;
j<45; j++) // function body
cout <<
'*';
cout <<
endl;
}
The program consists of two
functions: main ( ) and starline ( ). You've al- ready seen many
programs that use main ( ) alone. What other components are necessary to
add a function to the program? There are three: the function declaration, the
calls to the function, and the function definition.
2.10
Defining a function:
We can group functions into
two categories: function that don’t have return values and functions that do
have return values. Functions without return values are termed type void functions and have the following
general form:
void functionName(argumentList)
{ statement(s)
return; //
optional
}
Here argumentList specifies the
types and number of arguments passed to the function. The optional return
statement marks the end of the function. Otherwise, the function terminates at
the closing brace. You can use a void
function to perform some sort of action. For example, a function to print
Hello! a given number (n) of times can look like this:
void hello(int n) // no return value
{
for (int i = 0; i < n; i++)
cout <<"Cheers!
\n";
}
The int n argument list means
that cheers( ) expects to be passed
an int value as an argument when you
call this function.
A function with a return value produces a value that it returns to the
function that called it. In other words, if the function returns the square
root of 9.0 (sqrt(9.0)), then the function call has the value 3.0. Such a
function is declared as having the same type as the value it returns. Here is
the general form:
typeName
functionName(argumentList) {
statements
return value; // value is of type typename
}
Functions
with return values require that you use a return statement so that the value is
returned to the calling function. The value itself can be a constant, a
variable, or a more general expression. The only requirement is that the
expression reduces to a value that has, or is convertible to, the typeName type. If the declared return
type is, say double, and the
function returns an int expression,
the int value is typecast to type double. The function then returns the
final value to the function that called it. C++ does place a restriction on
what types you can use for a return value: The return value cannot be an array.
Everything else is possible-integers, floating-point numbers, pointers, even
structures and objects. Even though a C++ function can't return an array
directly it can return an array that’s part of a structure or object.
2.11 Calling The Function:
All we need to call the
function is: the function name followed by parentheses. The syntax of the call
is very similar to that of the declaration, except that the return type is not
used. A semicolon terminates the call statement. Executing the call statement
causes the function to execute; that is, control is transferred to the
function, the statements in the function definition are executed, and then
control returns to the statement following the function call.
Function Prototyping: The prototype describes the function interface to the compiler. That is,
it tells the compiler what type of return value, if any, the function has, and
it tells the compiler the number and type of function arguments. For example,
double volume = cube(side);
First, the prototype tells the compiler that cube( ) should have one type double argument. If the program fails
to provide one, prototyping allows the compiler to catch the error. Second,
when the cube( ) function finishes
its calculation, it places its return value at some specified location perhaps
in a CPU register, perhaps in memory. Then, the calling function, main( ) in this case, retrieves the
value from that location. Because the prototype states that cube( ) is type double, the compiler knows how many bytes to retrieve and how to
interpret them. Without that information, the compiler could only guess.
2.12
Passing Arguments to functions:
An argument is a piece of
data (an e.g.: int value) passed from a program to the function. Arguments
allow a function to operate with different values, or even to do different
things, depending on the requirements of the program calling it.
passing by Value: Passing arguments by value means the numeric value of the
argument is passed to the function, where it is assigned to a new variable. For
example,
double volume =
cube(side);
Here side is a variable that,
in the sample run, had the value 5. The function heading for cube( ), is:
double cube(double x);
When this function is called, it creates a new type double variable called x
and assigns the value 5 to it. This insulates data in main( ) from actions that take place in cube( ) , for cube( )
works with a copy of side rather than
with the original data. A variable that's used to receive passed values is
called a formal argument or parameter. The value passed to the
function is called the actual argument or
parameter. Thus, argument passing
assigns the actual argument to the formal argument. Variables, including formal
parameters, declared within a function are private to the function. When a
function is called, the computer allocates the memory needed for these
variables. When the function terminates, the computer frees the memory that was
used for those variables. C++ literature refers to this allocating and freeing
of memory as creating and destroying variables. That does make it sound much
more exciting. Such variables are called local
variables because they are localized to the function. This helps preserve
data integrity It also means that if you declare a variable called x in main( ) and another variable called x in some other function, these are two distinct, unrelated
variables, much as the Hyderabad in India is distinct from the Hyderabad in
Pakistan.
//demonstrates passing
structure as argument
#include <iostream.h>
struct Distance
{ int
feet;
float inches;
};
void engldisp( Distance ); // declaration
void main( )
{
Distance d1, d2; // define two lengths
// get length d1 from user
cout <<
“Enter feet:”; cin>>d1.feet;
cout
<<”\nEnter inches:”; cin>>d1.inches;
//get
length d2 from user
cout <<
"\nEnter feet: "; cin>>d2.feet;
cout<<”\nEnter
inches: "; cin>>d2.inches;
cout <<
"\nd1 = ”; // display length 1
engldisp(d1);
cout <<”
\nd2 =”; /I display length 2
engldisp(d2);
}
void engldisp(Distance dd) //parameter dd of type Distance
{
cout <<
dd.feet << "\" << dd.inches << "\"";
}
The function
declaration and the function calls in main( ), and the declarator in the
function body, treat the structure variables just as they would any other
variable used as an argument; this one just happens to be type Distance, rather
than a basic type like char or int .
In main( ) there are
two calls to the function engldisp ( ).
The first passes the structure d1 ; the second passes d2. The
function engldisp ( ) uses a parameter that is a structure of type Distance
, which it names dd. As with simple variables, this structure variable is
automatically initialized to the value of the structure passed from main( ). Statements in engldisp ( ) can then access the
members of dd in the usual way, with the expressions
dd
.feet and dd .inches .
As with simple variables,
the structure parameter dd in engldisp ( ) is not the same as the
arguments passed to it (d1 and d2). Thus engldisp ( ) could modify dd
without affecting d1 and d2.
Returning values from Functions: When a function completes its execution it can return a single value
to the calling program. Usually this return value consists of an answer to the
problem the function has solved. The next example demonstrates a function that
returns a weight in kilograms, after being given a weight in pounds. Here’s the listing for convert.
// demonstrates return values, converts
pounds to kg
#include <iostream.h>
void main( )
{
float lbs,
kgs;
cout<<"\nEnter
your weight in pounds: ";
cin<< lbs;
kgs =
lbstokg(lbs);
cout<<"Your
weight in kilograms is “<<kgs;
}
// function to converts
pounds to kilograms
float
lbstokg(float pounds)
{
float kilograms =
0.453592 * pounds;
return kilograms;
}
When a function returns a
value, the data type of this value must be specified. The function declaration
does this by placing the data type, float in this case, before the
function name in the declaration and the definition. Functions in earlier
program examples returned no value, so the return type was void.
Returning
Structure Variables: We know that structures can be used as arguments to
functions. You can also use them as return values. Here's a program that
incorporates a function that adds variables of type structure Distance and returns a value of this
same type.
//demonstrates returning a
structure
# include <iostream.h>
struct Distance
{
int feet;
float inches;
};
Distance addengl(Distance,
Distance); //declarations
void engldisp(Distance);
void main( )
{
Distance d1, d2, d3; II define
three lengths
II get length d1 from user
cout<<"\nEnter
feet: "; cin >>
d1.feet;
cout<<"Enter
inches: "; cin >>
d1.inches;
//get length d2
from user
cout<<"\nEnter
feet: "; cin>>
d2.feet;
cout<<"Enter
inches: "; cin>> d2.inches;
d3 = addengl(d1,
d2); // d3 is sum of d1 and d2
engldisp(d1); cout<<" + "; IIdisplay all lengths
engldisp(d2); cout<<" = ";
engldisp(d3); cout<<"\n";
}
Distance addengl( Distance
dd1, Distance dd2 )
{
Distance dd3;
dd3.inches =
dd1.inches + dd2.inches;
dd3.feet = 0;
if(dd3.inches
>= 12.0)
{
dd3.inches - =
12.0;
dd3.feet++;
}
dd3.feet + = dd1.feet+ dd2.feet;
return dd3;
}
//display structure of type
Distance in feet and inches
void engldisp( Distance dd )
{
cout <<
dd.feet << ..\'-" << dd.inches << "\"";
}
The program asks the user
for two lengths, in feet-and-inches format, adds them her by calling the
function addengl( ) and displays the results using the engldisp( ) function introduced in the
program.
The main ( ) part of the
program adds the two lengths, each represented by a structure of type D i s t a
n c e , by calling the function a d d E n g l :
d3 = addengl(d1,
d2);
This function returns the
sum of d1 and d2, and the result is assigned to the structure d3. Internally
the addengl ( ) function must create a new variable of type Distance to hold
the results of its calculation. It can't simply return an expression, as in
return dd1+dd2; // doesn't make sense
because the process of
adding the two structures actually takes several steps: The inches are added
separately from the feet. Instead the values of the individual members of d3
are calculated, and then d3 is returned to the calling program with the
statement
return d3;
Besides showing how
structures are used as return values, this program also shows functions (three
if you count main( )) used in the same
program. You arrange the functions in any order. The only rule is that the
function declarations must appear in the listing before any calls are made to
the functions.
Reference
Arguments: A reference provides an alias-a different
name-for a variable. By far the most important use for references is in passing
arguments to functions. We've seen examples of function arguments passed by
value. When arguments are passed by value, the called function creates a new
variable of the same type as the argument and copies the argument’s value into
it. As we noted, the function does not have access to the original variable in
the calling program, only to the copy it created. Passing arguments by value is
useful when the function does not need to modify the original variable in the
calling program. In fact, it offers insurance that the function cannot harm the
original variable. Passing arguments by reference uses a different mechanism
Instead of a value being passed to the function, a reference to the original
variable, in the calling program, is passed.
The primary advantage of passing by
reference is that the function can access the actual variables in the calling
program. Among other benefits this provides a mechanism for returning more than
one value from the function back to the calling program.
//orders two arguments
passed by reference
#include <iostream.h>
void main( )
{
void order(int&,
int&); //prototype
int n1=99, n2=11;
//this pair not ordered
int n3=22. n4=88;
//this pair ordered
order(n1, n2); //order each pair of
numbers
order(n3, n4);
// print out all numbers
cout <<
endl << "n1=" << n1;
cout <<
endl << "n2=" << n2;
cout <<
endl << "n3=" << n3;
cout <<
endl << "n4=" << n4;
}
void order(int& numb1,
int& numb2) // orders two numbers
{
if(numb1 > numb2) // if 1st Larger
than 2nd,
{
int
temp = numb1; //
swap them
numb1
= numb2;
numb2
= temp;
}
}
In main( ) there are
two pairs of numbers-the first pair not ordered and the second pair ordered.
The order( ) function is called once for each pair, and then all the
numbers are printed out. The output reveals that the first pair has been swapped
while the second pair hasn't.
Passing structures by reference: You can pass structures by reference. Here is the sample program.
// Program to demonstrates
passing a structure by reference
#include <iostream.h>
struct Distance
{
int feet;
float inches;
};
void scale(Distance&,
float); //function declarations
void engldisp(Distance);
void main( )
{
Distance d1={12,
6.5}; // initialize
d1 and d2
Distance d2={10,
5.5}
cout <<
“\nd1 =”; engldisp(d1); // display old d1 and d2
cout << ” \nd2 =”; engldisp(d2);
II scale d1 and d2
scale(d1,0.5);
scale(d2, (0.25);
// display new d1 and
d2
cout <<
"\nd1 = ", engldisp(d1);
cout <<
"\nd2 = ", engldisp(d2);
}
//scales value of type
Distance by factor
void scale
(Distance& dd, float factor)
float inches =
{dd.feet*12 + dd.inches) * factor;
dd.feet = inches I 12;
dd.inches =
inches -dd.feet * 12;
}
//Display structure of type
Distance in feet and inches
void engldisp( Distance dd )
II parameter
dd of type Distance
{
cout<< dd.feet
<< "\,-“<<dd.inches << "\"";
}
The program initializes two
Distance variable d1 & d2 to specific values, and displays them. Then it
calls the scale( ) function to multiply d1 by 0.5 and d2 by 0.25.Finally
it displays the resulting values of the distances.
2.13 Function Overloading:
Function overloading allows
you to create different functions that have the same name provided that they
have different argument lists. Function overloading enables you to use the same
name for related functions that performs the same basic task in different ways
or for different types. It would be far more convenient to use the same name
for all three functions, even though each value has different arguments. Here’s a program, to illustrate overloading.
//demonstrates function
overloading
#include<iostream.h>
void repchar( );
void repchar(char);
void repchar(char,int);
void main( )
{
repchar( );
repchar(‘=’);
repchar(‘*’,40);
}
void repchar( );
{
for(int i=0;
i<=40; i++)
cout<<”+”<<endl;
}
void repchar(char c);
{
for(int i=0;
i<=40; i++)
cout<<c<<endl;
}
void repchar(char c, int j);
{
for(int i=0;
i<=j; i++)
cout<<c<<endl;
}
The program
contains three functions with the same name. There are three declarations,
three function calls, and three function definitions. What keeps the compiler
from becoming hopelessly confused? It uses the number of arguments, and their
data types, to distinguish one function from another. In other words, the
declaration
void repchar( );
which takes no arguments,
describes an entirely different function than does the declaration
void
repchar(char);
which takes one argument of
type c h a r, or the declaration
void
repchar(char, int);
which takes one argument of
type c h a r and another of type i n t .
The compiler, seeing several
functions with the same name but different numbers of arguments, it sets up a
separate function for every such definition. Which one of these functions will
be called depends on the number of arguments supplied in the call.
2.14 Inline Function:
Functions save memory space
because all the calls to the function, it causes the same code to be executed;
the function body need not be duplicated in memory. When the compiler sees a
function call, it normally generates a jump to the function. At the end of the
function it jumps back to the instruction following the calls. While this
sequence of events may save memory space, it takes some extra time. There must
be an instruction for the jump to the function, instructions for saving
registers, instructions for pushing arguments onto the stack in the calling
program and removing them from the stack in the function (if there are
arguments), instructions for restoring registers, and an instruction to return
to the calling program. The return value (if any) must also be dealt with. All
these instructions slow down the program.
To save execution time in
short functions you may elect to put the code in the function body directly in
line with the code in the calling program. That is, each time there's a
function call in the source file, the actual code from the function is
inserted, instead of a jump to the function. The trouble with repeatedly
inserting the same code is that you lose the benefits of program organization
and clarity that come with using functions.
The program may run faster and take less space, but the listing is
longer and more complex.
The solution to this
quandary is the inline function. This kind of function is written like a normal
function in the source file but compiles into inline code instead of into a
function. The source file remains well organized and easy to read, since the
function is shown as separate entity.
However, when the program is compiled, the function body is actually
inserted into the program, wherever a function calls occurs. Functions that are very short, say one or two
statements, are candidates to be inlined. Here’s a program to demonstrate
inline function.
#include<iostream.h>
//converts pounds to
kilograms
inline float lbstockg(float
pounds)
{
return 0.453592 *
pounds;
}
void main( )
{
float lbs;
cout<<”Enter the
weight in pounds:”;
cin>>lbs;
cout<<”Your weight in
kilograms is”<<lbstokg(lbs);
}
The keyword inline in the
function definition defines a inline function:
inline float
1gstokg(float pounds)
The compiler must have seen the function definition (not just the
declaration) before it gets to the first function call. This is because it must
insert the actual code into the program, not just instructions to call the
function. In the above program we must place the definition of lbstokg( ) before main( ). When you do this the function declaration is unnecessary
and can be eliminated. inline
keyword is actually just a request to the compiler. Sometimes the compiler will
ignore the request and compile the function as a normal function. It might
decide the function is too long to be inline.
2.15 Automatic Variables:
Variables may be defined inside main(
) or inside other functions; the effect is the same, since main( ) is a function. Variables
defined within a function body are called automatic
variables Actually, a keyword, auto,
can be used to specify an automatic variable. You would say
void somefunc( )
{
auto int somevar;
auto float othervar;
// other statements
}
However, since this is the default, there is seldom any need to use the
auto keyword. Variables defined
within a function are automatic anyway.
2.16 External
Variables:
The next major storage class is external.
While automatic variables are defined within functions, external variables
are defined outside of any function. External variables are also called global variables, since they are known
by all the functions in a program.
//demonstrates external variables
#include <iostream.h>
#include <conio.h> //
for getch( )
char ch = 'a' ; //
exteral variable ch
void main( )
{
while( ch != '\r' ) II main( ) accesses ch
{
getch( );
cout<<ch;
}
}
The significant of this program is that the variable ch is not defined in any of the
functions. Instead it is defined at the beginning of the file, before the first
function. It is an external variable. Thus the visibility of ch is the entire source file.
2.17 Static
Variables:
A static automatic variable has the visibility of a local variable but
the lifetime of an external variable. Thus it is, visible only inside the
function in which it is defined, but it remains in existence for the life of
the program. Static automatic variables are used when it's necessary for a
function to remember a value when it is not being executed; that is, between
calls to the function.
// Program to demonstrates static variables
# include <iostream .h>
float getavg(float); //prototype
void main( )
{
float data=1,avg;
while( data != 0 )
{
cout << "Enter a number: ";
cin>> data;
avg = getavg(data);
cout << "New average is" << avg << endl;
}
}
float getavg(float newdata) {
static float total=0; //
static variables are initialized
static int count =0; // only
once per program
count ++;
total += newdata;
return total / count;
}
In the program above the static variables
total and count in getavg( ) retain their values after getavg( )
returns, so they’re available the next time its called.
Exercise:
1.
A phone number, such as 091-040-123
4567, can be thought of as having four parts: the country, the state, the area
and the number. Write a program that uses a structure to store these four parts
of a phone number separately and asks the user to input the phone number and
display it.
2.
Write a function called
zeroSmaller( ) that is passed two int arguments by reference and then sets the
smaller of the two numbers to 0. Write a program to exercise this function.
3.
Write a function to swap the
values send to it by the main and display the swapped output in main.
4.
Write a function that takes two
Distance values as arguments and return the lager one.
5.
Here is a structure template:
struct box {
char market[40];
float height;
float width;
float volume;
};
a)
Write a function that passes a
box structure by value and that displays the value of each member.
b)
Write a function that passes
the address of a box structure and that sets the volume members to the product
of the other three dimensions.
c)
Write a program that uses these
two functions.
6.
Write a recursive function that
takes an integer argument and returns the factorial of that argument.