Found 26504 Articles for Server Side Programming

C Program to print the sum of boundary elements of a matrix

Bhanu Priya
Updated on 24-Mar-2021 14:28:37

6K+ Views

Given a matrix, we need to print the boundary elements of the matrix and display their sum.ExampleRefer the matrix given below −Given matrix1 2 3 4 5 6 7 8 9Boundary Matrix1 2 3 4   6 7 8 9Sum of boundary elements: 1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40The logic to find the sum of boundary matrix is as follows −for(i = 0; i

C program to calculate power of a given number

Bhanu Priya
Updated on 24-Mar-2021 14:14:46

19K+ Views

Take two integers from the user for base and exponent and calculate the power as explained below.ExampleConsider the following for writing a C program.Suppose base =3Exponent = 4Power=3*3*3*3AlgorithmFollow the algorithm given below −Step 1: Declare int and long variables. Step 2: Enter base value through console. Step 3: Enter exponent value through console. Step 4: While loop. Exponent !=0    i. Value *=base    ii. –exponent Step 5: Print the result.ExampleThe following program explains how to calculate power of given number in C language.#include int main(){    int base, exponent;    long value = 1;    printf("Enter a base value: ... Read More

How to assign a column value in a data frame based on another column in another R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:18:25

5K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 ... Read More

How to truncate character vector with three dots after n characters in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:21:59

222 Views

To truncate character vector with three dots after n characters can be done with the help of str_trunc function of stringr package. For example, if we have a character vector say x and each value containing 10 characters then truncating those values with three dots after 5 characters can be done by using the command str_trunc(x, 8).Example1Live Demo> x1 x1Output[1] "rstuvwxyz" "rstuvwxyz" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq" [7] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "abcbefgh" [13] "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq" "ijklmnopq" [19] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" [25] "ijklmnopq" "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" [31] "rstuvwxyz" "abcbefgh" "abcbefgh" "rstuvwxyz" "rstuvwxyz" ... Read More

How to filter single column of a matrix with column name in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:25:51

2K+ Views

To filter a single column of a matrix in R if the matrix has column names, we can simply use single square brackets but this will result in a vector without the column name. If we want to use the column name then column name or column number needs to be passed with drop=FALSE argument as shown in the below examples.Example1Live Demo> M1 colnames(M1) M1Output      V1 V2 V3 V4 [1, ]  0  0  1  0 [2, ]  1  1  1  1 [3, ]  0  0  0  0 [4, ]  0  1  1  0 [5, ]  1  1  1 ... Read More

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:41:48

580 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output

How to split month and year from 6-digit numbers in an R data frame column?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:05:07

477 Views

Sometimes we get data that is not in the form to proceed with the analysis and one such situation is dates stored in 6-digit numbers as 202105 that represents fifth month of year 2021 instead of date format as 2021/05. Therefore, we need to split the date and extract the month and year from the number. This can be done easily with the help of transform function as shown in the below examples.Example1Consider the below data frame −Live Demo> Date Response1 df1 df1Output   Date    Response1 1 202103   0.946367628 2 202103   1.241718518 3 202101  -0.657920816 4 202103  -0.809622853 ... Read More

How to find the row and column index of a character value in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:19:52

5K+ Views

To find the row and column index for a numerical value in an R data frame we use which function and if the value is character then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say Data then we can find the row and column index of Data by using the command as which(df=="Data", arr.ind=TRUE).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output    x1    x2 1 Female  5 2 Female  5 3 Female  6 4 Female ... Read More

C Program to find minimum occurrence of character in a string

Bhanu Priya
Updated on 24-Mar-2021 14:12:33

2K+ Views

An array of characters is called a string.DeclarationFollowing is the declaration for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.Finding minimum occurrenceThe logic to find minimum occurrence of a character in a given string is as follows −for(i=0; i

C program to print array of pointers to strings and their address

Bhanu Priya
Updated on 19-Mar-2021 10:04:52

8K+ Views

First, let us understand what are the arrays of pointers in C programming language.Arrays of pointers: (to strings)It is an array whose elements are ptrs to the base add of the string.It is declared and initialized as follows −char *a[ ] = {"one", "two", "three"};Here, a[0] is a pointer to the base add of string "one".         a[1] is a pointer to the base add of string "two".         a[2] is a pointer to the base add of string "three".AdvantagesThe advantages of array of pointers are explained below −Unlink the two dimensional array of characters, in ... Read More

Advertisements