Posts

Compiler Design (Exp 1)

Image
Compiler Design (Exp 1) Task -  To print the count of the number of keywords, identifiers, operators, and symbols after reading a line of code as input, then store each of these in an array and display them as an output. Logic -  Accept STRING input using getline(). Make a function 'to_vector()' to store each element of the input as a vector element. Call each function keyword(), identifier(), operator() and symbols(). Traverse the vector elements for each function call and see if there is a particular match. Increase the count and display as required. Declare global vector variables to store these details separately. #include <bits/stdc++.h> using namespace std ; vector < string > keys; vector < string > oper; vector < string > ident; vector < string > syms; vector < string > to_vector ( string input ) {     vector < string > temp;     int k = 0 ;     for ( int i = 0 ;i < input . length ();i ++ )     {         string part

Function in C++ || Project OPTIMIZATION

Image
  Function in C++ Functions are used to perform certain actions, and they are important for reusing code : Define the code once and use it many times. It also enables programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task. For our projects, we will start off by performing the task below. Try to do this on your own before seeing the final answer. Create a deposit function Declare a variable inside the function with a value of 1000. Accept user integer input. Add to the existing variable of 1000. Display the final result. For the other function, Create a withdraw function Declare a variable inside the function with a value of 1000. Accept user integer input. Subtract from the existing variable of 1000. Display the final result. #include<bits/stdc++.h> using namespace std ; int  Deposit () {      int  existing_amount =  1000 , amt ;     cout<<endl<< "Enter how much to Deposit - " ;     cin>>amt ;  

The if-else STATEMENTS in C++

Image
  The if-else STATEMENTS in C++ The if-else statements can control the flow of the code . For example, if you are 10 and only 10, you can play this game or else if you are 13 and only 13, you can play gta4 or else if you are 18+ , you can play any game else , you can't play From this, you can try and first guess, which ages won't be able to play games . Only ages 10,13 and 18+ can play at least some game. However, 9 and below and 11,12,14-17 can not play any games. This is how the flow control in a code works, now keep in mind, the syntax is if (condition) {} else if (condition) {} else {} Also, keep in mind, a==5 checks if a is 5 and returns true or false. a=5 initializes the value 5 to a. Try to print the output like this #include<bits/stdc++.h> using namespace std ; int  main () {     string username ;     cout<< "Enter your username - " ;     cin>>username ;          cout<<endl ;          cout<< "Welcome to ABC Online Bankin

Basic Syntax & First Code Build

Image
Basic Syntax & First Code Build Here is the syntax list for beginners to know how to start coding. Header Files #include<bits/stdc++.h> Function Syntax cin>> cout<< endl Data Type int string char double float Structure #include<bits/stdc++.h> using namespace std ; int  main () { //**ALL MAGIC HERE** --> Commented using '//' } The header file given above is usually enough for any code since it is a combined file of almost all C++ header files . The cin must have >> and cout must have  <<   and all code usually ends with a " ; " int accepts integer numbers between  -2,147,483,648 to 2,147,483,647 . string accepts inputs like agh3487bkk. You can guess and then check what the other data types do . With this knowledge, try and see if you can generate this output, if you just got started in C++, then see the code and get the idea and then try it on your own. #include<bits/stdc++.h> using namespace std ; int  main () {     

C++ Mindset & Understanding

Image
C++ Mindset & Understanding Welcome to the 45-minute course for C++ from 0 to PROJECT ready . In this segment of the article which happens to be the first video & read, we will cover the basic syntax in C++. To start with, you need to know what are the different types of syntaxes you will be learning in the next video & why they are in a particular order. We will also start working on our very own PROJECT. So not only will you finish a C++ course, but you will also have some experience. Code structure. Every code that will be written will have to follow certain patterns like Proper header files Proper functions The main function Proper declaration of variables Proper logic Respective Syntax Input Output Loops Manipulations Storage Others Now, how are each of them useful? Let's take a look at a sample project idea. Consider the banking management system . The intricacies of a banking system are so huge that it would compel us to think out of the box and uniquely. We will

Finding Factorial in C++ || Recursion

Image
 Finding Factorial in C++ || Recursion The logic and code are pretty simple for this. Considering that we don't have to do any exception handling. We get the input n. We return 1 if the input is 0 or 1. If the input is any other number (positive only) then we do recursion. The recursion is when we multiply the present number by the previous number and run the same function with this new result that we have. For example, try a dry run on your won for 5, See how it works for n=3 Iteration 1 in the function 3 != 1 or 0, so else case  return 3*factorial(2)    2 != 1 or 0, so else case return 3*2*(factorial(1))  1 == 1, so if-case return 3*2*1 = 6 So this is the logic behind the code. Here is the code, have fun, mess around and see what new things you can find in this.  #include<bits/stdc++.h> using namespace std ; int  factorial ( int  n ) {      if ( n== 1 ||n== 0 )          return   1 ;      else          return  n*factorial ( n -1 ); } int  main () {      int  n ;     cin>&

To check if input is a PALINDROME in C++

Image
Palindrome Checking in C++ Let's break this question down first. What is expected from us? We are to see if any input given to us is a palindrome or not. Now we consider the input to be proper, which means no exception handling will be required just yet. Now we need to check if the given input is the same whether read from left to right or from right to left. What is the logic? The inputs can be either string or int, so let's just go with the string data type , since it can also deal with int-based data. Now, we declare for loops, one will POINT to the first element and the SECOND will point to the last element. Let us have a conditional increment of the for loops, this way, the complexity of the code reduces. The algorithm! If the first and last elements are the same, then move to the second and last second element. If the second and last second elements are the same, then move to the third and last third elements. Do this until the pointers cross over. In any case, if the con