Found 1401 Articles for C

Why is a[i] == i[a] in C/C++ arrays?

Arnab Chakraborty
Updated on 03-Jan-2020 11:15:27

233 Views

Here we will see one amazing trick in C or C++. The array subscript A[i] can also be written as i[a]. In C/C++ E1[E2] is defined as (*((E1) + (E2))). The compiler performs arithmetic internally to access the array elements. Because of the conversion of rules, that is applied to the binary + operator, if E1 is an array object, and E2 is an integer, then E1[[E2] signifies the E2th element in the E1 array. So A[B] can be defined as *(A + B), so B[A] = *(B + A). so they are basically the same thing.Example Live Demo#include using ... Read More

Unary operators in C/C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:14:06

2K+ Views

Here we will see what are the unary operators in C / C++. Unary operator is operators that act upon a single operand to produce a new value. The unary operators are as follows.OperatorDescriptionIndirection operator (*)It operates on a pointer variable and returns an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.Address-of operator (&)The unary address-of operator (&) takes the address of its operand. The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared ... Read More

How to sort an array of dates in C/C++?

Arnab Chakraborty
Updated on 03-Jan-2020 10:52:24

731 Views

Suppose we have an array of dates. Here we will see how to sort then using C or C++ code. The dates are stored in a class (struct can be used in C also). We will use the sort function of the C++ STL. For comparing dates, we have to write our own compare function that will be used in the sort function. Let us see the example to get better view.Example Live Demo#include #include #include using namespace std; class Date {    public:       int d, m, y; }; bool compare(const Date &date1, const Date &date2){    if ... Read More

Print colored message with different fonts and sizes in C

sudhir sharma
Updated on 03-Jan-2020 08:05:02

2K+ Views

C/C++ programming language, the user is provided with functionality to customize the output based on the requirement of the user. C/C++ graphics functions are included in graphics.h header file. Using this library you can create different objects, set the color of text, change the font and size of the text and change the background of the output.Now, let's see the working of all the function to alter the text of the output in c/c++ programming language −setcolor() − This function is used to change the color of the output text.Syntaxsetcolor(int)Example#include #include int main(){    int gdriver = DETECT, gmode, i; ... Read More

Binary Search (Recursive and Iterative) in C Program

sudhir sharma
Updated on 26-Jun-2024 23:40:25

70K+ Views

Binary Search is a search algorithm that is used to find the position of an element (target value ) in a sorted array. The array should be sorted prior to applying a binary search. Binary search is also known by these names, logarithmic search, binary chop, half interval search. Working of Binary Search The binary search algorithm works by comparing the element to be searched by the middle element of the array and based on this comparison follows the required procedure. Case 1 − element = middle, the element is found return the index. Case 2 − element > middle, ... Read More

C Program for Activity Selection Problem

sudhir sharma
Updated on 03-Jan-2020 07:08:44

4K+ Views

The activity selection problem is a problem in which we are given a set of activities with their starting and finishing times. And we need to find all those activities that a person can do performing the single activity at a time.The greedy algorithm is appointed in this problem to select the next activity that is to be performed. Let’s first understand the greedy algorithm.Greedy Algorithm is an algorithm that tries to find the solution to a problem by finding the solution step by step. For selecting the next step, the algorithm also selected the step that seems to be ... Read More

C / C++ Program for Subset Sum (Backtracking)

sudhir sharma
Updated on 03-Jan-2020 07:05:07

10K+ Views

Backtracking is a technique to solve dynamic programming problems. It works by going step by step and rejects those paths that do not lead to a solution and trackback (moves back ) to the previous position.In the subset sum problem, we have to find the subset of a set is such a way that the element of this subset-sum up to a given number K. All the elements of the set are positive and unique (no duplicate elements are present).For this, we will create subsets and check if their sum is equal to the given number k. Let's see a ... Read More

Matrix Multiplication and Normalization in C program

Arnab Chakraborty
Updated on 02-Jan-2020 06:18:27

641 Views

Matrix MultiplicationNow procedure of Matrix Multiplication is discussed. The Matrix Multiplication can only be performed, if it satisfies certain condition. Suppose two matrices are P and Q, and their dimensions are P (a x b) and Q (z x y) the resultant matrix can be found if and only if b = x. Then the order of the resultant matrix R will be (m x q).AlgorithmmatrixMultiply(P, Q): Assume dimension of P is (a x b), dimension of Q is (z x y) Begin    if b is not same as z, then exit    otherwise define R matrix as (a ... Read More

C Program for Radix Sort

sudhir sharma
Updated on 24-Dec-2019 06:33:31

12K+ Views

A sorting algorithm is an algorithm that puts components of a listing in a certain order. The most-used orders are numerical order and lexicographic order.The Radix sort is a non-comparative sorting algorithm. The Radix sort algorithm is the most preferred algorithm for the unsorted list.It sorts the elements by initially grouping the individual digits of the same place value. The idea of Radix Sort is to do digit by digit sort starting from least significant digit(LSD) to the most significant digit(MSD), according to their increasing/decreasing order. Radix sort is a small method that is used several times when alphabetizing an ... Read More

C Program for Rabin-Karp Algorithm for Pattern Searching

sudhir sharma
Updated on 24-Dec-2019 06:26:19

2K+ Views

Pattern matching in C − We have to find if a string is present in another string, as an example, the string "algorithm” is present within the string "naive algorithm". If it is found, then its location (i.e. position it is present at) is displayed. We tend to create a function that receives 2character arrays and returns the position if matching happens otherwise returns -1.Input: txt = "HERE IS A NICE CAP"    pattern = "NICE" Output: Pattern found at index 10 Input: txt = "XYZXACAADXYZXYZX"    pattern = "XYZX" Output: Pattern found at index 0    Pattern found at index ... Read More

Advertisements