C File
C File
By-Kashish Arora
B.sc(hons) Statistics
PRACTICAL 14
AIM: To conduct F-test using f-scanf and f-printf
PROBLEM: Two random samples are drawn from Normal populations and
observations are given. Obtain estimates of the variances of the populations. Then
test whether the populations have equal variances. Note: • Use data in FMS page
16.56 Problem 16.35 • File Read operation required: Input data stored in a file. Use
𝑓𝑠𝑐𝑎𝑛𝑓(…) • File Write operation required: Print your results into another output file.
Use 𝑓𝑝𝑟𝑖𝑛𝑡𝑓(…)
THEORY: fprint and fscanf are functions in C language used for input and output
operations on files.
- stream: Pointer to the FILE object that identifies the stream where the output is to
be written.
- format: Format control string, similar to the format strings used in `printf`,
containing the text to be written and format specifiers.
Additional arguments corresponding to the format specifiers in the `format` string.
2. fscanf: It is used to read formatted input from a file. Its syntax is:
- stream: Pointer to the FILE object that identifies the stream from which the input
is to be read.
- format: Format control string, similar to the format strings used in `scanf`,
specifying how the input should be interpreted.
Pointers to variables where the values read will be stored, according to the format
specifiers in the `format` string.
Both functions are part of the Standard Input/Output Library (`stdio.h`) in C, and they
operate similarly to their counterparts `printf` and `scanf`, respectively, but with files
instead of the standard input and output streams.
CODE:
// This is Practical 14
#include <stdio.h>
/*
OUTPUT FOR PRACTICAL 14:
^Z
F(11,9) test statistics is 2.14
*/
PRACTICAL 13
AIM: To develop a function in C to fit a Poisson distribution to the given data
(𝑥𝑖,𝑓𝑖)(xi,fi) for 𝑖=1,2,…,𝑛i=1,2,…,n, where 𝑥𝑖xi represents the observed values and
𝑓𝑖fi represents the frequencies.
PROBLEM: Develop a function to fit a Poisson distribution to the given data {(𝑥𝑖 , 𝑓𝑖
)|𝑖 = 1,2, … , 𝑛}. Hence, demonstrate its use in the 𝑚𝑎𝑖𝑛( ) for given data.
X 0 1 2 3 4 5 6 7 8 Total
f 56 156 132 92 37 22 4 0 1 500
THEORY:
The Poisson distribution is a discrete probability distribution that expresses the
probability of a given number of events occurring in a fixed interval of time or space,
𝑒^(−𝜆)⋅𝜆^𝑘
given the average rate of occurrence. It is given by the formula: P(X=k)= 𝑘!
CODE:
// This is Practical 13
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define E 2.71828182846
*/
PRACTICAL 15
Aim:
The aim of this program is to develop a function in C to draw a random sample of
one observation from the Exponential distribution expo(θ). Additionally, the program
will draw a sample of size 10 for the parameter values θ = 0.5 and θ = 1.5.
PROBLEM:
Theory:
The Exponential distribution with parameter θ has a probability density function
(PDF) given by:
𝐹(𝑥∣𝜃)=1−𝑒−𝑥𝜃F(x∣θ)=1−e−θx
𝑋=−𝜃ln(1−𝑈)X=−θln(1−U)
lm = 1.5;
random_sample(sample,n,lm);
print_arr(sample,n);
return 0;
}
/*
OUTPUT FOR PRACTICAL 15:
^Z
0.4161 0.2538 3.6231 3.6234 0.0337 1.4703 0.5423 2.9429 6.8872 4.9648
0.9412 1.4264 0.3218 0.1618 0.5937 0.9685 0.9931 0.3635 0.3894 0.2825
*/
PRACTICAL 16
AIM: To fit a gamma distribution sample.
PROBLEM: Develop a function to draw one random sample from the Gamma
distribution with parameters 𝑆ℎ𝑎𝑝𝑒 = 𝛼 (𝑖𝑛𝑡𝑒𝑔𝑒𝑟) and 𝑆𝑐𝑎𝑙𝑒 = 𝜆 (𝑝𝑜𝑠𝑖𝑡𝑖𝑣𝑒 𝑟𝑒𝑎𝑙).
Hence draw a sample of size 𝑛 = 10 for the parameter values 𝛼 = 3,𝜆 = 1.5.
CODE:
// This is Practical 16
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
random_sample(sample,n,lm,a);
print_arr(sample,n);
return 0;
}
/*
OUTPUT FOR PRACTICAL 16:
^Z
1.4310 1.7091 3.4575 4.0226 1.0773 2.3251 1.0792 3.3976 1.7030 0.9984
*/
PRACTICAL 17
AIM: The aim of this program is to develop a function in C to draw one random
sample from the Standard Normal distribution 𝑁(0,1) using the Central Limit
Theorem (CLT).
PROBLEM:
Develop a function to draw one random sample from the Normal distribution 𝑁(0,1)
by using the central limit theorem (CLT). Hence draw a sample of size 𝑛 = 10
THEORY: The Central Limit Theorem states that the sum of a large number of
independent and identically distributed random variables, suitably normalized, will be
approximately normally distributed. We can exploit this theorem to generate a
sample from a normal distribution with mean 0 and standard deviation 1, which is
also called the Standard Normal distribution.
To generate a random sample from 𝑁(0,1) using the CLT, we can take the average
of a large number of uniform random variables and then standardize them.
Specifically, if 𝑈1,𝑈2,…,𝑈𝑛U1,U2,…,Un are independent and identically distributed
uniform random variables on [0, 1], then 𝑍=1𝑛∑𝑖=1𝑛(𝑈𝑖−0.5)Z=n1∑i=1n(Ui−0.5) will
have a distribution that converges to 𝑁(0,1) as 𝑛n approaches infinity.
/*
OUTPUT FOR PRACTICAL 17:
^Z
0.0753 -0.8045 -0.7129 -0.7103 0.2700 0.5111 -1.7773 1.9762 -0.5153
0.2053
*/
PRACTICAL 6
AIM: To fit a binomial distribution using recursive function.
THEORY: Recursive functions are often used to solve problems that can be broken
down into smaller, similar subproblems.
1. Base Case: Every recursive function must have a base case, which is a condition
under which the function stops calling itself recursively. Without a base case, the
recursion would continue indefinitely, leading to a stack overflow.
2. Recursive Case: In addition to the base case, recursive functions also have a
recursive case, where the function calls itself with modified arguments to solve a
smaller version of the same problem. This process continues until the base case is
reached.
3. Termination: Recursive functions terminate when the base case is met, at which
point the function stops calling itself and returns a value. Each recursive call returns
control to the previous call once the base case is reached.
Recursive functions can be elegant and concise solutions for certain problems, but
they require careful consideration of the base case and the recursive case to ensure
termination and correctness. Additionally, excessive recursion can lead to stack
overflow errors, so it's essential to use recursion judiciously.
CODE:
// This is Practical 6
#include <stdio.h>
#include <math.h>
float binom(int x, int n, float p){
if(x == 0){
float res = pow((1-p),n);
printf("For X = %d, p = %.2f The probability is %f\n",x,p,res);
return (float)res;}else{
float val = (((n-x+1)*p)/(x*(1-p)));
float res = val*binom(x-1,n,p);
printf("For X = %d, p = %.2f The probability is %f\n",x,p,res);
return res;}
}
int main(){
int n = 5;
int x = n;
binom(x,n,0.25);
binom(x,n,0.5);
binom(x,n,0.75);
return 0;
}
/*
OUTPUT FOR PRACTICAL 6:
For X = 0, p = 0.25 The probability is 0.237305
For X = 1, p = 0.25 The probability is 0.395508
For X = 2, p = 0.25 The probability is 0.263672
For X = 3, p = 0.25 The probability is 0.087891
For X = 4, p = 0.25 The probability is 0.014648
For X = 5, p = 0.25 The probability is 0.000977
For X = 0, p = 0.50 The probability is 0.031250
For X = 1, p = 0.50 The probability is 0.156250
For X = 2, p = 0.50 The probability is 0.312500
For X = 3, p = 0.50 The probability is 0.312500
For X = 4, p = 0.50 The probability is 0.156250
For X = 5, p = 0.50 The probability is 0.031250
For X = 0, p = 0.75 The probability is 0.000977
For X = 1, p = 0.75 The probability is 0.014648
For X = 2, p = 0.75 The probability is 0.087891
For X = 3, p = 0.75 The probability is 0.263672
For X = 4, p = 0.75 The probability is 0.395508
For X = 5, p = 0.75 The probability is 0.237305
*/
PRACTICAL 7
AIM: To write a program in C that generates Poisson distribution probabilities using
the recursive expression
Note: (a) The following recursive form is more convenient to write the code: 𝑝(𝑥; 𝜆) =
𝜆 𝑥 𝑝(𝑥 − 1; 𝜆), 𝑥 = 1,2,3, … and 𝑝(0; 𝜆) = 𝑒 −𝜆 .
(c) Terminate computing the next value of 𝑝(𝑥) using the accuracy |1 − ∑ 𝑝(𝑘) 𝑥 𝑘=0
| < 10−4 = 0.0001
CODE:
#include <stdio.h>
#include <math.h>
#define E 2.71828
float poisson(int x,float lm)
{
float res;
static float sum_total =0;
if(x==0)
{
float res0;
res0 = pow(E,(-lm));
sum_total += res0;
printf("For x = %d, p(x;lm = %.2f) = %f\n",x,lm,res0);
return(res0);
}
res = (lm/(float)x)*poisson(x-1,lm);
sum_total+= res;
/*
OUTPUT FOR PRACTICAL 7:
For x = 0, p(x;lm = 0.10) = 0.904837
For x = 1, p(x;lm = 0.10) = 0.090484
For x = 2, p(x;lm = 0.10) = 0.004524
*/
PRACTICAL 8
AIM:
The aim of the program is to generate Poisson distribution probabilities using a
recursive expression, specifically the formula 𝑝(𝑥 + 1; 𝜆) = 𝜆 / (𝑥 + 1) * 𝑝(𝑥; 𝜆), 𝑥 =
0,1,2, … with 𝑝(0; 𝜆) = 𝑒^−𝜆. The program should test this calculation for different
values of 𝜆, such as 0.01, 0.1, and 10.0.
PROBLEM:
Write a program to generate Poisson distribution probabilities using the recursive
expression 𝑝(𝑥 + 1; 𝜆) = 𝜆 𝑥+1 𝑝(𝑥; 𝜆), 𝑥 = 0,1,2, … with 𝑝(0; 𝜆) = 𝑒 −𝜆 . Test your
program using 𝜆 = 0.01, 0.1, 10.0 Note: (a) The following recursive form is more
convenient to write the code: 𝑝(𝑥; 𝜆) = 𝜆 𝑥 𝑝(𝑥 − 1; 𝜆), 𝑥 = 1,2,3, … and 𝑝(0; 𝜆) = 𝑒 −𝜆
. (b) Develop a function in a separate file and use the static global feature available
in C. (c) Terminate computing the next value of 𝑝(𝑥) using the accuracy |1 − ∑ 𝑝(𝑘) 𝑥
𝑘=0 | < 10−4 = 0.0001
THEORY:
The Poisson distribution is often used to model the number of events occurring in a
fixed interval of time or space. It is characterized by a single parameter 𝜆 (lambda),
which represents the average rate of occurrence of the events. The probability mass
function of the Poisson distribution is given by 𝑝(𝑥; 𝜆) = (𝜆^𝑥 * 𝑒^−𝜆) / 𝑥!, where 𝑥 is
the number of events that occur and 𝑥! is the factorial of 𝑥.
CODE:
// This is Practical 8
#include <stdio.h>
int main()
{
poisson(100,0.01);
printf("\n");
poisson(100,0.1);
printf("\n");
poisson(100,10.0);
printf("\n");
return(0);
}
//file2
#include <stdio.h>
#include <math.h>
#define E 2.71828
AIM: To develop a function sort(int *x, int n) that reorders an array of integers into
ascending order using the Bubble sort algorithm
THEORY: The program will demonstrate the use of this function in the main()
function to sort an array of random numbers 𝑋∼𝑈[0,50]X∼U[0,50] (integers). It will
display the sorted sequence of numbers along with the numbers in the intermediate
steps after each swap operation. The program will also calculate and display the
total number of swaps. The Bubble sort algorithm is a simple sorting algorithm that
repeatedly steps through the list, compares adjacent elements, and swaps them if
they are in the wrong order. The pass through the list is repeated until the list is
sorted.
CODE:
#include <stdio.h>
#include <stdlib.h>
int swaps_no;
int random2(int x)
{
float i = (rand()/(float)RAND_MAX);
return (i*x);
}
void gen_rand(int *x,int n,int k){
int i;
for(i = 0; i <n; i++){
x[i] = random2(k+1);
}
}
void print_arr(int *x,int n){
int i;
for(i = 0; i<n;i++){
printf("%d ",x[i]);
}
printf("\n");
}
void swap(int *a, int *b){
int temp;
temp = *b;
*b = *a;
*a = temp;
swaps_no++;
}
void sort(int *x,int n){
int i,j;
for(i=0;i<n-1;i++){
for(j = 0; j <n-1-i;j++){
if( (x[j]) > (x[j+1])){
swap(&x[j],&x[j+1]);
print_arr(x,n);
}
}
}
}
int main(){
int n=8;
int k = 51;
int x[8];
int i;
for(i = 0; i <n; i++){
x[i] = random2(k+1);
}
sort(x,n);
print_arr(x,n);
printf("\nNo of swaps are: %d\n",swaps_no);
return(0);
}
/*
OUTPUT FOR PRACTICAL 9:
^Z
20 43 40 41 47 10 17 39
20 40 43 41 47 10 17 39
20 40 41 43 47 10 17 39
20 40 41 43 10 47 17 39
20 40 41 43 10 17 47 39
20 40 41 43 10 17 39 47
20 40 41 10 43 17 39 47
20 40 41 10 17 43 39 47
20 40 41 10 17 39 43 47
20 40 10 41 17 39 43 47
20 40 10 17 41 39 43 47
20 40 10 17 39 41 43 47
20 10 40 17 39 41 43 47
20 10 17 40 39 41 43 47
20 10 17 39 40 41 43 47
10 20 17 39 40 41 43 47
10 17 20 39 40 41 43 47
10 17 20 39 40 41 43 47
No of swaps are: 17
*/
Practical 10
AIM: Develop a function to calculate Mean and hence another function to calculate
Variance of Raw data {𝑥𝑖 for 𝑖 = 1,2, … 𝑛}. Test these by calling from a 𝑚𝑎𝑖𝑛( )
function with real data type.
Theory:
int main(){
int n = 5;
float x[5];
int i;
for(i = 0; i <n; i++){
x[i] = 110+ (5*rand())/(RAND_MAX+1.0);
}
print_arr(x,n);
printf("Mean of the data is %f\n",mean(x,n));
printf("Variance of the data is %f\n",variance(x,n));
return(0);
}
PROBLEM: Develop a function to calculate Mean and hence another function to calculate
Variance of grouped frequency data {(𝑥𝑖 , 𝑓𝑖 ) for 𝑖 = 1,2, … 𝑛}. Test these by calling from a
𝑚𝑎𝑖𝑛( ) function with 𝑓𝑙𝑜𝑎𝑡 data type.
f 7 6 19 35 30 23 7 1
THEORY:
Mean (Average): The mean of grouped frequency data is calculated using the
formula: Mean=∑𝑖=1𝑛𝑥𝑖𝑓𝑖∑𝑖=1𝑛𝑓𝑖 where 𝑥𝑖xi is the midpoint of the ith class
interval and 𝑓𝑖fi is the frequency of the ith class interval.
Variance: The variance of grouped frequency data is calculated using the
𝑓𝑖⋅(𝑥𝑖−𝑥ˉ)2
formula: σ2= ∑𝑖 where 𝑥𝑖 is the midpoint of the ith class interval, 𝑓𝑖 is
𝑁
the frequency of the ith class interval, and Mean is the mean calculated earlier.
CODE:
// This is Practical 11
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 8;
float x[8]={10.5,15.5,20.5,25.5,30.5,35.5,40.5,45.5};
float f[8]= {7,6,19,35,30,23,7,1};
print_arr(x,n);
print_arr(f,n);
printf("Mean of the data is %f\n",mean(x,f,n));
printf("Variance of the data is %f\n",variance(x,f,n));
return 0;
}
/*
OUTPUT FOR PRACTICAL 11:
10.5 15.5 20.5 25.5 30.5 35.5 40.5 45.5
7.0 6.0 19.0 35.0 30.0 23.0 7.0 1.0
Mean of the data is 27.414062
Variance of the data is 56.297302
*/
PRACTICAL 12
AIM: Fit Binomial Distribution for grouped data.
PROBLEM:
Develop a function to fit a Binomial distribution to the given data {(𝑥𝑖,𝑓𝑖)|𝑖 = 1,2, …,𝑛}.
Hence, demonstrate its use in the 𝑚𝑎𝑖𝑛( ) for given data. (see FMS Example 8.19)
X 0 1 2 3 4 5 6 7 Total
f 7 6 19 35 30 23 7 1 128
P(X=k)=𝐶𝑘𝑛 pk(1−p)n−k
CODE:
// This is Practical 12
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
OUTPUT FOR PRACTICAL 12:
^Z
Fitted Data is:
X: 0 1 2 3 4 5 6 7
F: 2 11 25 33 29 18 7 2
*/
PRACTICAL 18
AIM: To carry out matrix operations in C.
1. Matrix Representation:
- Matrices are typically represented using 2D arrays in C++.
- Each element of the array corresponds to an element of the matrix.
3. Matrix Multiplication:
- Matrix multiplication is a bit more involved than addition and subtraction.
- The product of two matrices ( A ) and ( B ), denoted as ( AB ), is calculated by
taking the dot product of rows of ( A ) with columns of ( B ).
- The number of columns in the first matrix must equal the number of rows in the
second matrix.
- The resulting matrix has the number of rows of the first matrix and the number of
columns of the second matrix.
4. Matrix Inversion:
- Matrix inversion is the process of finding the inverse of a matrix.
- The inverse of a matrix ( A ), denoted as ( A^{-1} ), is such that ( A times A^{-1} =
A^{-1} times A = I ), where ( I ) is the identity matrix.
- Not all matrices have inverses; square matrices with non-zero determinants have
inverses.
5. Library Support:
- C++ provides libraries like the Standard Template Library (STL) and external
libraries like Eigen or Armadillo for matrix operations.
- These libraries offer efficient implementations of various matrix operations and
often include additional functionalities like matrix decomposition, solving linear
equations, and more.
CODE:
// This is Practical 18
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define R1 3
#define C1 3
#define R2 3
#define C2 3
void read_matrix(int row, int col,int arr[row][col]){
int i,j;
for(i =0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d",&arr[i][j]);
}
}
}
void print_matrix(int row, int col,int arr[row][col]){
int i,j;
for(i =0;i<row;i++){
for(j=0;j<col;j++){
printf("%3d ",arr[i][j]);
}
printf("\n");
}
}
void mat_addition(int rows, int cols,int A[rows][cols],int
B[rows][cols],int R[rows][cols]){
int i,j;
for(i =0;i<rows;i++){
for(j=0;j<cols;j++){
R[i][j] = A[i][j]+B[i][j];
}
}
}
void mat_subtraction(int rows, int cols,int A[rows][cols],int
B[rows][cols],int R[rows][cols]){
int i,j;
for(i =0;i<rows;i++){
for(j=0;j<cols;j++){
R[i][j] = A[i][j]- B[i][j];
}
}
}
void mat_mult(int r1, int c1,int r2, int c2,int A[r1][c1],int
B[r2][c2],int R[r1][c2]){
if(c1!= r2){printf("Matrix multiplication not possible");return;}
int i,j,k;
int sum;
for(i =0;i<r1;i++){
for(j=0;j<c2;j++){
sum = 0;
for(k=0;k<c1;k++){
sum += A[i][k]*B[k][j] ;
}
R[i][j] = sum;
}
}
}
void transpose(int row,int col,int y[row][col]){
int i,j,tmp;
for(i=0;i<row;i++)
for(j=0;j<i;j++)
{
tmp = y[i][j];
y[i][j] = y[j][i];
y[j][i] = tmp;
}
}
int main(){
int M1[R1][C1];
int M2[R2][C2];
int res[R1][C1];
printf("Enter Values of Matrix 1: \n");
read_matrix(R1,C1,M1);
printf("Enter Values of Matrix 2: \n");
read_matrix(R2,C2,M2);
printf("Matrix 1 is :\n");
print_matrix(R1,C1,M1);
printf("Matrix 2 is :\n");
print_matrix(R2,C2,M2);
CODE:
//This is Practical 1
#include <stdio.h>
int main() {
int i = getchar()!= EOF;
printf("%d",i);
return 0;
}
PROBLEM: Write a program to count the number of characters on the input using
getchar()!=EOF
THEORY:
2. EOF (End of File): EOF is a macro defined in `<stdio.h>`. It's a signal indicating
that there are no more characters to read from the input stream. It's typically
represented by the integer value `-1`.
CODE:
// This is Practical 2
#include <stdio.h>
int main() {
int len=0;
while(getchar()!=EOF){
len++;
}
printf("length is %d",len);
return 0;
}
/*
OUTPUT FOR PRACTICAL 2:
abcd
length is 5
*/
Practical 3
AIM: Write a program to count blanks, tabs and new-lines on the input. (Hint: either
use scanf("%[-]s", var) feature of scanf(...) or use getchar() !=EOF to store a string
on input to be terminated with EOF).
Theory:
• The getchar() function does not require any arguments. It simply reads a
single character from the input stream and returns it as an integer value. If
successful, it returns the character read as an unsigned char cast to an int or
EOF (end-of-file) if there is no more input available.
• The scanf() function takes a format string as its first argument, which
specifies the format of the input to be read. The subsequent arguments are
pointers to variables where the input values will be stored.
Code & Output:
// This is Practical 3
#include <stdio.h>
int main() {
int line = 0;
int tab = 0;
int blank = 0;
int i;
do
{
i = getchar();
if(i == '\n'){
line++;
}else if (i == ' ')
{
blank++;
}else if(i == '\t'){
tab++;}
} while (i != EOF);
printf("The Number of \nlines : %d\nTabs : %d\nBlanks :
%d\n",line,tab,blank);
return 0;
}
AIM: to implement the trimup() function in C programming. This function will take a
string as input and modify it
PROBLEM: Develop a function trimup() such that when a string is passed into it the
characters, except in the range A-Zor a-z, are removed and the lower case
characters are converted to upper case.
THEORY: he function will iterate over each character in the input string. For each
character, it will check if it falls within the desired range (A-Z or a-z). If it does, the
character will be preserved or converted to uppercase if it is a lowercase letter. If the
character does not fall within the desired range, it will be removed from the output
string.
CODE:
// This is Practical 4
#include <stdio.h>
int main(){
char a[]="A4__r*787ya%$n";
int len=14;
trimup(a,len);
return 0;
}
/*
OUTPUT FOR PRACTICAL 4:
A4__r*787ya%$n
ARYAN
*/
PRACTICAL 5:
PROBLEM: It is known that the range of numbers for int data type is -32768 to
+32767. Using integer variables verify the numbers, which results on adding +1 (or
subtracting-1) sequentially in starting from, say, +32760 (or-32760). Hence confirm
the 2's complement representation for integral numbers in C.
THEORY: For a 16-bit integer (int), the range of numbers is from -32768 to +32767.
When you add 1 to +32767, it overflows and wraps around to -32768. Similarly,
when you subtract 1 from -32768, it overflows and wraps around to +32767.
CODE:
// This is Practical 5
#include <stdio.h>
int main() {
int i;
int n = 32763;
for(i=1;i<7;i++){
printf("%d\n",n+i);
}
return 0;
}
/*
OUTPUT FOR PRACTICAL 5:
32764
32765
32766
32767
-32768
-32767
*/