Endsem 2022
Endsem 2022
#include<vector>
// FinalExam.cpp : This file contains the 'main' function. Program execution begins and ends
there.
//
void Q1()
{
//std::cout << The saint said, "God is one.";
std::cout << "The saint said, \"God is one\".";
//std::cout << \"The saint said, "God is one".\";
//std::cout << "The saint said, \\"God is one\\".";
}
void Q2()
{
int a = 1, b = 2, c = 3;
std::cout << (++a) - (b--) + (c++);
}
void Q3()
{
int a = 5, b = 4, c;
a = (a > 3) + (b <= 5);
b = (a == 3) + ((b - 2) >= 3);
c = (b != 1);
std::cout << a << ' ' << b << ' ' << c;
}
void Q4()
{
std::cout << ~(~0 << 5);
}
void Q5()
{
unsigned int a = 0xb9;
a = a ^ 0x1 << 4;
std::cout << std::hex << a;
}
void Q6()
{
union m { char a; int b; int c; };
std::cout << sizeof(m);
}
void Q7()
{
typedef struct { int a; int b; } M;
std::cout << sizeof(M);
}
void Q8()
{
typedef struct { int a; } S;
S z, * pz{ &z };
z.a = 10;
pz->a = z.a * 2;
std::cout << z.a << ' ' << pz->a;
}
void Q9()
{
typedef struct { int a; long sq; } SQ;
int i, j;
SQ squares[4], * sp;
for (i = 10, j = 0; i <= 40; i += 10, j++) {
squares[j].a = i; squares[j].sq = i * i;
}
for (j = 0, sp = squares; j < 4; ++j, sp++) {
std::cout << sp->a << " squared is " << sp->sq << std::endl;
}
}
#pragma region Q10_13
return p;
}
void DisplayNumbers(const int* p, unsigned int count)
{
while (count-- > 0) std::cout << *p++ << ' ';
}
void Q10_13() {
const int count{ 40 };
int* nums = GetArrayOfNumbers(count);
DisplayNumbers(nums, count);
delete[] nums;
return pSquarePtrs;
}
void SortSquaresBySide(Square** pSquarePtrs, const int& count)
{
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (pSquarePtrs[i]->side > pSquarePtrs[j]->side ) {
//swap
Square* temp = pSquarePtrs[i];
pSquarePtrs[i] = pSquarePtrs[j];
pSquarePtrs[j] = temp;
}
}
}
}
void DisplaySquaresData(Square** pSquarePtrs, const int& count)
{
Square* pSquare = NULL;
for (int row = 0; row < count; row++) {
// let pSquare point to individual Square object present
pSquare = pSquarePtrs[row];
printf("Side=%.1f, Perimeter=%.2f, Area=%.2f\n",
pSquare->side, pSquare->perimeter, pSquare->area);
}
}
void UseSquares()
{
float side[] = { 4.0,5.7,2.4,3.5 };
Square** pSquarePtrs = CreateSquares(side, 4);
SortSquaresBySide(pSquarePtrs, 4);
DisplaySquaresData(pSquarePtrs, 4);
private:
T* stack;
int tos;
};
struct Player
{
int gameId;
int age;
int pay;
};
void UseStack()
{
// create stack of integers
Stack<int> stackInts(6);
stackInts.Push(5);
stackInts.Push(3);
std::cout << stackInts.Pop() << std::endl;
struct Student{
Student(int a, int i) { age = a; id = i; }
int age;
int id;
};
template<typename T>
struct LLNode //Node of Doubly Linked List
{
std::unique_ptr<T> data; // each node owns its data object provided by the user of DLL
std::shared_ptr<LLNode<T>> next; // pointer to the next node
std::weak_ptr<LLNode<T>> prev; // pointer to the previous node, avoiding cyclic reference
};
template<typename T>
class DLL // doubly linked list
{
public:
DLL(bool (*callback) (T&, T&))
{
// need a callback to ask user of DLL to check for equality
comparer = callback;
}
if (startLL == nullptr)
{
startLL = newNode;
}
else
{
newNode->next = startLL;
startLL->prev = newNode;
startLL = newNode;
}
}
std::unique_ptr<T> Extract(T id) // search for a node, remove from list and return it
{
auto p{ startLL };
std::unique_ptr<T> extract;
while (p != nullptr && p->next != nullptr)
{
T* obj1 = p->data.get();
return extract;
}
private:
std::shared_ptr<LLNode<T>> CreateNode(std::unique_ptr<T> data)
{
// create a node
std::shared_ptr<LLNode<T>> pNode = std::make_shared<LLNode<T>>();
pNode->data = std::move(data); // get exclusive ownership of the data
return pNode;
}
private:
std::shared_ptr<LLNode<T>> startLL; // pointer to start of the node
bool (*comparer)(T&, T&);
};
void UseDLL()
{
DLL<Student> doubleLL(operator==);
s = std::make_unique<Student>(19, 20001022);
doubleLL.Add(std::move(s));
s = std::make_unique<Student>(18, 20001023);
doubleLL.Add(std::move(s));
s = std::make_unique<Student>(19, 20001024);
doubleLL.Add(std::move(s));
std::cout << "Got back from doubly linked list: \n" << "Age: " << s->age << " Id: " << s->id <<
std::endl;
}
#pragma endregion
void main()
{
UseDLL();
}