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 4 of 597
Converting Strings to Numbers in C/C++
In C programming, converting strings to numbers is a common task that can be accomplished using several standard library functions. C provides multiple approaches to convert string representations of numbers into their corresponding numeric values. Syntax int atoi(const char *str); long atol(const char *str); double atof(const char *str); long strtol(const char *str, char **endptr, int base); double strtod(const char *str, char **endptr); Method 1: Using atoi() for Integer Conversion The atoi() function converts a string to an integer value − #include #include int main() { ...
Read MoreConvert a String to Integer Array in C/C++
In C, converting a comma-separated string to an integer array involves parsing the string character by character and extracting numeric values. This process requires traversing the string, identifying delimiters (commas), and converting individual number substrings to integers. Syntax // Function to convert string to integer array void convertStringToArray(char str[], int arr[], int *size); Example: Using Character-by-Character Parsing This approach traverses the string and builds integers by accumulating digits until a comma is encountered − #include #include void convertStringToArray(char str[], int arr[], int *size) { int i = 0, j = 0, num = 0; int len = strlen(str); for (i = 0; i = '0' && str[i]
Read MoreHow to add "graphics.h" C/C++ library to gcc compiler in Linux
In C programming, the graphics.h library provides functions for creating graphics programs. However, this library is not part of the standard C library and requires special installation on Linux systems. Here we'll show how to install and use the graphics library with gcc compiler. Installation Requirements Before using graphics.h, you need to install the required packages and compile the libgraph library on your Linux system. First, install the build essentials and required dependencies − sudo apt-get install build-essential sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev ...
Read MoreHow arrays are passed to functions in C/C++
In this tutorial, we will be discussing how arrays are passed to functions in C. Understanding this concept is crucial for proper array handling in C programming. In C, arrays are never passed by value to functions. Instead, when you pass an array to a function, what actually gets passed is a pointer to the first element of the array. This is called "array decay" − the array name decays into a pointer. Syntax // Method 1: Array notation void function_name(data_type array_name[]); // Method 2: Pointer notation void function_name(data_type *array_name); // Method ...
Read MorePrint 2D matrix in different lines and without curly braces in C/C++
Here, we will see how to print a 2D matrix in C programming language without using curly braces. This technique uses a clever approach to eliminate the need for braces in nested loops. Curly braces are separators in C that define separate code blocks in the program. Without curly braces, defining scopes is difficult, but we can use a shorthand technique to achieve the same result for simple operations. Syntax for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ...
Read MoreBinary Number System - Overflow in Arithmetic Addition in C/C++?
The 2's complement number system is widely implemented in computer architecture for representing signed integers. In an N-bit 2's complement system, numbers can be represented from -2n-1 to 2n-1 - 1. For example − 4-bit system represents numbers from -8 to 7 5-bit system represents numbers from -16 to 15 Overflow occurs when adding two N-bit 2's complement numbers and the result is too large to fit into the N-bit representation. Syntax // Overflow detection formula overflow = carry_in_msb ^ carry_out_msb Understanding Overflow A computer uses N-bit fixed registers. ...
Read MoreBarabasi Albert Graph (for Scale Free Models) in C/C++?
The Barabási-Albert model is one of the most important models for generating scale-free networks. It combines two fundamental concepts: growth and preferential attachment. Growth means that the number of nodes in the network increases over time, while preferential attachment means that highly connected nodes are more likely to receive new connections. This model simulates real-world networks where popular nodes (like well-known websites or influential people) attract more connections than less popular ones, following the "rich get richer" principle. Syntax // Basic structure for BA model implementation typedef struct { int **adjacencyMatrix; ...
Read MoreArrange a binary string to get maximum value within a range of indices C/C++?
In this problem, we have a binary string consisting of only 0's and 1's, along with M non-intersecting ranges [A1, B1], [A2, B2], ..., [AM, BM]. We need to rearrange the string to maximize the sum within the given ranges while ensuring the result is lexicographically maximum. Syntax char* arrangeString(char* binaryStr, int ranges[][2], int m); Algorithm The approach involves two main steps − First, fill all the given ranges with 1's to maximize the sum Then, place remaining 1's from left to right to achieve lexicographical maximum Example 1: Basic ...
Read MoreAn application on Bertrandís ballot theorem in C/C++
Bertrand's ballot theorem states that in an election where candidate A receives p votes and candidate B receives q votes (where p > q), the probability that A is always strictly ahead throughout the counting process is (p-q)/(p+q). This theorem has practical applications in probability theory and combinatorics. Syntax Probability = (p - q) / (p + q) where p > q > 0 Mathematical Example Let there are 5 voters, of whom 3 vote for candidate A and 2 vote for candidate B (so p = 3 and q = 2). Ten possibilities ...
Read MoreAll combinations of strings that can be used to dial a number in C/C++?
Given a phone number, this program generates all possible string combinations that can be formed using the traditional phone keypad mapping. Each digit (2-9) maps to multiple letters, while digits 0 and 1 map to themselves. Syntax void printWords(int number[], int n); Keypad Mapping 2 ABC 3 DEF 4 GHI 5 JKL ...
Read More