Skip to content

Instantly share code, notes, and snippets.

View RahulBRB's full-sized avatar
💭
Learning DSA

R A H U L RahulBRB

💭
Learning DSA
View GitHub Profile
@RahulBRB
RahulBRB / search_element_in_array.cpp
Created September 13, 2022 08:58
Use this code to find the position of an element inside the array.
#include<iostream>
int myIndex(int numbers[], int size, int element);
int main(){
int numbers[] = {1,2,3,4,5,6,7,8,9,0};
int size = sizeof(numbers)/sizeof(numbers[0]);
int index;
int element;
@RahulBRB
RahulBRB / bubblesort.cpp
Created September 13, 2022 09:45
This is the code for performing bubble sorting in C++
#include <iostream>
void sort(int array[], int size);
int main()
{
int array[] = {10, 1, 9, 2, 8, 3, 7, 4, 6, 5};
int size = sizeof(array)/sizeof(array[0]);
sort(array, size);
@RahulBRB
RahulBRB / fill_function.cpp
Created September 13, 2022 10:02
Code to fill 60 elements in an array, in such a way that first 20 elements are "Pizza", next 20 elements are "Burgers", and the last 20 elements are "Hotdog".
#include<iostream>
int main(){
const int SIZE = 60;
std::string foods[SIZE];
fill(foods, foods + (SIZE/3), "Pizza");
fill(foods+(SIZE/3), foods + (SIZE/2), "Burgers");
fill(foods+(SIZE/2), foods + SIZE, "Hotdog");
@RahulBRB
RahulBRB / array_user_input.cpp
Created September 13, 2022 17:47
To fill up an array with user's input
#include<iostream>
int main(){
std::string food[10];
int size = sizeof(food)/sizeof(food[0]);
std::string temp;
for(int i=0;i<size;i++){
std::cout << "Enter the foods you like # or type 'q' if you're done. " << i + 1 << ": ";
@RahulBRB
RahulBRB / 2d_array.cpp
Created September 13, 2022 18:03
Example code for displaying a 2d array
#include<iostream>
int main(){
// rows . columns
std::string colours[][3] = {{"Black","White","Blue"},
{"Orange","Purple","Green"},
{"Yellow", "Red", "Brown"}};
int rows = sizeof(colours)/sizeof(colours[0]);
int columns = sizeof(colours[0])/sizeof(colours[0][0]);
@RahulBRB
RahulBRB / constants.cpp
Created September 13, 2022 18:06
Variable, data types, and constants in c++
#include<iostream>
int main(){
const double PI = 3.14159; // cant change val later
const int LIGHT_SPEED = 299792458;
const int WIDTH = 1920;
const int HEIGHT = 1080;
double radius = 10;
double circumference =2 * PI * radius;
@RahulBRB
RahulBRB / main.cpp
Created September 13, 2022 19:14
Pass by Value VS Pass by Reference
#include <iostream>
void swap(std::string &x, std::string &y);
int main()
{
std::string x = "Kool-Aid";
std::string y = "Water";
swap(x, y);
@RahulBRB
RahulBRB / main.cpp
Created September 13, 2022 19:14
Memory addresses
#include <iostream>
int main() {
// memory address = a location in memory where data is stored
// a memory address can accessed with & (address-of operator)
std::string name = "BOB";
int age = 21;
bool student = true;
@RahulBRB
RahulBRB / code.cpp
Created September 16, 2022 09:50
Intro to Dynamic memory
#include <iostream>
// Dynamic mem: Memory that is allocated after the
// program is already compiled and running.
// Use the "new" operator to allocate memory
// in the heap rather than the stack
/* Useful when we dont know how much memory we will need.
Makes our programs more flexible, specially when
accepting user input. */
@RahulBRB
RahulBRB / factorial.cpp
Created September 16, 2022 10:07
Factorial using iterative approach
#include<iostream>
int factorial(int num);
int main(){
std::cout << factorial(5);
return 0;
}