Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. A function prototype tells the compiler four things. What are they? There are

ID: 3622341 • Letter: 1

Question

1. A function prototype tells the compiler four things. What are they?


There are three methods data is passed to functions, what are they?


Give an example of an event controlled loop.


Defining an identifier as const does what?

Why is the following condition good practice? if( 5 == num)


Explain short circuit logic using || (OR).


Compare contrast static and automatic storage class (lifetime).


  Explain what the ‘&’ ampersand allows regarding functions.


Define scope.


Compare contrast post and pre increment unary operators.


C++ has no bounds checking. What does this mean?


Write a declaration statement for a file named infile.  


1. Write a for loop that creates the following.  
+++++
++++
+++
++
+

     
                              


1. Explain the outcome of the following code.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
     ofstream outfile;
     outfile.open("c:\date.dat");
     if(!outfile)
     {
       cerr << "Unable to create file " << endl;
        exit(1);
     }

    for(int year = 1997; year <= 2007; year++)
       for(int month = 1; month <= 13; month++)
          for(int day = 1; day <= 31; day++)
          {
          cout << month << "/" << day << "/" << year << endl;
          outfile << month << "/" << day << "/" << year << endl;
          }
return 0;
}

Explanation / Answer

1. A function prototype tells the compiler four things. What are they?
Return type, scope, number of parameters, type of parameters
There are three methods data is passed to functions, what are they?
By value, by reference, by pointer (?)

Give an example of an event controlled loop.
string name;

//...

while (name=="Me") {

//...

}

Defining an identifier as const does what?
Prevents its value from changing

Why is the following condition good practice? if( 5 == num)
Because there's nothing wrong with it...?

Explain short circuit logic using || (OR).
If one of the operands evaluates to true, the other need not be calculated because the entire expression must be true.

Compare contrast static and automatic storage class (lifetime).
This is very vague...

Explain what the ‘&’ ampersand allows regarding functions.
The ampersand is the "address of" unary operator. It returns the memory address of the value of the operand. It is commonly used to pass values to functions by reference

Define scope.
The time/location in which a variable can be accessed

Compare contrast post and pre increment unary operators.
A postfix increment operator such as val++ increments val after any inline use of val.

A prefix increment operator such as ++val increments val before any inline use of val.

C++ has no bounds checking. What does this mean?
Bounds checking is the practice of checking that a variable or array index is within some constant bounds before use. These bounds depend on the data type of the variable, or the array size (in the case of an array index).  One consequence of the lack of c++ bounds checking is that variable overflows do not throw exceptions and array access is not checked for validity during compilation.

Write a declaration statement for a file named infile.
file infile; (?)

1. Write a for loop that creates the following.
+++++
++++
+++
++
+

//Assuming "create" means "print to console"
#include <iostream>

int main()
{

for(int i = 5; i>0; i--) {

for (int j = i; j>0; j--) {

std::cout << '+';

}

std::cout << std::endl;

}

}


1. Explain the outcome of the following code.
#include <--
#include <--

//should be

#include <iostream>

#include <fstream>

#include <cstdlib>


using namespace std;

int main()
{
ofstream outfile;
outfile.open("c:\date.dat");
if(!outfile)
{
cerr << "Unable to create file " << endl;
exit(1);
}

for(int year = 1997; year <= 2007; year++)
for(int month = 1; month <= 13; month++)
for(int day = 1; day <= 31; day++)
{
cout << month << "/" << day << "/" << year << endl;
outfile << month << "/" << day << "/" << year << endl;
}
return 0;
}

This code should not compile...

If the #include directives were fixed  it would write the following to the console and to the file "c:/data.dat":

1/1/1997

1/2/1997

1/3/1997

And so on until 13/31/2007