diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31edd90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.vscode +/**/practice +**.exe diff --git a/Learn-CPP/Day-01/hello.cpp b/Learn-CPP/Day-01/hello.cpp new file mode 100644 index 0000000..e9f3fbd --- /dev/null +++ b/Learn-CPP/Day-01/hello.cpp @@ -0,0 +1,21 @@ +// Let's with the basics of CPP + +// Import a library 😁 + +#include // It's precompiled code, which includes keywords, namespaces + +using namespace std; + +// Main function, it's better to write void as an argument +// It makes program more readable +int main(void){ + + // Print statement, outputs to the console + cout<<"Let's go with g++!!"; + // return 0; // Tells the os, that the program execution was succesfull +} + +// g++ {programFileName}.cpp -o {outputFileName}.(out | exe) +// Run the file +// ./{outputFile} - Powershell / bash +// call {outputFile} - On CMD diff --git a/Learn-CPP/Day-01/hello.out b/Learn-CPP/Day-01/hello.out new file mode 100644 index 0000000..7cfce11 Binary files /dev/null and b/Learn-CPP/Day-01/hello.out differ diff --git a/Learn-CPP/Day-02/alias.cpp b/Learn-CPP/Day-02/alias.cpp new file mode 100644 index 0000000..741bef7 --- /dev/null +++ b/Learn-CPP/Day-02/alias.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; + + +// Typedef is just like defining a new keyword with properties or behaviour of existing keyword +typedef int rollno; // rollno has properties of `int` now + + +// enum is like defining a counter to the given set of items +// By default this counter will start from 0, but we can specify the counter value for specific item, and others will have there counters updated +enum working {mon=1, tue, wed, fri=5, sat, sun}; +// 1 2 3 5 6 7 + +int main(void){ + + rollno myRollNo = 46; // myRollNo is of type `int` only, but this version of code is much more readable + cout<<"Size of 'myRollNo': "< +using namespace std; + +int main(void){ + + char a = 'a'; + char b = 'b'; + + char c = a + b; + + cout<<"Printing directly the sum: "< + +using namespace std; + +int main(void){ + /* + + Primitive Data types: (int, char): Integral, bool, (float, double): Decimal + Data type + signed - Default one , one bit is reserved for the sign + unsigned - All bits are used, minimum number will be 0 + short - Almost half the size + long - Almost double the size (depends on compiler) + + */ + + int var = 5; + signed int svar = 6; + unsigned int uvar = 6; + short int shvar = 6; + long int lvar = 6; + + + cout<<"Size of int: "< + +using namespace std; + +// Instead of writing long long int, now we have to write only ll, to define a long long int +typedef long long int ll; + +int main(void){ + ll myLongInt; + cout< + +// Define namespace +using namespace std; + +// Main function +int main(void){ + int a = 5; // Declare a variable with integer data type + cout<<"Value of a: "<< a; //output above integer + return 0; +} diff --git a/Learn-CPP/Day-03/advanced_conditionals.cpp b/Learn-CPP/Day-03/advanced_conditionals.cpp new file mode 100644 index 0000000..d774f00 --- /dev/null +++ b/Learn-CPP/Day-03/advanced_conditionals.cpp @@ -0,0 +1,14 @@ +#include + +using namespace std; + +int main(void){ + // Comma(,) Operator returns last output + int a=5, b=6, c=7; + if(ac){ + cout<<"This is inside if statement"; + }else{ + cout<<"Though the first expression is true, else is getting processed..."; + } + return 0; +} diff --git a/Learn-CPP/Day-03/menu.cpp b/Learn-CPP/Day-03/menu.cpp new file mode 100644 index 0000000..c1f3298 --- /dev/null +++ b/Learn-CPP/Day-03/menu.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +int main(void){ + string time, name; + cout<<"Enter your name: "; + cin>>name; + + cout<<"What's the section of the day now?(morning, afternoon, eve): "; + cin>>time; + + switch(time){ // May throw, switch quantity is not integer, in CPP version<17 + case "morning": + cout<<"Good morning "< + +using namespace std; + +int main(void){ + + /* + Relational Operators + > : Greater than + < : Less than + >= : Greater than or equal to + <= : Greater than or equal to + == : Is equal + != : Not equal + */ + + int a = 8, b = 7; + + cout<<"a = 8\nb = 7\n"; + cout<<"a > b: "<<(a>b); + cout<<"\na < b: "<<(a= b: "<<(a>=b); + cout<<"\na <= b: "<<(a<=b); + cout<<"\na == b: "<<(a==b); + cout<<"\na != b: "<<(a!=b); + + + /* + Logical Operators + && : AND + || : OR + ! : NOT : Unary Operator + */ + + int c = 5; + cout<<"\n\na>b and a>c (Is a greatest of them): "<<(a>b) && (a>c); + cout<<"\nb>c and b>a (Is b greatest of them): "<<(b>c) && (b>a); + cout<<"\nc>a and c>b (Is a greatest of them): "<<(c>a) && (c>b); + + return 0; +} diff --git a/Learn-CPP/Day-03/switch_case.cpp b/Learn-CPP/Day-03/switch_case.cpp new file mode 100644 index 0000000..f39b24e --- /dev/null +++ b/Learn-CPP/Day-03/switch_case.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +int main(void){ + int choice; + cout<<"Enter your choice: "; + cin>>choice; + + // Compare the value of `choice` variable + switch(choice){ + case 0: + cout<<"Choice 0"< +using namespace std; + +int main(void){ + + int a, b, c, _MAX; + cout<<"Type in 3 integers: "; + cin>>a>>b>>c; + + // Just use ternary operators to minimise the LOC (lines of code) + _MAX = (a>b && a>c)?a:(b>c?b:c); + cout<<"Maximum is: "<<_MAX; + + return 0; +} diff --git a/Learn-CPP/Day-04/factorial.cpp b/Learn-CPP/Day-04/factorial.cpp new file mode 100644 index 0000000..6526e75 --- /dev/null +++ b/Learn-CPP/Day-04/factorial.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +int main(void){ + int n; + unsigned long long int fact=1; + cout<<"Enter n: "; + cin>>n; + + for(int i=2; i<=n; i++) fact*=i; + cout< +using namespace std; + +int main(void){ + int num; + bool isPrime = true; + + cout<<"Enter the number to check: "; + cin>>num; + + + // Check till i*i<=num + for(int i=2; i +using namespace std; + +int main(void){ + int n, _sum = 0; + + cout<<"Enter the number: "; + cin>>n; + + for(int i=1; i<=n; i++) _sum += i; + + // Alternative: cout< +using namespace std; + +int main(void){ + int a, b, c, _max; + + cout<<"Type in 3 integers: "; + cin>>a>>b>>c; + + // Only takes to arguments + _max = max(a, max(b, c)); + cout<<"Greatest of these numbers: "<<_max; + + return 0; +} diff --git a/Learn-CPP/Day-04/perfectNumEff.cpp b/Learn-CPP/Day-04/perfectNumEff.cpp new file mode 100644 index 0000000..23fb917 --- /dev/null +++ b/Learn-CPP/Day-04/perfectNumEff.cpp @@ -0,0 +1,27 @@ +// Trying to find efficient algorithm to find if the number is a Perfect Number +#include +#include +using namespace std; + +typedef long int l; + +bool isPerfect(int num){ + l sum = 1; + for (int i = 2; i < num/2; i++){ + if(num%i == 0){ + sum += i; + if(num/i >= num/2 && num/i != i){ + sum += num/i; + } + } + } + return num==sum; +} + +int main(void){ + assert(isPerfect(6)); + assert(isPerfect(28)); + assert(isPerfect(496)); + assert(!isPerfect(5)); + cout<<"Yupp"; +} diff --git a/Learn-CPP/Day-04/perfectNumber.cpp b/Learn-CPP/Day-04/perfectNumber.cpp new file mode 100644 index 0000000..57a99cd --- /dev/null +++ b/Learn-CPP/Day-04/perfectNumber.cpp @@ -0,0 +1,22 @@ +// Check if the number is a Perfect number + +#include +using namespace std; + +typedef long long int ll; + +int main(void){ + ll sum=1, num; + cout<<"Enter the number: "; + cin>>num; + + // Working, but time inefficient + for(int i=2; i +using namespace std; + +int main(void){ + int n; + cout<<"Enter the base: "; + cin>>n; + + for(int i=1; i<=10; ++i) cout< +using namespace std; + +int main(void){ + + int num = 1; + + // While loop executes till the condition specified is true + while(num<=5) cout< +using namespace std; + +int main(void){ + // arrayName [arraySize] + + int arr[] = {1, 2, 3, 4, 5}; // With size as much as the given initialzers + int arr1[5] = {1, 2, 3}; // Give the values for 3 places, remaining are 0 + int arr2[5] = {}; // All five elements are 0 + // int arr[3] = {1, 2, 3, 4}; // Gives erros, as size of array is 3, but 4 initializers are given + + // print array + for(int i=0; i<5; i++) cout<>arr2[i]; + + return 0; +} diff --git a/Learn-CPP/Day-05/array_que.cpp b/Learn-CPP/Day-05/array_que.cpp new file mode 100644 index 0000000..4632368 --- /dev/null +++ b/Learn-CPP/Day-05/array_que.cpp @@ -0,0 +1,29 @@ +// +#include // Includes most needed libraries, no other needed +using namespace std; + +int main(void){ + int n; + cout<<"What's the size: "; + cin>>n; + + int arr[n] = {}; + + cout<<"Type in all of the elements here separated by space: "; + for(int i=0; i>arr[i]; + } + + // Sort in the increasing order + sort(arr, arr + n); + + // Sort in decreasing order + sort(arr, arr+n, greater()); + + // Print the array + for(int i=0; i +using namespace std; + +int main(void){ + int n; + cout<<"What's the size of your array: "; + cin>>n; + + int arr[n]; + + cout<<"\nDrop in "<>arr[i]; + } + + int sum = 0; + for(int i=0; i +using namespace std; + +typedef long int ll; + +void utility(ll element){ + cout< +using namespace std; + +int main(void){ + int n, b, t; + cin>>t; + + for(int _=0; _>n>>b; + + int arr[n]; + for(int i=0; i>arr[i]; + + sort(arr, arr+n); + + int ptr = 0; + while(ptr=arr[ptr]){ + b -= arr[ptr++]; + } + cout< +using namespace std; + +int main(void){ + int n; + cout<<"Size of array?: "; + cin>>n; + + int arr[n] = {}; + + cout<<"Enter the array elements: "; + for(int i=0; i>arr[i]; + } + for(auto element:arr){ + if(element%2==0 && element>0){ + cout< +using namespace std; + +int main(void){ + int year; + cout<<"Enter the year to check if it's leap year: "; + cin>>year; + + bool isLeap = (year%100==0)?(year%400==0):(year%4==0); + + cout< +using namespace std; + +int main(void){ + int num, front; + cout<<"Enter the number to check: "; + cin>>num; + + while(num>0){ + front = num%10; + } + + return 0; +} diff --git a/Learn-CPP/Day-05/rangeLoop.cpp b/Learn-CPP/Day-05/rangeLoop.cpp new file mode 100644 index 0000000..e33e3f9 --- /dev/null +++ b/Learn-CPP/Day-05/rangeLoop.cpp @@ -0,0 +1,24 @@ +// Range based for loop, we don't need the array size for this +#include +using namespace std; + +typedef long int ll; + +int main(void){ + ll arr[] = {6, 8, 5, 6, 12}; + + ll sum = 0; + + // Range based for loop + // for(type element variable: iterable) + for(int element: arr){ // As the array elements are of int type + sum += element; + } + + for(auto element: arr){ // To automatically fetch the data type of array elements + sum += element; + } + cout< +using namespace std; + +int main(void){ + int a = 7; + cout<<"Value: "< +using namespace std; + +int main(void){ + // numPoints: Total points, points: points on each line(calculated separately for each line) + int numPoints, points; + cout<<"Enter the number of points: "; + cin>>numPoints; + + // Draw first line + // Initial padding is 4 space + cout<<" "; + points = (numPoints-3)/4 + 1; + for(int i=0; i +using namespace std; + +int main(void){ + int numLines, gaps; + cout<<"Enter the number of lines: "; + cin>>numLines; + + for(int i=1; i<=numLines; i++){ + gaps = (numLines - i)*2; + for(int j=0; j +using namespace std; + +int main(void){ + int numLines, gaps; + + cout<<"Enter the number of lines: "; + cin>>numLines; + + cout<<"Pattern 4 - Rhombus: \n"; + for(int i=0; i +using namespace std; + +int main(void){ + + // Number of lines + int numLines; + cout<<"Enter the number of lines: "; + cin>>numLines; + + for(int i=0; i +using namespace std; + +int main(void){ + int numLines; + bool one; + + cout<<"Enter the number of lines: "; + cin>>numLines; + + cout<<"Pattern 2: \n"; + for(int i=0; i +using namespace std; + +int main(void){ + int var = 7; + + // Create pointer with * before the variable name, and assign the address of a variable to it + int *ptr = &var; + + cout<<"Value of var: "< +using namespace std; + +int main(void){ + int arr[5] = {4, 2, 6, 0, 15}; + int *arrPtr = arr; + + for(int i=0; i<(sizeof(arr)/sizeof(int)); i++){ + // Below ptr has changed the value + // cout<<"\narr["< Always initialize `pointer` with `NULL` or `nullptr`(introduced in **c++11**) +```cpp + int *ptr1 = NULL; + int *ptr2 = nullptr; +``` + +3. After freeing the memory, pointer is still pointing to some location, which is known as `dangling pointer`(pointing to the empty location) +```cpp + int *m = new int[2]; + free(m); + m = nullptr; +``` + +4. You can now `delete` the pointer directly +```cpp +int* ptr = new int[2]; +delete(ptr); +```