Skip to content

Instantly share code, notes, and snippets.

View Gaurav-Singh-97's full-sized avatar

Gaurav Singh Gaurav-Singh-97

View GitHub Profile
void placeQueen(vector<int>& colNo, vector<bool>& usedCol, vector<bool>& rowPlusCol, vector<bool>& rowMinusCol, int row, vector<vector<string>>& result)
{
int n = usedCol.size();
if (row == n)
{
//all queens have been placed
vector<string> board(n, string(n, '.'));
int i;
for (i = 0; i < n; ++i)
@Gaurav-Singh-97
Gaurav-Singh-97 / SharedPtr.cpp
Created November 25, 2017 10:19
Usage of shared_ptr in C++
#include<iostream>
#include<string>
#include<memory>
using namespace std;
class Book
{
string name;
public:
Book(string nm) : name(nm) { cout << "Book created with name \'" << nm << "\'\n"; }
#include<iostream>
#include<vector>
//using namespace std;
bool isPowerOf2 (long long x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
@Gaurav-Singh-97
Gaurav-Singh-97 / Cube_ArraySum.c
Created September 7, 2017 21:46
MPI program in C to find sum of an array of 8 elements using 3-d cube topology
#include<stdio.h>
#include<mpi.h>
int main(int argc, char **argv)
{
int rank=0, n, src, dest;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &n);
@Gaurav-Singh-97
Gaurav-Singh-97 / PrimeFactorisation.hpp
Last active December 14, 2016 13:52
Function for prime factorisation of a number
/*
* Header containing function primeFactorisation which returns std::map of prime factorisation of number
* PROTOTYPE:
* std::map<int, int> GS::primeFactorisation(int num);
* 'num' is the number for which prime factorisation is required
* In the map returned, for each element (i.e. pair), first element is factor and second is its power
* E.g.
* Prime factorisation of 49 = 2 * 7 * 7 = (2^1) * (7^2)
* So, the map would contain pairs (2, 1) and (7, 2),
*/
@Gaurav-Singh-97
Gaurav-Singh-97 / StringToEscSeq.cpp
Created December 2, 2016 17:55
Convert string to corresponding escape seq character (Eg "\n" to '\n')
#include<iostream>
using namespace std;
#include<stdexcept>
#include<string.h>
char strToEscSeq(const char str[])
{
@Gaurav-Singh-97
Gaurav-Singh-97 / Startup_Exit_NonGcc.h
Created December 2, 2016 17:51
Running functions before and after main() (doesn't work on gcc and g++)
#include<stdio.h>
void First();
void Last();
#pragma startup First
#pragma exit Last
void First()
{
@Gaurav-Singh-97
Gaurav-Singh-97 / Startup_Exit.h
Last active December 2, 2016 18:03
Running functions before and after main() (works on gcc and g++)
/*
Usage :
"test.cpp"
----------
#include <iostream>
#define _START_ begin
#define _EXIT_ end
#include "Startup_Exit.h"
@Gaurav-Singh-97
Gaurav-Singh-97 / IsInteger.h
Created December 2, 2016 17:13
Self implemented IsInteger() function in C++
#ifndef IsInteger
#define IsInteger
// Note: Here, RealNumberType should be convertible to int
// and the value contained in RealNumberType should not be greater than
// max value of int
//Maybe IntegerType should only be take as template arguement (seems necessary)
//Original type of arguement might be found using typeinfo or something...(Not sure, check)