# Implementing Mensuration Formula in C

July 06, 2024
Sarah Williams
C
Sarah is a skilled C programmer with a bachelor's degree in computer science and over 600 completed orders. Specializing in data structure implementation, she delivers meticulously crafted solutions that leverage arrays, linked lists, trees, and hash tables for effective mapping operations.
Key Topics
• Circumference and other mensuration formula implemented in C
Tip of the day
News

## Circumference and other mensuration formula implemented in C

Problems 1:

Write a C assignment function, calCircle, to perform the following task:

Calculate the area of a circle (AC = πr2), the circumference of a circle (CC = 2πr), the volume of a sphere (V S = 4 πr3), or the area of a sphere (AS = 4πr2).

The first argument is either “AC”, “CC”, “VS”, or “AS” to determine which calculation needs to be performed

The value that the first argument takes can contain either lower, upper, or mixed cases of letters (Use the toupper function).

If the values of the argument are not “AC”, “CC”, “VS”, or “AS”, stop the function and write

The second argument is the radius (r), which is a numeric vector of length great or equal to 1.

Make sure to use the if ... else statement for this problem.

The function returns a numeric vector.

Here’re the expected results by calling this function:

> calCircle('ac',4)

[1] 50.265

> calCircle('Vs',1:5)

[1] 4.189 33.510 113.097 268.083 523.599

rm(list = ls())

```## Defining the function. We shall use the if condition to check for the ## method and will use stop criteria if none of them are met. calCircle <- function( method, r ) { if(toupper(method) == "AC") return(pi*r^2) if(toupper(method) == "CC") return(2*pi*r) if(toupper(method) == "VS") return(4*pi*r^3/3) if(toupper(method) == "AS") return(4*pi*r^2) stop("your method is not supported") } calCircle('ac',4) calCircle('Vs',1:5) ```

Problems 2:

Write a function, calCircle2, to perform the following task:

Calculate the area of a circle, the circumference of a circle, the volume of a sphere, and/or the area of a sphere.

The first argument is a character vector that contains values of “AC”, “CC”, “VS”, and/or “AS”. The value can contain either lower, upper, or mixed cases of letters. The calculations need to be performed based on the first argument.

The second argument is a numeric vector of length great or equal to 1.

The function returns a list that contains the calculated results. The length of the list is the same as the length of the first argument.

Here’re the expected results by calling this function:

> calCircle2(c('cc'), 1:4)

\$CC

[1] 6.283 12.566 18.850 25.133

> calCircle2(c('AC', 'VS'), seq(5,25,5))

\$AC

[1] 78.540 314.159 706.858 1256.637 1963.495

\$VS

[1] 523.599 4188.790 14137.167 33510.322 65449.847

> calCircle2(c('AC', 'VS', "cc", "aS"), 3:10)

\$AC

[1] 28.274 50.265 78.540 113.097 153.938 201.062 254.469 314.159

\$VS

[1]

113.097 268.083 523.599

904.779 1436.755 2144.661 3053.628 4188.790

\$CC [1]

18.850 25.133 31.416 37.699

43.982 50.265 56.549 62.832

\$AS [1]

113.097 201.062 314.159

452.389 615.752 804.248 1017.876 1256.637

```## Here we will use Sapply as we need to preserve the name of the list ## We could have used the lapply but the names in the list can't be preserved. calCircle2 <- function( task, r ) { task = as.character(task) return(sapply(task, calCircle, r = r, simplify = F, USE.NAMES = T )) } calCircle2(c('cc'), 1:4) calCircle2(c('AC', 'VS'), seq(5,25,5)) calCircle2(c('AC', 'VS', "cc", "aS"), 3:10) ```

Problems 3:

You will work with the data frame, chol, for this problem:

 sex age chol tg ht wt sbp dbp vldl hdl ldl bmi id.2 M 60 137 50 68.25 111.75 110 70 10 53 74 2.399066 id.3 M 26 154 202 82.75 184.75 88 64 34 31 92 2.698040 id.4 M 33 198 108 64.25 147.00 120 80 22 34 132 3.560993 id.5 F 27 154 47 63.25 129.00 110 76 57 57 88 3.224547 id.6 M 36 212 79 67.50 176.25 130 100 16 37 159 3.868313 id.7 F 31 197 90 64.50 121.00 122 78 18 58 111 2.908479

Write a function, table1, to perform the following task:

Calculate the descriptive statistics for each numeric variable, including mean, median, standard deviation, the number of non-missing values, and the number of missing values.

The first argument is a data frame.

The second argument is a character vector that contains the names of the variables.

The returned value is a matrix that contains numeric values.

For the values of mean, median, or standard deviation, you can round them to two decimal places in the result.

The expected results look like following

> table1(chol, c("age"))

 Mean Median SD N N_miss age 22.66 17 15.46 192 0

>table1(chol, c("age", "chol"))

 Mean Median SD N N_miss age 22.66 17 15.46 192 0 chol 198.57 179 66.64 190 2

>table1(chol,c("age","chol","tg","ht","wt","bmi"))

 Mean Median SD N N_miss age 22.66 17.00 15.46 192 0 chol 198.57 179.00 66.64 190 2 tg 80.37 68.00 44.63 190 2 ht 61.43 64.00 9.29 192 0 wt 123.55 126.00 49.40 192 0 bmi 3.10 3.07 0.72 192 0

```load("chol.RData") head(chol) ## First we would create a fun function to get all the metrics. funn <- function(x) { return(c( "Mean" = mean(x),"Median" = median(x),"SD" = sd(x), "N" =length(x), "N_miss" = sum(is.na(x)) )) } ## Now we will get the final function just by applying the funn function on each columns table1 <- function( df, cols ) { df_work = df[cols] return(t(sapply(df_work, funn, simplify = "array"))) } table1(chol, c("age")) table1(chol, c("age", "chol")) table1(chol, c("age", "chol", "tg", "ht", "wt", "bmi")) ```

Problems 4:

You will use the data frame, chol, for this problem.

To test the correlation between two continuous variables, you can use the cor.test function. For example:

> age_chol <- cor.test(chol\$age, chol\$chol)

> str(age_chol)

List of 9

\$ statistic : Named num 3.13

..- attr(*, "names")= chr "t"

\$ parameter : Named int 188

..- attr(*, "names")= chr "df"

\$ p.value : num 0.00202

\$ estimate : Named num 0.223

..- attr(*, "names")= chr "cor"

\$ null.value : Named num 0

..- attr(*, "names")= chr "correlation"

\$ alternative: chr "two.sided"

\$ method : chr "Pearson's product-moment correlation"

\$ data.name : chr "chol\$age and chol\$chol"

\$ conf.int : num [1:2] 0.0829 0.3538

..- attr(*, "conf.level")= num 0.95

- attr(*, "class")= chr "htest"

To extract the correlation coefficient and the corresponding p-value, you can write the following:

> age_chol\$estimate

cor 0.2226466

> age_chol\$p.value

[1] 0.002018013

Write a function named myCorTest, which is used to calculate the pairwise correlation between one variable with a list of given variables. This function takes three arguments:

dat: the name of the data frame, such chol.

mainVar: a character vector of length 1 that contains the name of a continuous variable. For example "wt". You will calculate the correlation between this variable with each of the variables in the third argument.

varlist: a character vector contains one or more values. This argument contains the names of a continuous variable.

The function will return a data frame that contains the correlation coefficient and the corresponding p-value between each pair. For example, here are some sample results that are based on the myCortest function:

> myCortest (chol, "wt", "age")

 var1 var2 R P age wt age 0.6660014 5.631448e-26

```age_chol <- cor.test(chol\$age, chol\$chol) str(age_chol) ## We shall use loop here for each vars in varlist myCortest <- function( df, mainVar, varlist ) { var1 = rep(mainVar, length(varlist)) R = c() p = c() for(var in varlist) { object = cor.test(unlist(df[mainVar]), unlist(df[var])) R = c(R, object\$estimate) p = c(p, object\$p.value) } final_dat <- data.frame(var1, "var2" = varlist, "R" = R, "p" = p) rownames(final_dat) <- varlist return(final_dat) } myCortest (chol, "wt", "age") myCortest (chol, "wt", c("age", "chol", "tg", "ht")) myCortest (chol, "bmi", c("sbp", "dbp", "vldl", "hdl", "ldl")) ```

## Related Samples

Explore our free C assignment samples designed to help you understand key concepts and improve your skills. These examples offer clear, detailed solutions, making complex topics easier to grasp and aiding in your academic success.