You are on page 1of 11

COS1512 ASSIGNMENT 02

QUESTION 1
#include <iostream>
#include<cstring>
#include<string>
#include <iomanip>
#include <cmath>
using namespace std;

const int MULT_1 = 7;


const int MULT_2 = 16;
const int MULT_3 = 31;

int calcDogsAge(int dogAge)


{
return dogAge * MULT_1;
}

int calcDogsAge(double dogAge)


{
return MULT_2 * log(dogAge) + MULT_3;
}

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>

using namespace std;


bool isLeapYear (int year);
int current_year = 2021;
int current_month = 6;
int current_day = 30;

void setDateOfBirth(int &day, int &month, int &year)


{
cout << "Please enter the date you were born (d m y): ";
cin >> day >> month >> year;
cout<<endl;
assert((day >= 1 && day <= 31) && (month >= 1 && month <= 12) && (year >= 1900 && year <=
current_year));
}

// check the leap year


bool isLeapYear (int year)
{
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;

int calcAge(int day, int month, int year)


{
if (month > current_month) {
current_year = current_year - 1;
}

return current_year - year;


}

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;

void process(ifstream& infile, ofstream& outfile)


{
string code;
float perc1;
float mark1;
float perc2;
float mark2;
float yearmark;

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";

string inName = "assignments.dat";


infile.open(inName.c_str());
outfile.open(outName.c_str());
if (!infile)
{
cout << "Cannot open file " << inName << " Aborting!" << endl;
exit(1);
}

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;

void checkFile(ifstream& infile)


{
char ch;
infile.get(ch);
while(!infile.eof())
{
cout << ch;
infile.get(ch);
}
}
int main()
{
ifstream infile;
ofstream outfile;
ifstream indisplay;
ifstream outdisplay;
string inName, outName;
cout << endl << "Enter the input file name. " << endl;
cin >> inName;
infile.open(inName.c_str());
if (infile.fail())
{
cout << "File "<< inName << " NOT found!" << endl;
exit(1);
}
cout << endl << "Enter the output file name. " << endl;
cin >> outName;
outfile.open(outName.c_str());
if (outfile.fail())
{
cout << "File "<< outName << " NOT found!" << endl;
exit(1);
}

cout << endl;


char next;
infile.get(next);
while (!infile.eof())
{
if (next == '0')
outfile << "s";
else if (next == '1')
outfile << "g";
else if (next == '2')
outfile << "o";
else if (next == '3')
outfile << "y";
else if (next == '4')
outfile << "v";
else if (next == '5')
outfile << "n";
else if (next == '6')
outfile << "f";
else if (next == '7')
outfile << "j";
else
outfile << next;
infile.get(next);
}

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 ;

if (middleName == " ")


{
middleName = "";
}
else
{
middleName = middleName.substr(0,1);
std::transform(middleName.begin(), middleName.end(),middleName.begin(),
::toupper);
middleName = middleName + ".";
}

for (int i = 0; i < firstName.length(); i++)


{
if (i == 0)
{
firstName[i] = toupper(firstName[i]);
}
else
{
firstName[i] = tolower(firstName[i]);
}
}

for (int i = 0; i < lastName.length(); i++)


{
if (i == 0)
{
lastName[i] = toupper(lastName[i]);}
else
{
lastName[i] = tolower(lastName[i]);
}
}
cout << lastName + ", " + firstName + " " + middleName;
return 0;
}

QUESTION 6

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

vector<string> split(string target,string delimeter);


vector<string> split(string target, string delimiter) {
vector<string> split_string;
string temp_string ("");
for (unsigned int i = 0; i < target.size( ); i++)
{
if ((target[i]) != (delimiter[0]))
{
temp_string += target[i];
}
else{
split_string.push_back(temp_string);
temp_string = "";
}
}
return split_string;
}

void display(vector<string> split_stringP, string delimiterP)


{
for (int i = 0; i < split_stringP.size(); i++) {
cout << '"' << split_stringP[i] << '"';
if (i < split_stringP.size()-1) {
cout << delimiterP;
}
}
cout << endl;
}

int main ()
{

string input, delimiter;


vector<string> split_string;
cout << "Enter string to split: " << endl;
getline(cin, input);
cout << "Enter a delimiter: ";
cin >> delimiter;
split_string = split(input, delimiter);
display(split_string, delimiter);
return 0;
}

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.

b) The dereference operator or indirection operator, sometimes denoted by "*" an asterisk,


is a unary operator. It operates on a pointer variable and returns an l-value equivalent to the value at
the pointer address. This means that we want to work on the value stored at the location pointed to, this
is when we use the dereferencing operator.

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

k) Advantages of dynamic arrays:


*Size can be determined at run-time
* Allocation errors can be detected
* Can be resized
* Limits are usually higher than automatic

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;
}

void fll_array(int a[], int size)


{
cout << "Enter " << size << " integers:\n";
for (int index = 0; index < size; index++)
{
cin >> a[index];
}
cout << "====================================="<<endl;
}

void sort_array (int a[], int size)


{
int smallest_val, indexPositon;
positon
smallest_val = a[0];
indexPositon = 1;

for (int index = 1; index < size; index++)


{
if (smallest_val > a[index] )
{
smallest_val = a[index];
indexPositon = index + 1;
}
}

cout << "*************************************"<<endl;


cout << "The smallest value in the array is: " << smallest_val <<endl;
cout << "The value is at positon " << indexPositon << " of the array" <<endl;
cout << "*************************************"<<endl;
}

You might also like