Programming Articles - Page 2374 of 3363

MCQ on Memory allocation and compilation process in C

Arnab Chakraborty
Updated on 21-Oct-2019 12:52:48

545 Views

Here we will see some MCQ questions on Memory Allocation and Compilation Processes.Question 1 − What will be the output of the following code − Live Demo#include #include int main() {    union my_union {       int i;       float f;       char c;    };    union my_union* u;    u = (union my_union*)malloc(sizeof(union my_union));    u->f = 20.60f;    printf("%f", u->f); }Options −Garbage Value20.600000Syntax Error20.6ExplanationUsing unions, we can use same memory location to hold multiple type of data. All member of union uses same memory location which has maximum space. Here ... Read More

Maximum Sum SubArray using Divide and Conquer in C++

Ravi Ranjan
Updated on 20-Aug-2025 13:41:56

1K+ Views

In this article, we have an array of integers. Our task is to find the maximum sum of a sub-array using divide and conquer algorithm. A sub-array is a continuous part of an array with consecutive array elements. For example: {2, 3, 4} is sub-array of {1, 2, 3, 4, 5} but {1, 4, 5} is not. What is Divide and Conquer Algorithm? The Divide and Conquer algorithm divides a problem into smaller sub-problems, and then each sub-problem is solved independently. We keep dividing the sub-problems till we reach a stage where no more division ... Read More

Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:25:24

253 Views

Suppose we have a complete graph; we have to count number of Edge Disjoint Spanning trees. The Edge Disjoint Spanning trees are spanning trees, where no two trees in the set have an edge in common. Suppose the N (number of vertices) is 4, then output will be 2. The complete graph using 4 vertices is like below −Two edge disjoint spanning trees are like −The maximum number of edge disjoint spanning tree from a complete graph, with N vertices will be $[\frac{n}{2}]$Example#include #include using namespace std; int maxEdgeDisjointSpanningTree(int n){    return floor(n/2); } int main() {    int n = 4;    cout

Sorting an array according to another array using pair in STL in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:20:28

934 Views

Suppose we have two different arrays. We have to sort one array based on the other array using C++ STL pair class. Consider two arrays are like A1 = [2, 1, 5, 4, 9, 3, 6, 7, 10, 8], and another array is like A2 = [A, B, C, D, E, F, G, H, I, J], The output will be like: A1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], A2 = [B, A, F, D, C, G, H, J, E, I]Here we are using the pairs of C++ STL. The pair is formed by taking one ... Read More

Sort the array of strings according to alphabetical order defined by another string in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:18:01

401 Views

Suppose we have an array of strings, and another string is there for the reference. We have to take the reference string and using the order of the characters in the reference string we will sort the string array. Here we are considering the strings in the array, and the reference string is in lower case letters.Suppose the string array is like: [“hello”, “programming”, “science”, “computer”, “india”], the reference string is like: “pigvxbskyhqzelutoacfjrndmw”, After sorting the output string will be like [“programming”, “india”, “science”, “hello”, “computer”]The task is simple. We have to traverse the reference string, then store the character ... Read More

Sort elements of the array that occurs in between multiples of K in C++

Arnab Chakraborty
Updated on 21-Oct-2019 12:15:15

178 Views

Suppose we have an array A, and another integer K. We have to sort the elements that are in between any two multiples of K. Suppose A is like [2, 13, 3, 1, 21, 7, 8, 13, 12], and K = 2. The output will be [2, 1, 3, 7, 13, 21, 8, 13, 12]. Here multiple of 2 are 2, 8 and 12, the elements in between 2 and 8 are 13, 3, 1, 21, 7, they will be sorted as 1, 3, 7, 13, 21, the elements between 8 and 12 is only 13, so that is already ... Read More

C Program to check if a number is divisible by any of its digits

Sunidhi Bansal
Updated on 21-Oct-2019 11:59:18

2K+ Views

Given a number n, task is to find that any of the digit in the number divides the number completely or not. Like we are given a number 128625 is divisible by 5 which is also present in the number.ExampleInput: 53142 Output: yes Explanation: This number is divisible by 1, 2 and 3 which are the digits of the number Input: 223 Output: No Explanation: The number is not divisible by either 2 or 3The approach used below is as follows −We will start from the unit place and take the unit place’s number.Check whether the number is divisible or ... Read More

C Program for product of array

Sunidhi Bansal
Updated on 21-Oct-2019 11:53:57

10K+ Views

Given an array arr[n] of n number of elements, the task is to find the product of all the elements of that array.Like we have an array arr[7] of 7 elements so its product will be likeExampleInput: arr[] = { 10, 20, 3, 4, 8 } Output: 19200 Explanation: 10 x 20 x 3 x 4 x 8 = 19200 Input: arr[] = { 1, 2, 3, 4, 3, 2, 1 } Output: 144The approach used below is as follows −Take array input.Find its size.Iterate the array and Multiply each element of that arrayShow resultAlgorithmStart In function int prod_mat(int arr[], ... Read More

C Program for Arrow Star Pattern

Sunidhi Bansal
Updated on 21-Oct-2019 11:49:31

2K+ Views

Given a number n and we have to print the arrow star pattern of maximum n number of stars.The star pattern of input 4 will look like −ExampleInput: 3 Output:Input: 5 Output:The approach used below is as follows −Take input in integer.Then print n spaces and n stars.Decrement till n>1.Now increment till n.And print spaces and stars in increasing order.AlgorithmStart In function int arrow(int num)    Step 1-> declare and initialize i, j    Step 2-> Loop For i = 1 and i

C Program for subtraction of matrices

Sunidhi Bansal
Updated on 21-Oct-2019 11:42:24

1K+ Views

Given two matrices MAT1[row][column] and MAT2[row][column] we have to find the difference between two matrices and print the result obtained after subtraction of two matrices. Subtraction of two matrices are MAT1[n][m] – MAT2[n][m].For subtraction the number of rows and columns of both matrices should be same.ExampleInput: MAT1[N][N] = { {1, 2, 3},    {4, 5, 6},    {7, 8, 9}} MAT2[N][N] = { {9, 8, 7},    {6, 5, 4},    {3, 2, 1}} Output: -8 -6 -4 -2 0 2 4 6 8Approach used below is as follows −We will iterate both matrix for every row and column and ... Read More

Advertisements