×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Implementing Mensuration Formula in C

July 06, 2024
Sarah Williams
Sarah Williams
🇨🇦 Canada
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
Ensure you understand the dataset thoroughly before starting your machine learning assignment. Visualize the data, check for missing values, and identify patterns or anomalies to guide your model-building process effectively.
News
In 2024, universities have introduced new programming courses focusing on cybersecurity, machine learning, and artificial intelligence to better prepare students for modern IT careers.

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

`your method is not supported.

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:

> load("chol.RData")

> head(chol)

sexagecholtghtwtsbpdbpvldlhdlldlbmi
id.2M601375068.25111.75110701053742.399066
id.3M2615420282.75184.7588643431922.698040
id.4M3319810864.25147.001208022341323.560993
id.5F271544763.25129.00110765757883.224547
id.6M362127967.50176.2513010016371593.868313
id.7F311979064.50121.001227818581112.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"))

MeanMedianSDNN_miss
age22.661715.461920

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

MeanMedianSDNN_miss
age22.661715.461920
chol198.5717966.641902

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

MeanMedianSDNN_miss
age22.6617.0015.461920
chol198.57179.0066.641902
tg80.3768.0044.631902
ht61.4364.009.291920
wt123.55126.0049.401920
bmi3.103.070.721920

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")

var1var2RP
agewtage0.66600145.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.