Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions basic-metaprogramming-malik/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

GCC = g++

CFLAGS = -std=c++17

main: main.cpp
$(GCC) $(CFLAGS) main.cpp -o main

clean:
rm -f main
31 changes: 31 additions & 0 deletions basic-metaprogramming-malik/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# C++ template madness

[C++ template madness](https://github.com/HackGT/challenge0002-submissions/ ) challenge submission

# Description
Implemented the meta programs to determine the architecture (x86 or x64) of the computer, If-Else conditions using templates, For loop with range, and finally Matrix addition, subtraction, and addition using metaprogramming with operator overloading. It took some time to figure out matrix multiplication using templates with overloading operators.



# Compiling and Running
To run the code clone this repository and run the following commands:
```bash
cd basic-metaprogramming-malik/
make
./main
```

# Requirements
G++ >= 9

# Author

Name: Malik Naik <br/>

Email: [maliknaik16@gmail.com](mailto:maliknaik16@gmail.com) / [mmoham25@students.kennesaw.edu](mailto:mmoham25@students.kennesaw.edu) <br/>

Portfolio: [http://maliknaik.me/](http://maliknaik.me/)<br/>

LinkedIn: [https://www.linkedin.com/in/maliknaik/](https://www.linkedin.com/in/maliknaik/)<br/>

28 changes: 28 additions & 0 deletions basic-metaprogramming-malik/architecture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#pragma once
#ifndef CHALLENGE_ARCHITECTURE_H_
#define CHALLENGE_ARCHITECTURE_H_

#include <string>
#include <iostream>

template<std::size_t B = sizeof(void*)>
struct Architecture
{
// constexpr static const unsigned short int value = Architecture<(B == 0) ? sizeof(void*) : B>::value;
constexpr static const unsigned short int value = Architecture<B>::value;
};

template<>
struct Architecture<4>
{
constexpr static const unsigned short int value = 32;
};

template<>
struct Architecture<8>
{
constexpr static const unsigned short int value = 64;
};

#endif
28 changes: 28 additions & 0 deletions basic-metaprogramming-malik/conditionals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#ifndef CONDITIONALS_H_
#define CONDITIONALS_H_

#include "type_defs.h"

template <bool condition>
struct If {};

template <>
struct If<true>
{
static inline void run(func t, func f)
{
t();
}
};

template <>
struct If<false>
{
static inline void run(func t, func f)
{
f();
}
};

#endif
48 changes: 48 additions & 0 deletions basic-metaprogramming-malik/loops.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#include <iostream>
#include <type_traits>
#include "type_defs.h"

template<int i>
class Loop {
public:
static inline void execute()
{
std::cout << "Item #" << i << std::endl;
Loop<i - 1>::execute();
}
};

template<>
class Loop<0> {
public:
static inline void execute()
{
}
};

template<class T>
constexpr T MaxValue(T a, T b)
{
return (a > b) ? a : b;
}

template<int start, int increment, int end>
class ForLoop {
public:
static inline void execute()
{
std::cout << "Item #" << start << std::endl;
ForLoop<start + increment, increment, MaxValue<int>(end - increment, 0)>::execute();
}
};

template<int start, int increment>
class ForLoop<start, increment, 0> {
public:
static inline void execute()
{

}
};

68 changes: 68 additions & 0 deletions basic-metaprogramming-malik/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

#include <iostream>
#include "architecture.h"
#include "matrix.h"
#include "conditionals.h"
#include "loops.h"

using namespace std;

void x86()
{
cout << "Running on x86 architecture..." << endl;
}

void x64()
{
cout << "Running on x64 architecture..." << endl;
}

int main()
{
If<Architecture<>::value == 32>::run(x86, x64);

cout << "Printing 10 items:" << endl;
ForLoop<0, 1, 10>::execute();

Matrix<int, 3, 3> a, b, c;
Matrix<float, 2, 2> d, e, f;
Matrix<int, 3, 2> g, h, i;

a.SetMatrix({{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}});
b.SetMatrix({{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}});

cout << "Adding Matrix A and B:" << endl;
c = a + b;


h.SetMatrix({{{1, 2}, {4, 5}, {7, 8}}});

cout << "Dimension of A and H Matrix is: " << endl;
cout << a.GetNumRows() << " x " << a.GetNumCols() << endl;
// cout << e.GetNumRows() << " x " << e.GetNumCols() << endl;
cout << h.GetDim()[0] << " x " << h.GetDim()[1] << endl;

cout << "Multiplying Matrix A and H:" << endl;
g = a * h;
g.Show();

cout << "New Matrix Dimension after multiplying A and H is: " << endl;
cout << g.GetDim()[0] << " x " << g.GetDim()[1] << endl;

d.SetIdentityMatrix();
e.SetOnes();

d.Show();
e.Show();
cout << "Adding Matrix D and E:" << endl;
f = d + e;
f.Show();

cout << "Subtracting Matrix D and E:" << endl;
f = d - e;
f.Show();

cout << ((a == b) ? "Matrix A is equal to B" : "Matrix A is not equal to B") << endl;

return 0;
}
Loading