0% found this document useful (0 votes)
8 views3 pages

Solved Paper Computer Programming

The document contains a solved paper for Computer Programming using R language, featuring various coding exercises. It includes tasks such as creating vectors, calculating sums, generating random numbers, and performing matrix operations. Additionally, it covers data manipulation techniques like creating data frames and calculating statistical measures.

Uploaded by

Ch Mubashir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Solved Paper Computer Programming

The document contains a solved paper for Computer Programming using R language, featuring various coding exercises. It includes tasks such as creating vectors, calculating sums, generating random numbers, and performing matrix operations. Additionally, it covers data manipulation techniques like creating data frames and calculating statistical measures.

Uploaded by

Ch Mubashir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Solved Paper: Computer Programming

(R Language)
1. 1. Create a numeric vector = c(2,4,6,8,10) and print its sum.

v <- c(2,4,6,8,10)
sum(v)

2. 2. Create a bundled tensor of v = (1:10) and print it.

v <- 1:10
v

3. 3. Generate uniform(0,1) by runif(10).

runif(10)

4. 4. Find area of triangle of sides by Heron’s formula. Sides = (3,4,5).

a <- 3; b <- 4; c <- 5


s <- (a+b+c)/2
area <- sqrt(s*(s-a)*(s-b)*(s-c))
area

5. 5. Calculate frequency of coin flip 100 times.

flips <- sample(c('Head','Tail'), 100, replace=TRUE)


table(flips)

6. 6. Matrix addition of A and B, where A = matrix(1:4, 2x2) and B = matrix(5:8, 2x2).

A <- matrix(1:4, nrow=2)


B <- matrix(5:8, nrow=2)
A + B

7. 7. Create a data frame with 2 columns (Name, Score) and 4 rows.

df <- [Link](Name=c('Ali', 'Sara', 'Omar', 'Zara'),


Score=c(90,85,88,92))
df

8. 8. Calculate two-year sales and perform square matrix multiplication where sales1 =
matrix(c(2,4,6,8),2,2) and sales2 = matrix(c(1,3,5,7),2,2).

sales1 <- matrix(c(2,4,6,8), nrow=2)


sales2 <- matrix(c(1,3,5,7), nrow=2)
sales1 %*% sales2
9. 9. Create a vector of 10 numbers x = c(1,3,5,2,7,4,1,6,8,2) and compute mean where x >
2.5.

x <- c(1,3,5,2,7,4,1,6,8,2)
mean(x[x > 2.5])

10. 10. Find variance & standard deviation of vector (1,3,5,7,9).

v <- c(1,3,5,7,9)
var(v)
sd(v)

11. 11. Write a loop to print numbers from 1 to 10.

for (i in 1:10) print(i)

12. 12. Generate seq(0, 2π, [Link]=10).

seq(0, 2*pi, [Link]=10)

13. 13. Create vectors x = c(10,20,30,40,50) and y = c(1,2,3,4,5), compute x+y.

x <- c(10,20,30,40,50)
y <- c(1,2,3,4,5)
x + y

14. 14. Explain recycling in R.

In R, when vectors of different lengths are operated on, the shorter


vector is recycled (repeated) to match the length of the longer vector.

15. 15. Make a 10x10 matrix of zeros.

matrix(0, nrow=10, ncol=10)

16. 16. Generate Poisson distributed random numbers using rpois(10, lambda=4).

rpois(10, lambda=4)

17. 17. Retrieve population of Pakistan (example given: ~241 million).

pakistan_pop <- 241000000


pakistan_pop

18. 18. Given a dataframe df <- [Link](Name=c('Ali', 'Sara', 'Zara', 'Omar'),


Marks=c(45,55,42,49)), calculate mean of Marks.

df <- [Link](Name=c('Ali', 'Sara', 'Zara', 'Omar'),


Marks=c(45,55,42,49))
mean(df$Marks)
19. 19. On vector x=c(-5:-1,0,1:3), replace negatives with 'neg', zero with 'zero', positives
with 'pos'.

x <- c(-5:-1,0,1:3)
result <- ifelse(x<0,'neg',ifelse(x==0,'zero','pos'))
result

You might also like