Professional Documents
Culture Documents
QUESTION 1
#include <iostream>
#include<cstring>
#include<string>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
int dogsAge = 0;
cout << "Please enter dog's age: ";
cin >> dogsAge ;
cout << "Age with conventional method is " << calcDogsAge(dogsAge) << " years." << endl;
cout << "Age with empirical equation method is "
<< calcDogsAge((double)dogsAge) << " years." << endl;
return 0;
}
QUESTION 2
#include <iostream>
#include <assert.h>
#include <iomanip>
#include <ctime>
int main()
{
int day, month, year;
setDateOfBirth(day, month, year);
if (isLeapYear(year)){
cout << "You were born in leap year!" << endl;
}
cout << "Your are " << calcAge(day, month, year) << " years old." <<endl;
return 0;
}
QUESTION 3
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
outfile.setf(ios::fixed);
outfile.precision(2);
while(infile >> code >> perc1 >> mark1 >> perc2 >> mark2)
{
yearmark = perc1 / 100 * mark1 + perc2 / 100 * mark2;
outfile << code << ' ' << yearmark << "%"<< endl;
}
}
int main()
{
ifstream infile;
ifstream inout;
ofstream outfile;
string outName = "yearmark.dat";
if (!outfile)
{
cout << "Cannot open file " << outName << " Aborting!" << endl;
exit(1);
}
process(infile, outfile);
outfile.close();
infile.close();
inout.open(outName.c_str());
if (!inout)
{
cout << "Cannot open file " << outNaame << " Aborting!" << endl;
exit(1);
}
inout.close();
return 0;
}
QUESTION 4
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
infile.close();
outfile.close();
indisplay.open(inName.c_str());
if (indisplay.fail())
{
cout << "Cannot open file "
<< inName << " Aborting!" << endl;
exit(1);
}
cout << endl << "The contents of the input file is : "<< endl << endl;
checkFile(indisplay);
indisplay.close();
outdisplay.open(outName.c_str());
if (outdisplay.fail())
{
cout << "Cannot open file "
<< outName << " Aborting!" << endl;
exit(1);
}
cout << endl << endl <<"The contents of the output file is : "<< endl << endl;
checkFile(outdisplay);
outdisplay.close();
cout << endl;
return 0;
}
QUESTION 5
#include <iostream>
#include <algorithm>
#include <string>
#include <ctype.h>
using namespace std;
int main()
{
string firstName = "";
string middleName = "";
string lastName = "";
cout << "Please enter your First NAme Followed by Middle Name and Last name." << endl;
cin >> firstName >> middleName >> lastName ;
QUESTION 6
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int main ()
{
QUESTION 7
a) A pointer is the memory address of a variable and NOT the value stored in that variable. It is an
address used to tell where a variable is stored in memory. Pointers “point” to a variable by telling
where the variable is located.
c) p1 = p2;
This is a standard assignment statement that assigns the value/variable on the right of the ‘equals to
sign’ to the value/variable to the left of the equals to sign.
*p1 = *p2;
When you add an asterisk, you are not dealing with the pointers p1 and p2 BUT rather the variables that
the pointers are pointing to. We are assigning the variable that the pointer p1 point to, to the variable
that the pointer p2 points to.
d) When we use delete on a pointer variable, we destroy the dynamic variable that is being
pointed to. If another pointer variable was pointing to the dynamic variable, that variable is also
undefined. Undefined pointer variables are called dangling pointers.
e) Variables created using the ‘new’ operator are called dynamic variables. Dynamic variables are
created and destroyed while the program is running.
f) Using the ‘new’ operator with class type calls a constructor as well as allocates memory for the
newly defined object.
g) When dynamic variables are no longer needed, they can be deleted to return memory to the
freestore. The delete operator releases memory within the application/program.
h) Free Store is a pool of un-allocated heap memory given to a program that is used by the
program for dynamic allocation during the execution of the program. Every program is provided with a
pool of un-allocated heap memory that it may utilize during the execution. This pool of available
memory is referred to as the free store of the program. The allocated free store memory is unnamed.
i)
Variables created with the ‘new’ operator are called Dynamic variables.
Variables declared in a function are created and destroyed when the function ends. They are
called automatic variables because their creation and destruction are controlled automatically.
j) A dynamic array is an array whose size is determined when the program is running, not when
you write the program
l) Arrays and pointers are intimately related in C++ and may be used almost interchangeably. An
array name can be thought of as a constant pointer. Pointers can be used to do any operation
involving arrays.
m)
int * p1, p2;
This is a declaration of two pointer variables of type integer. A pointer is the memory address of a
variable.
typedef int* IntPtr;
This is a pointer type name definition, which means that we can use the type IntPtr which is the type
for pointer variables that contain pointers to int variables.
IntPtr p1, p2;
Using the typedef ‘IntPtr’, we create two pointers to integer variables namely: p1 and p2. This is
equivalent to: int* p1, p2;
n)
(i) // Declare two variables fPtr1 and fPtr2 to be pointers to objects of type double
double *fPtr1, *fPtr2;
(ii) fPtr1 = new double;
(iii) fPtr2 = * fPtr1;
(iv) // Print the address of the object pointed to by fPtr1
print("Address of object pointed to by fPtr1 is %d\n",&fPtr1);
(v) // Print the value of the object pointed to by fPtr2.
cout << *fPtr2 << endl;
(vi) delete fPtr1;
(vi)
fPtr1 = NULL;
fPtr2 = NULL;
(o)
LINE 1: int x = 10, y = 20, *p, *q;
LINE 2: p = &x;
LINE 3: q = &y;
LINE 4: *p = 20;
LINE 5: *q += 10;
LINE 6: cout << x << " " << y << endl;
LINE 7: cout << x << " " << y << endl;
LINE 8: *p = *q + 5;
LINE 9: cout << *p << " " << *q << endl;
LINE 10: q = p;
LINE 11: LINE 10: q = p;
(p)
#include <iostream>
#include<cstring>
#include <stdio.h>
using namespace std;
integer values/addresses
typedef int* IntArrayPtr;
void fll_array (int a[], int size);
void sort_array (int a[], int size);
int main()
{
int array_size;
cout << "How many numbers will be stored? ";
cin >> array_size;
cout << "====================================="<<endl;
IntArrayPtr a;
a = new int[array_size];
fll_array(a, array_size);
sort_array(a, array_size);
delete [] a;
return 0;
}