Found 1339 Articles for C

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

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

12K+ 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

838 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

14K+ 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

3K+ 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

C Program for Naive algorithm for Pattern Searching

sudhir sharma
Updated on 24-Dec-2019 06:18:02

8K+ 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 2 character 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 ... Read More

C Program for Minimum number of jumps to reach the end

sudhir sharma
Updated on 24-Dec-2019 06:12:27

642 Views

We are given, an array of non-negative integers denoting the maximum number of steps that can be made forward from that element. The pointer is initially positioned at the first index [0 index] of the array. Your goal is to reach the last index of the array in the minimum number of steps. If it is not possible to reach the end of the array then print the maximum integer.naive approach is to begin from initial{the primary} component and recursively call for all the components accessible from the first element. The minimum range of jumps to reach the end from ... Read More

C Program for Minimum Cost Path

sudhir sharma
Updated on 24-Dec-2019 06:06:27

886 Views

Here, we will solve the minimum cost path problem in C. The implication is done on a 2D-matrix where each cell has a cost to travel. We have to find a path from the left top corner to the bottom right corner with minimum travel cost. You can only traverse down and right lower cells from a given cell.To solve this particular problem dynamic programming is much better to approach than recursion.Given cost matrix cost[ ][ ] and a position (m, n), we have to write a function that returns cost of minimum path to reach (m, n) from (0, ... Read More

Problem with scanf() when there is fgets()/gets()/scanf() after it in C

Sunidhi Bansal
Updated on 23-Dec-2019 11:37:34

550 Views

The problem states that what will be the working or the output if the scanf is followed by fgets()/gets()/scanf().fgets()/gets() followed by scanf()Example Live Demo#include int main() {    int x;    char str[100];    scanf("%d", &x);    fgets(str, 100, stdin);    printf("x = %d, str = %s", x, str);    return 0; }OutputInput: 30 String Output: x = 30, str =Explanationfgets() and gets() are used to take string input from the user at the run time. I above code when we run enter the integer value then it won’t take the string value because when we gave newline after the integer ... Read More

Process Synchronization in C/C++

Sunidhi Bansal
Updated on 23-Dec-2019 11:33:09

7K+ Views

Process synchronization is the technique to overcome the problem of concurrent access to shared data which can result in data inconsistency. A cooperating process is the one which can affect or be affected by other process which will lead to inconsistency in processes data therefore Process synchronization is required for consistency of data.The Critical-Section ProblemEvery process has a reserved segment of code which is known as Critical Section. In this section, process can change common variables, update tables, write files, etc. The key point to note about critical section is that when one process is executing in its critical section, ... Read More

Printing source of a C program itself

Sunidhi Bansal
Updated on 23-Dec-2019 11:01:17

643 Views

Given the task is to print the written C program itself.We have to write a C program which will print itself. So, we can use file system in C to print the contents of the file of which we are writing the code, like we are writing the code in “code 1.c” file, so we open the file in the read mode and read all the contents of the file and print the results on the output screen.But, before opening a file in read mode, we must know the name of the file we are writing code in. So, we ... Read More

Advertisements