Server Side Programming Articles

Page 940 of 2109

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 399 Views

In C programming, there's an interesting feature where array subscript notation a[i] can also be written as i[a]. This happens because of how C internally handles array indexing through pointer arithmetic. Syntax a[i] == i[a] // Both expressions are equivalent How It Works In C, E1[E2] is defined as (*((E1) + (E2))). The compiler performs pointer arithmetic internally to access array elements. Because the binary + operator is commutative, a[i] becomes *(a + i), and i[a] becomes *(i + a). Since addition is commutative, both expressions evaluate to the same memory ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 1K+ Views

In C programming, sorting an array of dates requires creating a structure to represent dates and implementing a custom comparison function. We use the qsort() function from the standard library to sort the array based on chronological order. Syntax void qsort(void *base, size_t num, size_t size, int (*compare)(const void *, const void *)); Method 1: Using qsort() with Custom Comparator This approach uses a structure to store date components and a comparison function that compares dates chronologically − #include ...

Read More

Print colored message with different fonts and sizes in C

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 3K+ Views

In C programming, the graphics.h library provides functions to customize text output with different colors, fonts, and sizes. This library is primarily used in graphics programming to create visually appealing text displays on the screen. Note: The graphics.h library requires Turbo C/C++ or compatible compilers. For modern compilers, you may need to install graphics libraries like WinBGIm or use alternative approaches. Key Graphics Functions for Text Formatting 1. setcolor() Function The setcolor() function changes the color of the output text − Syntax void setcolor(int color); Example #include ...

Read More

C Program for Activity Selection Problem

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 6K+ Views

The activity selection problem is a classic optimization problem where we are given a set of activities with their starting and finishing times. The goal is to select the maximum number of activities that can be performed by a single person, given that only one activity can be performed at a time. This problem is efficiently solved using a greedy algorithm approach. The greedy algorithm makes locally optimal choices at each step, hoping to find a global optimum. It selects activities based on their finishing times in ascending order. Syntax void activitySelection(int start[], int finish[], int ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 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 such that the elements 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). Syntax void subset_sum(int s[], int t[], int s_size, int t_size, int sum, int index, int target_sum); ...

Read More

Matrix Multiplication and Normalization in C program

Arnab Chakraborty
Arnab Chakraborty
Updated on 15-Mar-2026 945 Views

In C programming, matrix multiplication and normalization are fundamental operations in linear algebra. Matrix multiplication follows specific rules where the number of columns in the first matrix must equal the number of rows in the second matrix. Matrix normalization scales each row so that its Euclidean norm equals 1. Syntax // Matrix Multiplication for (i = 0; i < rows1; i++) { for (j = 0; j < cols2; j++) { for (k = 0; k < cols1; k++) { ...

Read More

C Program for Naive algorithm for Pattern Searching

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 8K+ Views

Pattern matching in C is the process of finding if a string (pattern) is present within another string (text). For example, the string "algorithm" is present within the string "naive algorithm". If found, its location (starting position) is displayed. The Naive algorithm is a simple brute-force approach that checks every possible position in the text. Syntax void naivePatternSearch(char text[], char pattern[]); Algorithm The Naive Pattern Searching algorithm works as follows − naive_algorithm(pattern, text) Input − The text and the pattern Output − locations where the pattern is present in the text ...

Read More

C Program for Minimum number of jumps to reach the end

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 719 Views

The minimum jumps problem involves finding the minimum number of jumps needed to reach the last index of an array from the first index. Each element in the array represents the maximum number of steps that can be taken forward from that position. Syntax int minJumps(int arr[], int n); Algorithm The naive approach starts from the first element and recursively calculates the minimum jumps needed − minJumps(start, end) = Min(minJumps(k, end)) for all k accessible from start We use dynamic programming with bottom-up approach. For each position, we check all ...

Read More

C Program for Minimum Cost Path

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 1K+ Views

The minimum cost path problem involves finding the path from the top-left corner to the bottom-right corner of a 2D matrix with minimum total cost. You can only move right or down from any given cell. Dynamic programming provides an efficient solution to this problem. Syntax int minCostPath(int cost[][COLS], int m, int n); Problem Approach The solution uses dynamic programming to build a table where each cell contains the minimum cost to reach that position from the starting point (0, 0). The formula for any cell (i, j) is − dp[i][j] = ...

Read More

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

Sunidhi Bansal
Sunidhi Bansal
Updated on 15-Mar-2026 635 Views

In C programming, a common issue occurs when using scanf() followed by fgets(), gets(), or another scanf() for character input. The problem arises because scanf() leaves a newline character in the input buffer, which interferes with subsequent input functions. Syntax scanf("format_specifier", &variable); fgets(string, size, stdin); Problem 1: scanf() Followed by fgets() When scanf() reads an integer and is followed by fgets(), the newline character left by scanf() is immediately consumed by fgets() − #include int main() { int x; char str[100]; ...

Read More
Showing 9391–9400 of 21,090 articles
« Prev 1 938 939 940 941 942 2109 Next »
Advertisements