CS lab Manual_copy
CS lab Manual_copy
The functions of a computer system can be carried out by using the three main
units namely input unit, system unit and output unit. The block diagram of a
computer system is as follows:
a) Input unit: is used to enter data and information into a computer by user. The
devices like keyboard, mouse and scanner are commonly used input devices.
▪ A keyboard is used to enter alphanumeric characters and symbols.
▪ The mouse is used to pick or select a command from the monitor
screen. A scanner is used to scan an image or read a barcode and so on.
b) Arithmetic and Logic Unit (ALU): is a digital circuit that perform arithmetic
(Add, Sub, Multiplication, Division) and logical (AND, OR, NOT) operations. It
d) Memory Unit (MU): is the unit where all the input data and results are stored
either temporarily or permanently. The memory of a computer has two types:
1. Main Memory / Primary Memory units
i. Random Access Memory (RAM): RAM constitutes internal memory of
the CPU for storing data, program and result. It is read/write memory. RAM
is volatile i.e. data stored in it is lost when we switch off the computer or if
there is power failure.
ii. Read Only Memory (ROM): ROM is a permanent memory that can be
read but not written. Memory access is faster in ROM compared to RAM.
ROM is non volatile. i.e. contents are not lost when the computer is
switched off. Used to store permanent programs that don‟t change
2. Secondary Memory: It is not directly connected to CPU. It is non-volatile.
Slower than primary memory but have larger capacities. Different Types of
Secondary Memory or storage devices are hard disk, Magnetic Tape, Optical
Disks – CD-ROM, DVD-ROM,
Flash memory
Search for the terminal application in the dash tool (Located as the topmost item in the
launcher). Open up a terminal by clicking on icon.
Type the command gedit hello.c (hello is the file name. .c is the c extension)
gcc command will invoke the GNU C compiler to compile the file hello.c and
create the output (-o) file i.e. executable file called hello.
Linking is the process of putting together other program files and functions that
are required by the program. For example if the sqrt() function is used then
function definition of this function should be brought from the math library and
linked to the main program.
The compiled and linked program is called executable object code and is stored
automatically in another file called a.out.
The command ./a.out would load the executable object code into the computer‟s
memory and execute the instructions. During the program execution, the program
may request for some data to be entered through the keyboard. Sometimes the
program does not produce the desired results if something is wrong with the
program logic or input data. Then it would be necessary to correct the source
program. In case if the source program is modified the entire process of
compiling, linking and executing the program should be repeated.
The entire process of compiling and running a C program is shown in figure below:
PROGRAM 1
ALGORITHM:
STEP 1: START
STEP 2: [Enter an arithmetic expression]
Read num1, num2, operator value
STEP 3: [Evaluate choice with case statements]
Step 3.1: case „+‟ res=num1+num2, print res. goto step 4
Step 3.2: case „-„ res=num1-num2, print res. goto step 4
Step 3.3: case „*‟ res=num1*num2, print res. goto step 4
Step 3.4: case „/‟ if(num2==0)
print Divide by zero error . goto step 4
else
res=num1/num2, print div. goto step 4
Step 3.5: case „%‟ if(num2==0)
print “Divide by zero error”. goto step 4
else
res=num1%num2, print res. goto step 4
Step 3.6: default print “Invalid expression”
STEP 4: STOP
FLOWCHART:
PROGRAM:
#include<stdio.h>
void main()
{
int num1,num2,res; char op;
printf("\n Enter a valid arithmetic expression:\n");
scanf("%d%c%d",&num1,&op,&num2);
switch(op)
{
case '+': res=num1+num2;
printf("\n Sum=%d", res);
break;
case '-': res=num1-num2;
printf("\n Difference=%d", res);
break;
case '*': res=num1*num2;
}
break;
}
else
{
res=num1%num2;
printf("\n Remainder=%d", res);
}
break;
EXPECTED OUTPUT:
OUTPUT 1
OUTPUT 5
gcc Lab1.c
./a.out
Enter a valid arithmetic expression: 12/0
Divide by zero error
QUESTIONS TO BE ANSWERED:
Ans- Header files contain declarations of functions and variables that are
defined in other source files.
PROGRAM 2
THEORY/LOGIC:
To read the coefficients a, b, c and check if any of coefficients value is zero, if so, print
appropriate messages and re-run the program.
i) If b2 − 4ac > 0 then can‟t take the square root of this positive amount and there
will be two different real roots for the quadratic equation.
ii) If b2− 4ac < 0 then can‟t take the square root of a negative number, so there
will be two imaginary roots.
iii) If b2− 4ac = 0 then the amount under the radical is zero and since the square
root of zero is zero, will get two real roots and they are equal.
fabs() is a built in function which purpose is: A negative value becomes positive,
positive value is unchanged. The return value is always positive
ALGORITHM:
STEP 1: START
STEP 3: [Check whether any of the coefficient value is zero] if a=0 and
b=0 then
[Based on discriminate value classify and calculate all possible roots and
print them]
print roots are real and equal, root1=root2 end if goto step10
STEP 7: [Check for real and distinct roots, if discriminate value is >0] else
if(disc>0) then root1=(-b+sqrt(disc))/(2*a); root2=(-b-sqrt(disc))/(2*a);
print The roots are real and distinct print root1 and print root2 end if goto
step10
STEP 8: [Check for real and imaginary roots, if discriminate value is <0]
if(disc<0) then real=-b/(2*a) img=sqrt(fabs(disc))/(2*a)
root1=real+i(imag) root2=real-i(imag)
STEP 9: [Finished]STOP
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,d,real,img,root1,root2;
printf("Enter the coefficients of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
{
printf("Roots can't be computed\n \n");
exit(0);
}
if(a==0)
{
printf("The roots are linear\n");
root1=-c/b;
printf("root=%f\n",root1);
exit(0);
}
d=b*b-4*a*c;
if(d==0)
{
root1=-b/(2*a);
printf("Roots are Real and Equal\n Root1=Root2=%f\n",root1);
}
else if(d>0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("Roots are Real and Distinct\n Root1=%f\n Root2=%f\n",root1,root2);
}
else
{
printf("Roots are Complex/Imaginary\n");
real=-b/(2*a);
img=sqrt(fabs(d))/(2*a);
printf("Root1= %f +i %f\n",real,img);
printf("Root2= %f -i %f\n",real,img);
}}
EXPECTED OUTPUT-
gedit Lab2.c
gcc Lab2.c -lm
./a.out
OUTPUT 1-
gedit Lab2.c
gcc Lab2.c -lm
./a.out
Enter the coefficients of a,b,c 0 0 5
Roots can't be computed
OUTPUT 2-
gcc Lab2.c -lm
./a.out
Enter the coefficients of a,b,c 0 7 3
The roots are linear
root=-0.428571
OUTPUT 3-
gcc Lab2.c -lm
./a.out
Enter the coefficients of a,b,c 1 2 1
Roots are Real and Equal
Root1=Root2=-1.000000
OUTPUT 4-
gcc Lab2.c -lm
./a.out
Enter the coefficients of a,b,c 1 5 2
Roots are Real and Distinct
Root1= -0.438447
Root2= -4.561553
OUTPUT 5-
gcc Lab2.c -lm
./a.out
Enter the coefficients of a,b,c 1 2 3
Roots are Complex/Imaginary Root1= -1.000000 +i 1.414214
Root2= -1.000000 -i 1.414214
QUESTIONS TO BE ANSWERED:
1. Define C- token.
2. Define variables.
Ans- primary data types represent the basic building blocks for storing and
manipulating data. Here are the main primary data types in C: int, float, char,
double, void.
PROGRAM 3
An electricity board charges the following rates for the use of electricity: for the first
200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs
1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total
amount is more than Rs 400, then an additional surcharge of 15% of the total amount is
charged. Write a program to read the name of the user, the number of units consumed,
and print out the charges.
ALGORITHM:
STEP 1: START
STEP 2: Read a customer name and the number of units
STEP 4: [To check electricity unit is greater than 200 and less than or
equal to 300 and calculate charges] -
charges=(200*0.8)+(100*0.9)+(unit-
300)*1+100
charges=charges+0.15*charges
STEP 8: STOP
FLOWCHART:
PROGRAM:
#include<stdio.h>
void main()
{
char name[20];
int unit;
float charge;
printf("Enter the name of consumer\n");
scanf("%s",name);
printf("Enter the number of units consumed\n");
scanf("%d",&unit);
if(unit<=200)
{
charge=(unit*0.80)+100;
}
else if(unit>200 && unit<=300)
{
charge=(200*0.80) + (unit-200)*0.90+100;
}
else
{
charge=(200*0.80) + (100*0.90) + (unit-300)*1+100;
}
if(charge>400)
{
charge=charge+0.15*charge;
}
printf("Consumer Name: %s\n”,name);
printf(“Number of Units consumed: %d\n”,unit);
printf(“ Total Rupees: %f \n"charge);
}
EXPECTED OUTPUT:
OUTPUT 1:
Enter the name of consumer
Rajesh
Enter the number of units
consumed 121
Consumer Name: Rajesh Units consumed: 121 Rupees: 196.80
OUTPUT 2:
gcc Lab4.c
./a.out
Enter the name of consumer
OUTPUT 3:
gcc Lab4.c
./a.out
Enter the name of consumer
Rahul Enter the number of
units consumed 350
Consumer Name: Rahul Units consumed: 350 Rupees: 400.00
OUTPUT 4:
gcc Lab4.c
./a.out
Enter the name of consumer
Smitha Enter the number of
units consumed 742
Consumer Name: Smitha Units consumed: 742 Rupees: 910.80
QUESTIONS TO BE ANSWERED:
Ans-if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
2) Which function is used to read input from the user in C?
Ans-scanf()
PROGRAM 4
Write a C Program to display the following by reading the number of rows as input.
PROGRAM:
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",j);
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
}
EXPECTED OUTPUT:
Enter no of rows: 4
1
121
12321
1234321
QUESTIONS TO BE ANSWERED :
PROGRAM 5
THEORY/LOGIC:
Consider a list of numbers stored in an array and to check whether the given number
is present within the given list or not. If the number is found in the list, search is
Successful and the message is printed otherwise unsuccessful message is printed.
Binary seaarch is applied if the items are arranged in either ascending or descending
order.
In binary search first element is considered as low and last element is considered as
high.
ALGORITHM:
STEP 1: START
mid← (low+high)/2
else high←mid-1
STEP 8: STOP
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int low, high, n, mid, i, a[50], key ;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements in ascending order\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key == a[mid])
{
printf("Successful Search & element is found at position = %d\n",mid+1);
exit(0);
}
if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
printf("Unsuccessful Search\n");
}
EXPECTED OUTPUT:
OUTPUT 1:
gcc Lab5.c
./a.out
Enter number of elements
5
Enter array elements in ascending order
- 5 10 20 30 80
Enter the number to be searched 10
Search successful
OUTPUT 2:
gcc Lab5.c
./a.out
Enter number of elements 8
Enter array elements in ascending order 5 9 10 15 20 24 30 87
Enter the number to be searched 99
Search unsuccessful
QUESTIONS TO BE ANSWERED:
4. What is an array?
Ans- Array is a collection of elements of same data type.
PROGRAM 6
THEORY/LOGIC:
Condition to satisfy for matrix multiplication: The number of columns of the 1st matrix must
be equal to the number of rows of the 2nd matrix. And the order of the resultant matrix will
have the same number of rows as the 1st matrix, and the same number of columns as the 2nd
matrix.
To read two matrices of the form A(m x n) and B(p x q) and if the compatibility is matched,
the multiplication for the defined matrices is carried out and displayed in the suitable matrix
format.
ALGORITHM:
Step 1 : Start
Step 2 : Read m,n,p,q
Step 3 : if(n!=p) then
Print “Matrix Multiplication is not Possible”
end if goto Step 8
Step 4 : Read Matrix a
for i←0 to m
for j←0 to n do
Read a[i][j]
end for
end for
Step 5 : Read Matrix b
for i←0 to p
for j←0 to q
Read b[i][j]
end for
end for
Step 6 : for i←0 to m
for j←0 to q
c[i][j] ← 0
for k←0 to n
c[i][j] ← c[i][j]+a[i][k]*b[k][j]
end for end for
end for
Step 7: Print “Resultant Matrix”
for i←0 to m
for j←0 to q do
Print “ c[i][j]”
end for
end for
Step 8 : Stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],c[50][50];
int m,n,p,q,i,j,k;
printf("Enter the order of Matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the order of Matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix Multiplication Not Possible\n");
}
else
{
printf("Enter the elements of Matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Matrix A is displayed as\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of Matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Matrix B is displayed as\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nMATRIX MULTIPLICATION \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The resultant Matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}
EXPECTED OUTPUT:
OUTPUT 1:
Enter the order of Matrix A 2 2
Enter the order of Matrix B 2 2
Enter the elements of Matrix A 1 2 3 4
Matrix A is displayed as
1 2
3 4
Enter the elements of Matrix B 5 6 7 8 Matrix B is displayed as
5 6
7 8
The resultant Matrix is 19 22
43 50
OUTPUT 2:
gcc Lab8.c
./a.out
Enter the order of Matrix A 2 3
Enter the order of Matrix B 4 5
QUESTIONS TO BE ANSWERED:
1. Differentiate row major and column major order matrices.
Ans- In row-major order, the elements of a matrix are stored row by row. The
consecutive elements of a row are placed next to each other in memory. In column-
major order, the elements of a matrix are stored column by column. The consecutive
elements of a column are placed next to each other in memory.
PROGRAM 7
Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
THEORY/LOGIC:
● Input degree is converted to radian and computation of sine value takes place.
● Taylor series is a representation of a function as an infinite sum of
terms that are calculated from the values of the function's derivatives at
a single point.
ALGORITHM:
Step 1: START
Step 2: Read degree , number of terms
Step 3: Compute x = degree*3.14593/180;
term = x;
sum = x;
Step 4: Repeat the following steps for i = 1 to 10
term = term*x*x/(2*i*(2*i+1));
sum = sum+term;
Step 5: Print sum, sin(x)
Step 6: STOP
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
int i,n;
float degree,x,sum,term;
printf(“Enter the number of terms\n”);
scanf(“%d”,&n);
printf("Enter a value in degree\n");
scanf("%f",°ree);
x=degree*3.14593/180;
term=x;
sum=x;
for(i=1;i<=n;i++)
{
term= - term*x*x/(2*i*(2*i+1));
sum=sum+term;
}
printf("The calculated sum is = %f\n",sum);
printf("The Mathematical sine value is = %f\n",sin(x));
}
EXPECTED OUTPUT:
$gcc lab8.c -lm
$./a.out
Enter a
value in
degree 90
QUESTIONS TO BE ANSWERED:
1. Why Taylor series approximation is important in science and engineering?
Ans- For function approximation, numerical analysis, error analysis.
PROGRAM 8
THEORY/LOGIC:
□ The process of arranging elements in either ascending order or
descending order is called Sorting.
Bubble Sort
□ In this technique two successive elements of an array such as a[j] and
a[j+1] are compared.
□ If a[j]>=a[j+1] the they are exchanged, this process repeats till all
elements of an array are arranged in ascending order.
□ After each pass the largest element in the array is sinks at the bottom
and the smallest element in the array is bubble towards top. Bubble
Sort algorithm compares each pair of adjacent (neighbouring)
elements and the elements are swapped if they are not in ascending
order.
ALGORITHM:
Step 1: Start
Step 2 : Read n
Step 3: for i←0 to n
Read a[i]
end for
Step 4: for i← 1 to n
for j← 0 to n-i
if (a[j]>a[j+1]) then
temp←a[j]
a[j] ←a[j+1]
a[j+1] ←temp
end if
end for
end for
Step 5 : Print “Sorted Array”
for i←0 to n
Print “a[i]”
end for
Step 6 : Stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
EXPECTED OUTPUT:
gedit Lab10.c
gcc Lab10.c
./a.out
Enter the number of elements : 10
Enter 10 elements
12 -5 2 -30 8 -15 3 -10 6 0
The entered elements are
12 -5 2 -30 8 -15 3 -10 60
The sorted elements are
-30 -15 -10 -5 0 2 3 6 8 12
QUESTIONS TO BE ANSWERED:
PROGRAM 9
Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques
.
ALGORITHM:
Step 1 : Start
Step 2 : Read str1, str2
Step 3:len1 = length(str1) and len2 = length(str2)
Step 4: Print len1, len2
Step 5: Compare(str1,str2)
Step 6: Concat(str1, str2)
Step 7: Stop
function length(str1)
for i←0 to str1[i]!= „\0‟ do
count←count+1
end for
return count
function concat(str1,str2)
i=length(str1)
for j←0 to str2[j]!= „\0‟ do
str1[i] ←str2[j]
i=i+1
end for
print str1
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int length(char s[100]);
void compare(char str1[100],char str2[100]);
void concat(char str1[100],char str2[100]);
}
if(flag==1)
printf(“Strings are equal”):
else
printf(“Strings are different”);
}
void main()
{
char str1[100],str2[100];
int len1,len2;
printf("Enter the 1st string\n");
scanf("%s",str1);
printf("Enter the 2nd string\n");
scanf("%s",str2);
len1=length(str1);
len2=length(str2);
printf("The length of first string is %d\n",len1);
printf("The length of second string is %d\n",len2);
compare(str1,str2);
concat(str1,str2);
}
PROGRAM 10
ALGORITHM:
Step 1 : Start
Step 3 : Read n
Read s[i].usn,
s[i].name,s[i].marks
sum←sum+s[i].marks
end for
Step 5 : avg←sum/n
if(s[i].marks>=avg)then
Print s[i].usn,s[i].name,s[i].marks
end if
end for
FLOWCHART:
PROGRAM:
#include<stdio.h>
struct student
{
int marks,usn;
char name[100];
};
void main()
{
struct student s[100];
int i,n;
float sum=0.0,avg=0.0;
printf("Enter the number of students\n");
scanf("%d",&n);
printf("Enter the student details\n");
printf("\n usn\t name\t marks\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d",&s[i].usn,s[i].name,&s[i].marks);
sum=sum+s[i].marks;
}
avg=sum/n;
printf ("Average marks is %f",avg);
printf("Students who scored above average marks:\n");
printf("\nusn \t name \t marks \n");
for(i=0;i<n;i++)
{
if(s[i].marks>=avg)
{
printf("%d\t%s\t%d\n",s[i].usn,s[i].name,s[i].marks);
}
}
printf("\nStudents scored below average marks:\n");
printf("\n usn\t name\t marks \n");
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
{
printf("%d\t%s\t%d\n",s[i].usn,s[i].name,s[i].marks);
}
}
}
EXPECTED OUTPUT:
Enter the number of students 4
Enter the student details:
usn, name, marks
7 alen 75
9 priya 85
20 sidharth 91
25 zuman 82
QUESTIONS TO BE ANSWERED:
1. Define structures
Ans- Structure is a collection of members of same or different data types.
PROGRAM 11
Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of n real numbers.
THEORY/LOGIC:
● The mean is the average and is computed as the sum of all numbers divided
Standard deviation (SD) (σ) is a measure that is used to quantify the amount of
variation or dispersion of a set of data values. A standard deviation close to 0
indicates that the data points tend to be very close to the mean (also called the
expected value) of the set, while a high standard deviation indicates that the
● sum=1+2+3+4+5=15.00
● Mean=15.00/5=3.00
● Standard deviation=sqrt((1-3.00)2+(2-3.00)2+(3-3.00)2+(4-3.00)2+(5-
3.00)2 / 5 )=1.41
ALGORITHM:
Step 1: Start
Step 3 : for i← 0 to n do
Read a[i]
end for
Step 4 : ptr←a
end for
Step 6 : mean←sum/n
sumvar←sumvar+pow( (*ptr-mean),2)
ptr←ptr+1
end for
Step 8 : var←sumvar/n
Step 9 : sd←sqrt(num)
Step 11 : Stop
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
float a[50],sum=0.0,sumvar=0.0,mean,var,sd;
float *ptr;
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d array elements\n",n);
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumvar=sumvar+(pow((*ptr-mean),2));
ptr++;
}
var=sumvar/n;
sd=sqrt(var);
printf("Sum = %f\n",sum);
printf("Mean = %f\n",mean);
printf("Standard Deviation = %f\n",sd);
}
EXPECTED OUTPUT:
gedit Lab14.c
gcc Lab14.c
./a.out
Enter the number of elements 5
Enter 5 array elements
2.1 4.2 6.3 8.4 10.5
Sum = 31.500000
Mean = 6.300000
Standard Deviation = 2.969849
QUESTIONS TO BE ANSWERED:
1. Why standard deviation is calculated for a list of N related values.
Ans- To provide insights into the spread or distribution of the data points around the
mean (average) value.
2. Define pointers in C.
Ans-Pointer is a variable that holds address of another variable.
PROGRAM 12
Write a C program to copy a text file to another, read both the input file name and
target file name
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *p1, *p2;
char filename[100], c;
printf("Enter the filename to open for reading\n");
scanf("%s", filename);
p1=fopen(filename,"r");
if(p1==NULL)
{
printf("Cannot open file %s\n", filename);
exit(0);
}
printf("Enter the filename to writing\n");
scanf("%s", filename);
p2=fopen(filename,"w");
c=fgetc(p1);
while(c!=EOF)
{
fputc(c,p2);
c=fgetc(p1);
}
printf("\n File copied successfully”);
fclose(p1);
fclose(p2);
}
EXPECTED OUTPUT-
To Execute :
Gedit file.c
Gcc file.c -o file.out
. / file.out
Outputs:
Enter the file name to open for reading.
Student.txt
Cannot open file student.txt
Student.txt
output.txt