Posts

Showing posts with the label cpp

Top 5 Programming language need to learn in 2020

Image
Top 5 Programming Languages need to learn in 2020 If you're new to software development. The first question that comes to mind is where to start? This is probably true! There are hundreds of choices. How are you going to discover that this is the language I want to learn? What will be the most appropriate for you, your interests and your professional goals? One of the easiest ways to choose the best programming language to learn in 2020 is to listen to what the market says. Where the technology trend is heading ... What to learn in 2020 1. JavaScript Without Javascript, you cannot think of interactive web pages. That is the main reason JavaScript is so much popular among software developers. Today, it seems impossible to be a software developer without the use of JavaScript. First from the list is JavaScript, it seems impossible to imagine software development without JavaScript. In Stack Overflow 2018 for developers, JavaScript is the most widely used by developers in t

Second Equation of motion, How to implement using C language

Image
The second equation of motion is used to calculate the displacement of an object under constant acceleration. Algorithm:  Without wasting any of your precious time let's try to understand how the algorithm is implemented. want to check the implementation of the Third equation of motion,   click here. The second equation of motion is s = u*t + 1/2 * a * t * t s: total displacement u: initial velocity t: time taken by the journey a: constant acceleration as you can see in our program we are considering  u=0  if you want you can set initialVelocity variable value as per your choice or you can ask it from user too also acceleration value is 9.8 equal to the gravity of earth. want to check the implementation of the first equation of motion,   click here. In a function  fallingDistance  we have implemented the equation. total_dist = 9.8 * i * i * 1/2 + u * i ; total_dist = (4.9 * i * i) + (u * i); Code: #include // this is the implementation of 2nd equations of motion. void fallingDista

First Equation of motion, How to implement using C language

Image
The first equation of motion is used to calculate the final velocity of an object under constant acceleration. Algorithm:  Without wasting any of your precious time let's try to understand how the algorithm is implemented. The first equation of motion is want to check the implementation of Second equation of motion ,  click here. v = u + a * t u: initial velocity t: time taken by the journey a: constant acceleration as you can see in our program we are considering u=0 if you want you can set initialVelocity variable value as per your choice or you can ask it from user too, accel=5 and timeOfJourney=7. want to check the implementation of the Third equation of motion,   click here. In function, finalVelocityWithConstantAcceleration we have implemented the equation. final_velocity = u * a * i Code: #include // this is the implementation of 1st equations of motion. void finalVelocityWithConstantAcceleration(int accel,float u,int xtime){ float final_velocity=0; printf("Tim

Creating Square, Rectangle, Multiply, slash using * (star) using C

Image
In the process of learning the basic of C language people often create shapes using the *, and these shapes question are also asked in the interview question and selection exam of various companies so it is very good to learn how to make various shapes. Rectangle we can also make multiply etc. Multiply Code for the above images it is in the order of images. #include <iostream> using namespace std; int main() { int size; cout << "Enter Number "; cin >> size; cout << endl; for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ cout << "*"; } cout << endl; } cout << endl; for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ if(i == j) cout << "*"; else cout << " "; } cout << endl; } cout << endl; for(int i=0;i<size;i++)

Selection Sort using C language (with every step)

Image
What is selection sorting The selection algorithm sorts an array by repeatedly finding the smallest element (considering increasing or ascending order) from the unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given group. 1) Subarray that is already sorted. 2) Remaining subarray that is unsorted. At each iteration of selection, the minimum value (with regard to rising order) is sorted from the unsorted subarray and moved to the sorted subframe. Code #include <stdio.h> void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void selectionSort(int a[], int n) { int i, j, minIndex; printf("sorting array of length %d...\n",n); // n-1 changed to n for (i = 0; i < n; i++) { // in selection sort we find the smallest element // from array and put it aside, then we // find the next smallest in the remaining array // again, and go on and go on // pri

Calculating Julian Day Number (JDN) using C++

Image
what is Julian Day Number Julian Day is the continuous number of days since the beginning of the Julian period and is mainly used by astronomers and in software to easily calculate past days between two events (eg, food production date and sales by date). Code #include<iostream> #include <limits> using namespace std; void input_data(int &month, int &year, int &day,int &bmonth, int &byear, int &bday); void calculations(int month, int year, int day, int &jdn); void output_results(int &month, int &year, int &day, int &jdn,int d); void calculate_dow(int &jdn , int &dow); void calculate_difference_and_print(int jdn , int bjdn); void pause(); int main() { int bday; int bmonth; int byear; int bjdn; int day; int month; int year; int jdn; int dow; int bdow; input_data(month, year, day,bmonth, byear, bday); // calculate JDN for the present day calculations(month, year, day, jdn); // calculat

Bubble Sort in C language

Image
Bubble sort is the simplest Sorting algorithm available. It works by repeatedly swapping the adjacent or neighbor element if they are in the wrong order. Working Actually in first pass bubble sort sets the largest element to its position, in the second pass algorithm sets the second largest element this continues till the list or array is not finally sorted. Here is the example image. To work with the algorithm you first have to understand what is swapping and how it is done. What is Swapping ?? Swapping is the technique to interchange the content of the two variables, using another variable called temporary variable. For the program in C #include <stdio.h> void bubbleSort(int s[],int size){ int c,swap,d; for (c = 0 ; c < size - 1; c++) { for (d = 0 ; d < size - c - 1; d++) { if (s[d] > s[d+1]) /* For decreasing order use < */ { swap = s[d]; s[d] = s[d+1]; s[d+1] = swap

How to Create Linked List in C/C++

Here is the Code to create linked list #include <stdio.h> #include <stdlib.h> struct Node{ int data; struct Node *next; }; typedef struct Node *node; node createNewNode(){ node temp; temp = (node)malloc(sizeof(struct Node)); // allocate memory equal memory as the size using malloc() temp->next = NULL;// make next point to NULL (suppose this as last node) return temp; } node addNode(node head, int value){ node tmp,p; tmp = createNewNode(); tmp->data = value; if(head == NULL){ head = tmp; } else{ p = head; while(p->next != NULL){ p = p->next; } p->next = tmp; } return head; } int main() { node head = NULL; head = addNode(head,2); head = addNode(head,3); head = addNode(head,5); head = addNode(head,8); node p; p = head; while(p != NULL){ printf("%d\n",p->data); p = p->next; } ret

Basics of Pointers In C

Image
Pointers in C A pointer is a variable which contains the address of the variable specified. for example:  int x = 100; int *data; data = &x; this variable data contains the address of the block in the memory which contains the value 100. * specifies that this is the pointer variable. & gives the address. that is the what I am doing here assigning the address of the integer variable x to the pointer variable data. to get the data from the pointers we have to use the * sign. for the previous example to get the data of the memory block of the printf("%d\n",*data );  this should print the output like the image. Further digging into deep for the pointers. we can use them for the array. if we change content in the memory block pointed by the data, then this change should reflect the in the content of the x. Because variable x has the content of that memory block. (this is the great advantage of the using the pointers) we can read, change anything without copying the data fr

How to Get the length of an Array in C

Image
Array in C an array is a collection of a variable of a fixed number of the same data type in a sequential memory. an array can be declared in various ways. one of them is given below int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; int n[10],j; // first declaring the array // then assigning the value. for ( j = 0; j < 10; j++ ) { n[ j ] = j + 100; } indexing in C array starts from the 0. so if you want to access the 4th element then you have to use x[3]. that's enough intro about the array. Getting Length of the Array in C to get the length of the array first we divide the size of the full array by the size of the one element of the array. Please note there is another way of getting the length of the char array (you can use strlen). you can see this in the given program. #include <stdio.h> int main(){ int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; // determining the size of the array // getting the

C/C++ program to check the Palindrome string.

Image
Palindrome string: A string or word which are same from the start and end means word or program which look like same as original and reversed. To do that we have to first reverse the string. Check out our post on the String reverse in C/C++. To write the program of the Palindrome steps used are. Get the input from the user. convert the string to the Lower case to avoid any case sensitivity. apply the logic of the reverse the string. compare the reversed and original string, if same then the string is a palindrome. if different string is not a palindrome. print the result to the user. without wasting much time lets start. First of all the C schema. #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char const *argv[]) { return 0; } ctype.h is used to get the advantage of the function tolower() later in the program. Now take the input from the user. char name[150],rev[150] ; int l=0; printf("Enter a string to check palindrome\n");

C/C++ program to Reverse a String

Image
String In C language string is an Array of the Char variable. to write a program of the string reverse we have to break this program into steps as always. Take input from the user. Reverse the string using the for loop print the final answer to the user. First of All, create a schema for the program. #include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { return 0; } Now write some code for the Taking input from the user. scanf is fine here but keep in mind it can take input up to first space. char name[150],rev[150] ; int l=0; printf("Enter a string to reverse\n"); // taking input from the user // string should not have the space in between scanf("%s",&name); This code will take a string from the user, now implement the main logic. what we are doing here is that first, we are counting the length of the string and then storing the last variable into the first element of another variable. l = strlen(name); // main logic

String Splitting in C++ (using find and substr)

Image
String Splitting in C++ Hello guys, you all have known the string splitting. Basically, string splitting is nothing but dividing the string based on a Delimiter or special character.  For example, example@gmail.com by the delimiter looks like [example,gmail.com]   use of string splitting is very broad as we can use this to tokenize thing in the web crawlers, data analysis in the data to extract useful things etc. without wasting much time let's start splitting. First of all create a schema for the c++ file. #include <iostream> #include <string> using namespace std; int main() { return 0; } Now let's take a string that is to be split. I took "codeincafeinweb". take a delimiter, for example, I took "in". and one more string variable for tokens. string.find() method returns the starting position of the delimiter, if it founds nothing then it will return -1. That is why we are using the while with terminating condition of -1. string name = "

Installing C/C++ (GCC) compiler On Windows

Image
Installing the C/C++ (GCC) Compiler On windows. In this Tutorial, I am gonna show you the full process of installing the C and C++ compiler or known as GCC to your windows machine. Without wasting much time Lets Start Installing. A good thing about the tdm-gcc is that it automatically sets up the path variables, we need not worry about the path variables anymore. In order to install the GCC software, you have to first Download it from the internet. you can also download this software from their Website by clicking here . Download you will see a page like this. just download any one from the versions that is suited for your machine. my machine is x64 so I have downloaded the 64 bit. Now go to the Download directory of your machine and search for the program and double-click it to open or run. Installing you will see something like this. click on the Create like this  Now select the type of installation as x86 or x64. and click next Now click next to accept their Privacy policy. No

Easy File handling in C++ (working with files, fstream)

Image
In this tutorial, we are working with the files in the C++ or you have known to work with files as the file handling.  File handling in any language requires Three main step to follow Open the file in Read/Write mode. Do various operation that you want to do. Close the file. So In CPP, you can also use the traditional approach of C using FILE pointer and etc. We are using fstream (file stream). Without wasting much time lets start working. Create a New CPP file of whatever name you want mine is files.cpp. create the basic schema of cpp. #include <iostream> using namespace std; int main() { return 0; } To work with file add two more header first is #include<fstream>  , and second is #include<string> #include <fstream> #include <string> Now, let's start working with the writing into the files, to write into the file we need to open the file into the Write mode.  Which can be done by the ofstream class of cpp from the fstream header. string