Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 959 of 2547
Print colored message with different fonts and sizes in C
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 MoreC Program for Activity Selection Problem
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 MoreC / C++ Program for Subset Sum (Backtracking)
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 MoreMatrix Multiplication and Normalization in C program
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 MoreC Program for Naive algorithm for Pattern Searching
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 MoreC Program for Minimum number of jumps to reach the end
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 MoreC Program for Minimum Cost Path
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 MoreProblem with scanf() when there is fgets()/gets()/scanf() after it in C
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 MoreProcess Synchronization in C/C++
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. Syntax /* General structure for process synchronization */ do { entry_section(); critical_section(); exit_section(); remainder_section(); } while(1); The Critical-Section Problem Every process has a reserved segment ...
Read MorePrinting source of a C program itself
In C programming, a self-printing program (also called a quine) is a program that prints its own source code as output. This can be achieved using the __FILE__ macro along with file I/O operations to read and display the current source file. Syntax FILE *fopen(__FILE__, "r"); The __FILE__ macro is a preprocessor macro that returns the name of the current source file as a string literal. This allows us to open and read our own source code at runtime. Example: Understanding __FILE__ Macro First, let's see how the __FILE__ macro works − ...
Read More