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
C++ Articles
Page 6 of 597
C/C++ Program for Greedy Algorithm to find Minimum number of Coins
A greedy algorithm is an algorithmic approach used to find an optimal solution for the given problem. Greedy algorithm works by finding locally optimal solutions (optimal solution for a part of the problem) of each part so that the global optimal solution can be found. In this problem, we will use a greedy algorithm to find the minimum number of coins/notes that could makeup to the given sum. For this we will take under consideration all the valid coins or notes i.e. denominations of { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000 }. We need to ...
Read MoreDifference between C and C++
Both C and C++ are middle-level programming languages that are used for developing system software as well as application software. C is a procedural programming language which have low-level memory access and minimal runtime; therefore, it is used for writing operating systems, embedded systems, and system-level programs. whereas C++ is just an extension of the C language, which is both a procedural programming language and object-oriented. Therefore, having extra features that make it suitable for game development, GUI applications, and high-performance software. C Programming Language C is a general-purpose, procedural programming language, which was developed by Dennis ...
Read MoreAlternate vowel and consonant string in C/C++?
We are given a string with both vowels and consonants. Our task is to rearrange it so that vowels and consonants appear alternately, while keeping their original order within their groups. This rearrangement is only possible if the number of vowels and consonants is equal, or their difference is exactly one. If multiple valid arrangements are possible, we return the one that is lexicographically smaller. Syntax char* alternateVowelConsonant(char* str); bool isVowel(char ch); Example Scenarios Scenario 1 Input: "objective" Output: "obejctive" Explanation: Vowels = [o, e, i, e], Consonants = [b, j, ...
Read MoreC/C++ Program for Finding the Number Occurring Odd Number of Times?
In C programming, finding the number that occurs an odd number of times in an array is a common problem. Given an array where all elements appear an even number of times except one, we need to identify that unique element. Consider the array [1, 2, 1, 3, 3, 2, 2]. Here, the number 2 appears 3 times (odd), while others appear even times. Example Scenarios Input: arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7} Output: 7 The number 7 appears 3 times (odd frequency). Input: arr[] = {2, 3, 2, 1, ...
Read MoreC/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2
This program finds the sum of a mathematical series where the n-th term is defined as Tn = n2 - (n-1)2. We need to calculate the sum Sn = T1 + T2 + T3 + ... + Tn modulo (109 + 7). Syntax result = ((n % mod) * (n % mod)) % mod; Mathematical Derivation First, let's simplify the term Tn − Tn = n2 - (n-1)2 Tn = n2 - (n2 - 2n + 1) Tn = n2 - n2 + 2n - 1 Tn = 2n - 1 ...
Read MoreAdd n binary strings?
In C programming, adding n binary strings involves performing binary addition on multiple binary numbers represented as strings. We need to add all binary strings together to produce a single binary result. The approach uses binary addition logic with carry propagation, adding all strings one by one to get the final result. Syntax char* addBinaryStrings(char* binary1, char* binary2); char* addNBinaryStrings(char** binaryArray, int n); Example: Adding Multiple Binary Strings This example demonstrates adding three binary strings using a helper function that adds two binary strings at a time − #include #include ...
Read MoreArray Manipulation and Sum using C/C++
In this problem, you are given an integer array arr of size n and an integer S. Your task is to find an element k in the array such that if all the elements greater than k in the array are replaced with k, then the sum of all the elements of the resultant array becomes equal to S. If such an element exists, print it; otherwise, print -1. Syntax int getElement(int arr[], int n, int S); Algorithm To solve this problem efficiently, we follow these steps − Sort the array in ...
Read MoreAddition and Subtraction of Matrix using pthreads in C/C++
Matrix addition and subtraction using pthreads allows us to perform operations on large matrices efficiently by utilizing multiple threads. Each thread handles a portion of the matrix, enabling parallel computation and improved performance. To compile and run pthread programs, you need to link with the pthread library using: gcc -pthread program.c -o program Syntax pthread_create(&thread_id, NULL, function_name, (void*)argument); pthread_join(thread_id, NULL); Example: Matrix Operations with Pthreads This example demonstrates matrix addition and subtraction using multiple threads. Each thread processes one row of the matrices − #include #include #include ...
Read MoreC/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
Here we will see how to get the sum of the series with n-th term as n2 − (n−1)2. The recurrence relation is like below − Tn = n2 − (n−1)2 So the series is − S = T₁ + T₂ + T₃ + ... + Tₙ S = (1² - 0²) + (2² - 1²) + (3² - 2²) + ... + (n² - (n-1)²) S = 1 + 3 + 5 + ... + (2n-1) = n² We need to find S mod (109 + 7), ...
Read MoreA C/C++ Pointer Puzzle?
This C programming puzzle demonstrates how pointer arithmetic works with different types of pointers and multi-dimensional arrays. Understanding pointer sizes and type differences is crucial for mastering pointer arithmetic in C. Syntax sizeof(pointer_variable) (char*)(pointer + 1) - (char*)pointer Example: Pointer Arithmetic Puzzle Let's analyze this step-by-step with a complete C program that demonstrates pointer arithmetic with various pointer types − #include int main() { int a[4][5][6]; int x = 0; int* a1 = &x; ...
Read More