Pop Manual Final
Pop Manual Final
LABORATORY MANUAL
“Principles of Programming using C”
Semester: I/II
Scheme: CBCS
Prepared By
Scrutinized by
Dr.,Suresh
HOD, Dept. of ISE
Programming Assignments
1 Simulation of a Simple Calculator.
2 Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
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 total
amount is charged. Write a program to read the name of the user, number of units consumed and print out
the charges.
4. Write a C Program to display the following by reading the number of rows as input,
1
1 2 1
12 3 21
12 3 4 3 2 1
nth row
5 Implement Binary Search on Integers.
6 Implement Matrix multiplication and validate the rules of multiplication.
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.
8 Sort the given set of N numbers using Bubble sort.
9 Write functions to implement string operations such as compare, concatenate, and find string length. Use the
parameter passing techniques.
10 Implement structures to read, write and compute average- marks of the students, list the students scoring above
and below the average marks for a class of N students.
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.
12. Write a C program to copy a text file to another, read both the input file name and target file name.
Principles of Programming using C [22POP13\23]
Algorithm:
• An algorithm is a basic tool which is used to express the solution for a given problem
systematically in the form of instructions. This solution is called logic. It takes a set of input
values and produces the desired output.
Characteristics of an Algorithm:
• Finiteness: It should be a sequence of finite instructions. That is, it should end after a fixed
time. It should not enter into an infinite loop.
• Effectiveness: This means that operations must be simple and are carried out in a finite time
at one or more levels of complexity. It should be effective whenever traced manually for the
results.
Algorithmic Notations:
4
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flowchart:
• The various types of geometric shapes, arrows and symbols used while drawing the
flowchart are called flowchart symbols. The symbols used and the meaning associated with
each symbol are shown below.
Connector
Flow of control
5
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
area, peri
Program Output
#include<stdio.h> Enter length and
{
breadth 2
int l, b, area, peri;
printf(“Enter length and breadth\n”); 4
scanf(“%d%d”, &l, &b);
The area is 8
area = l * b;
peri = 2 * (l + b); The perimeter is 12
printf(“The area is %d\n”, area);
printf(“The perimeter is %d\n”, peri);
}
6
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Program-1
7
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flowchart
Start
Input a,b,ch
Switch(ch)
1 2 3 4 5 Default
Print res
Stop
Viva Questions:
a. What is switch statement?
b. What is a case in a switch statement?
c. Is Default necessary in switch case?
d. How many cases can you have in a switch statement?
8
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include<stdio.h>
#include<stdlib.h
> void main()
{
int ch,a,b;
float res;
printf(“Enter two numbers:\
n”); scanf(“%d%d”,&a,&b);
printf(“1=Add, 2=Sub, 3=Mul, 4=Div, 5=Rem\
n”); printf(“Enter your choice:\n”);
scanf(“%d”,&ch);
switch(ch)
{ case 1:
res=a+
b;
break;
case 2: res=a-b;
break;
case 3:
res=a*b;
break;
case 4: res=(float)a/b;
break;
case 5: res=a%b;
break;
default: printf(“Entered Wrong choice\n”);
}
printf(“Result=%f\n”,res);
}
Output
Enter two numbers: 10 20
1=Add, 2=Sub, 3=Mul, 4=Div, 5=Rem
Enter your choice:1
Result=30
9
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Program-2
Step 1: Start
Step 2: Read the values of non-zero coefficients a,b and c.
Step 3: If a|b|c is zero goto Step 9. Otherwise Compute the value of discriminant (disc)which is
Equal to (b*b)-(4*a*c).
Step 4: Check if disc is equal to 0. If true, then go to Step 5. Otherwise, goto Step6
Step 5: Compute the roots.
root1 = (-b)/(2*a)
root2=root1
Step 6: Check if disc is greater than zero or not. If true, then go to Step 7. Otherwise, goto Step 8.
root1 =
(-b+sqrt(disc))/(2*a) root2
= (-b-sqrt(disc))/(2*a)
Output roots as
root1 = r_part +
i_part root2 = r_part
– i_part
10
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flow chart
Start
Input a, b, c
if((a*b*c*)==0) True
Roots can’ be
False determined
Disc=(b*b)-(4*a*c)
False
False
Roots are
complex
Stop
11
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include<stdio.h>
#include<stdlib.h
>
#include<math.h>
int main( )
{
float a, b, c, root1, root2, rpart, ipart, disc;
printf("Enter the 3 coefficients:\n”);
scanf(%f%f%f", &a, &b, &c);
if((a*b*c) = = 0)
{
printf("Roots cannot be Determined:\
n"); exit(0);
}
disc = (b*b) - (4*a*c);
if(disc = = 0)
{
printf("Roots are equal\n");
root1=root2= -b / (2*a);
printf("root1=root2=%f",root1);
}
else if(disc>0)
{
printf("Roots are real and distinct\
n"); root1= (-b + sqrt(disc)) / (2*a);
root2= (-b - sqrt(disc)) / (2*a);
printf ("root1 = %f \n root2 = %f\n", root1, root2);
}
els
e
{ printf("Roots are complex\
n"); rpart = -b / (2*a);
ipart = (sqrt (-disc)) / (2*a);
printf("root1 = %f + i %f \n", rpart, ipart);
printf("root2 = %f - i %f \n", rpart, ipart);
}
}
12
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Output
First Run:
Enter the 3 coefficients:
1
2
1
Roots are equal
root1=root2=-
1.000000 Second
Run:
Enter the 3 coefficients:
2
3
2
Roots are real and
equal root1=-0.500000
root2=-2.000000
Third Run:
Enter the 3 coefficients:
2
2
2
Roots are complex
root1=-0.500000+i 0.866025
root2=-0.500000- i 0.866025
Viva Questions:
e. What is quadratic Equation?
f. What is math.h ? why it is used in C program?
g. What are decision making statements in C language?
h. Write the syntax of “ if ”statement?
i. Difference between if and switch statements?
13
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
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 total amount is charged. Write a program to read the name of the
user, number of units consumed and print out the charges.
Algorithm:
Input: Customer name and unit consumed
Output: Customer name, units consumed and total amount to be paid
Step 1: Start
Step 2: Read the name of customer and the unit consumed by the customer.
Step 3: Check if the unit consumed is greater than 1 and less than 200,if true goto step 4 else goto
step 5.
Step 4: Compute: amt=100+(0.8*units).
Step 5: if unit is greater than 200 and less than 300,if true goto step 6 else goto step 7
Step 6: Compute amt=100+(200*0.8)+((units-200)*0.9)
Step 7:Compute amt=100+(200*0.8)+(100*0.9)+((units-300)*1), then goto step 8
Step 8: Check if the amt is less than or equal to 400, if true goto step 9 otherwise goto step 10.
Step 9: Print the amount charged and goto step 11.
Step 10: compute amt=amt*1.15,and print the amount charged.
Step 11: Stop .
14
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flow chart
Start
Input name
Input units
If(units<=200) amt=100+(0.8*units);
True
False
amt=100+(200*0.8)+(100*0.9)+(units-300)*1;
True
If(units>400)
True
amt=amt*1.15
Stop
15
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include <stdio.h>
void main()
{
char name[10];
float unit, amt;
printf("Enter your name and unit
Consumed:"); scanf("%s %f",name,&unit);
if(unit<=200)
amt=unit*0.80+100;
else if((unit>200)&&(unit<=300))
amt=200*0.80+((unit-
200)*0.90)+100;
else
amt=200*0.80+100*0.90+((unit-300)*1)+100;
if(amt>400)
amt=1.15*amt;
printf("Name: %s\n Unit=%f \n charge=%f ",name,unit,amt);
}
Output
First Run
Enter your name and unit Consumed: Siri
52 Name: Siri
Unit=52
charge=141.600000
Second Run
Enter your name and unit Consumed: Rajesh 460
Name: Rajesh
Unit=460
charge=586.500000
Viva Questions:
1. Difference between float and double data types.
2. Write syntax of for loop?
3. What is the use of break statement?
4. Difference between continue and break statement?
16
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Program-4
Write a C Program to display the following by reading the number of rows as input,
1
1 2 1
12 3 21
12 3 4 3 2 1
nth row
Algorithm:-
Step1 : start
Step2 : define value of N
Step3 : [printing the pattern]
For i=1 to N repeat
For k=N to i repeat
Print “ “
[end of for loop]
For j=1 to i repeat
Print j
[end of for loop]
For l=j-2 to 1 repeat
Print l
[end of for loop]
Move to next line
[end of for loop]
Step4 : stop
17
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flow fff
chart
Start
Declaration i,j,k,l
for(i=1;i<=N;i++)
for(k=N;k>I;k--)
Print “ “
for(j=1;j<=i;j++)
Print “j”
for(l=j-
Print “l”
Print”\n”
Stop
18
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include<stdio.h>
#define N 4
int main()
{
int i,j,k,l;
for(i=1;i<=N;i++)
{
for(k=N;k>=i;k--)
printf(“ “);
for(j=1;j<=i;j++)
printf(“%d”,j);
for(l=j-2;l>0;l--)
printf(“%d”,l);
printf(“\n “);
}
return 0;
}
Output :-
1
1 2 1
12 3 21
12 3 4 3 2 1
19
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Program-5
Step 1: Start
Step 2: Read size of the array n
Step 3: Read the list of elements in sorted order a[i].
Step 4: Read the key element in key
Step 5: initialize low=0 and high= n-1
Step 6: Check if low is less than or equal to high. If condition is true, goto step 7, other wise goto
Step 11
Step 7: find the middle element.i.e. mid=(low+high)/2;
Step 8: if key element is equal to mid element, then initialize loc value, loc = mid+1; otherwise
goto step 9
Step 9: Check if key element is less than mid element is true, then initialize high=mid-1 then goto
step 6, if it false goto step10.
Step 10: Check if key element is greater than mid element is true, then initialize low=mid+1 then
goto step 6.
Step 11: Check if loc value is greater then zero then print the search is successful then goto step 13,
otherwise goto step 12.
Step 12: print search is unsuccessful, then goto step 13.
Step 13: Stop
20
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
FlowChart
Start
Input n
False
for(i=0;i<n;i++)
True
Read a[i]
Read key
low=0 and
high=n-1
While(low<=high) False
True
mid=(low+high)/2
stop
21
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include<stdio.h>
void main()
{
int n, a[100], i, key, high, low, mid, loc=-1;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of array in sorted order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element to be searched\n");
scanf("%d",&key);
low=0;
high=n-
1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
loc = mid+1;
break;
}
els
e
{
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
}
if(loc>0)
printf("\n The element %d is found at %d ",key,loc);
else
22
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Output
First Run
Enter the size of the array
5
Enter the elements of array in sorted order
10
20
30
40
50
Enter the element to be
searched 40
The element 40 is found at 4
Viva Questions:
1. What is an array/definition of array?
2. What are the types of array?
3. What is a multidimensional array?
4. How to declare and initialize one dimensional array?
5. What are the advantages of an array?
6. What is the difference between array & string?
7. Write the syntax of declaring an array.
23
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Program-6
Step 1: Start
Step 2: Read order of matrix A i.e., m and n.
Step 3: Read order of matrix B i.e., p and q.
Step 4: check if (n!=p), then display matrix multiplication is not possible goto step 9,otherwise
goto step 5
Step 5: Read elements of matrix A
Step 6: Read elements of matrix B
Step 7: Find A x B .by using c[row][col]= c[row][col]+a[row][k]*b[k][col];
Step 8: Display matrix A
Step 9: Display matrix B
Step 10: display matrix C, i.e A x B .
Step 11:: Stop.
24
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
Flowchart
Start
Read m,n,p,q
False
False
For(row=0;row<m;row++)
Stop
True
False For(col=0;col<n;col++)
Read a[row][col]
False
For(row=0;row<p;row++)
True
False
For(col=0;col<q;col++)
True
Read b[row][col]
For(row=0;row<m;row++)
For(k=0;k<n;k++)
True
C2
C[row][col]=c[row][col]+ a[row][k]*b[k][col];
C2
False
For(row=0;row<p;row++)
True
False
For(col=0;col<q;col++)
True
Print b[row][col]
False
For(row=0;row<m;row++)
False
True
For(col=0;col<n;col++)
True
Print a[row][col]
False
For(row=0;row<m;row++)
False True
For(col=0;col<q;col++)
True
#include<stdio.h>
#include<stdlib.h
> int main( )
{
int m,n,p,q,row,col,k,a[3][3],b[3][3],c[3][3];
printf("Enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("enter order of matrix B\
n"); scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix Multiplication is not possible\n");
exit(0);
}
printf("Enter the elements into matrix A\n");
for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
scanf("%d",&a[row][col]);
}
}
printf("Enter the elements into matrix B\n");
for(row=0;row<p;row++)
{
for(col=0;col<q ;col++)
{
scanf("%d",&b[row][col]);
}
}
for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
c[row][col]=0;
for(k=0;k<n;k++)
{
c[row][col]= c[row][col]+a[row][k]*b[k][col];
}
}
}
printf("The elements of matrix A are\n");
for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
printf("%3d",a[row][col]);
}
printf("\n");
}
printf("The elements of matrix B are\n");
for(row=0;row<p;row++)
{
for(col=0;col<q;col++)
{
printf("%3d",b[row][col]);
}
printf("\n");
}
printf("Product of Matrix A and B is\n");
for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
printf("%3d",c[row][col]);
}
printf("\n");
}
}
Output:
Viva Questions:
1. How to initialize two dimensional arrays?
2. How to pass a two dimensional array as function parameter?
3. How the memory is allocated for two dimensional array
4. Write the program to add and subtract two matrices.
5. Program to find the transpose of a matrix.
6. Program to find determinants of a matrix.
7. Program to find the diagonal elements of a matrix.
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.
Step 1: Start
Step 2: Read angle x
Step 3: Assign y=x
Step 4: Compute the value of x in radians:
x = (3.1412/180.0) * degree
Step 5: Initialise:
i. sum = x
ii. t= x
iii. i=1
Step 6: Compute:
i. i=i+2
ii. t = ( - t * x * x ) / ( (i-1) * i )
iii. Sum = sum + t
Step 7: Check if the absolute value term is greater than 0.00005. If true goto Step 6 else goto
Step 8
Step 8 : Display sine using iteration : sum
Step 9 : Assign res=sin(x);
Step 10 : Display sine using library function: sin(x)
Step 11: Stop
Assign y=x
Compute X=(3.1412/180.0)*degree
Initialize Sum=x
t=x
i=i+2
t=(-t*x*x)/((i-1)*i)
res=sin(x)
#include <stdio.h>
#include
<math.h> int
main()
{
int i,degree,n;
float x,sine=0,num,den=1;
printf("Enter angle in degrees & no. of terms\n");
scanf("%d%d",°ree,&n);
x=degree*3.142/180;
num=x;
for(i=1;i<=n;i++)
{
sine=sine+num/den;
num= -num*x*x;
den=den*(2*i)*(2*i+1);
}
printf("sine by series=%f\n",sine);
printf("Using inbuilt function sin =
%f",sin(x)); return 0;
}
Output:
Enter the angle 30
sine by series=0.500000
Viva Questions:
Program-8
Step 1: START
Step 2: Read unsorted array n elements in to a[i], where i is the index value.
Step 3: Initialize index i=0.
Step 4: Check if i is less than n. if true, goto step 5. Otherwise goto step output array.
Step 5: initialize index j to zero.
Step 6: check if j is less than (n-i-1). If true goto step 7. Otherwise increment j and goto step 4.
Step 7: inside the for loop check if a[j] is greater than a[j+1](i.e., adjacent elements are compared). If
true swap elements using temporary variables. Otherwise goto step 6.
Step 8: Output the sorted array elements using for loop.
Step 9: Stop
Flowchart
Start
Read n
False
For(i=0;i<n;i++)
True
Read a[i]
False
For(i=0;i<n;i++)
True
False
For(j=0;j<(n-i)-1;j++)
True
a[j]>a[j+1]) False
True
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp
True False
For(i=0;i<n;i++)
print a[i]
Stop
#include<stdio.h>
int main()
{
int i,j,n,temp;
int a[20];
printf("enter the value of
n"); scanf("%d",&n);
printf("Enter the numbers in unsorted order:\
n"); for(i=0;i<n;i++)
scanf("%d", &a[i]);
// bubble sort logic
for(i=0;i<n;i++)
{
for(j=0;j<(n-i)-1;j++)
{
if( a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Output
Enter the value of n
5
Enter the numbers one by one in unsorted order:
20
10
30
50
40
The sorted array is
10
20
30
40
50
Viva Questions:
Program-9
Step 1: Start
Step 2: press 1-compare 2-concatenate 3-length of string, if press 1 goto step 3, if press 2 goto step 4,
if press 3 goto step 5,
Step 3: Read String1 and String 2 and Comparison function by using strcmp built in function is used.
if res=0 then print both strings are equal, else print strings are not equal and goto step 4.
Step 4: Read String1 and String 2 and find concatenation of two strings using string handling
function strcat( ) and display string and return back to main function.
Step 5: Read String1and call function to find the length of string by calling function length(*string)
Step 6: if digit=1 then goto step 2 otherwise goto step 7
Step 7: stop.
Flowchart
Start
Input a,b
do loop
Read N
Switch(n)
1 2 3
Read str1 & str2 Read str1 & str2 Read str1 Default entered
wrong choice
C1 C2 C3
Read digit
loop while(digit==1
False
Stop
C1 C2
res= strcat(s1,s2)
i= strcmp(s1,s2)
Prints s1
If(i==0)
stop
Strings equal Strings not equal
stop C3
Length(int *s1)
stop
#include<stdio.h>
#include<string.h>
void compare(char [ ],char [ ]); .
void concat(char [ ],char [ ]);
void length(char *[ ]);
void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
{
case 1:printf("enter first
string=");
scanf("%s",str1);
printf("enter second
string="); scanf("%s",str2);
compare(str1,str2);
break;
case 2: printf("enter first string=");
scanf("%s",str1);
printf("enter second
string="); scanf("%s",str2);
concat(str1,str2);
break;
case 3:printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default: printf("wrong
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
choice"); break;
}
printf("\n Do you want to continue(1/0)?
"); scanf("%d", &digit);
}while(digit==1);
}
void compare(char str1[ ],char str2[ ])
{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("strings are equal\n ");
else
printf("string are not equal\n");
}
void concat(char str1[ ],char str2[ ])
{
strcat(str1,str2);
printf("concatenate string=%s",str1);
}
void length(char *str1[ ])
{
int len;
len=strlen(str1);
printf("the length of string=%d",len);
}
Output
press 1-compare 2-concatenate 3-length of string
enter your choice=1
enter first string=ram
enter second
string=Ram string are
not equal
Do you want to continue(1/0)? 1
press 1-compare 2-concatenate 3-length of string
enter your choice=2
enter first
string=RAM
enter second string=kumar
concatenate string=RAMkumar
Do you want to continue(1/0)? 1
press 1-compare 2-concatenate 3-length of string
enter your choice=3
enter string=ram the length of string=3
Do you want to continue(1/0)? 1
press 1-compare 2-concatenate 3-length of
string enter your choice=4
wrong choice
Do you want to continue(1/0)? 0
Viva-Voce Question
1. What is string?
2. How to declare string?
3. What are the string manipulation function?
4. What is gets() and puts() function in string?
Program-10
Implement structures to read, write and compute average- marks of the students,
list the students scoring above and below the average marks for a class of N students.
Step 1: START
Step 2: Read the number of students
Step 3: For each student, read the student id, name and marks for all subjects.
Step 4: Calculate the average of the marks and store in the avg field.
Step 5: Print results.
Step 6: Initialise loop
Step 7: Read the average of each student
Step 8: Check if avg>35.00
Step 9: If yes print result else go to next iteration
Step 10: Initialise loop
Step 11: Read average of each student
Step 12: Check if avg<35.00
Step 13: If yes print result else go to next iteration
Step 14:STOP
Flowchart
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
Output
Enter the number of student=2
Enter the detail of 1 students
Enter USN=101
Enter Name=Ram
Enter the three subject score 10 21 15
Enter the detail of 2 students
Enter USN=102
Enter Name=Kumar
Enter the three subject score 11 9 10
Ram has scored above the average marks
Kumar has scored below the average marks
Viva-Voce Questions
1. What is structure?
2. How to declare a structure?
3. What is structure member?
4. What is difference between array and structure?
5. What is nested structure?
6. What is typedef?
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.
Step1: START
Step2:Read n
Step3:for each value of n read x
Step4: initialize sum=0, i=0
Step5: for each value of n and i, Compute sum using sum=sum+(*(x+i)-mean)*(*(x+i)-mean)
Step 6: using sum value compute variance=sum/n and deviation=sqrt(variance)
Step 7: display mean, variance,
deviation Step 8: Stop
.Flowchart
Start
Read n
False
for (i=0;i<n;i++)
True
Read (X+i)
Sum=0
for (i=0;i<n;i++)
sum=sum+(*(x+i)-mean)*(*(x+i)-mean)
variance=sum/n
deviation=sqrt(variance)
Print mean,variance
Print deviation
Stop
#include<stdio.h>
#include<math.h>
int main()
{
int n , i;
float x[20],sum,mean;
float variance ,
deviation;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("enter %d real values \n",n);
for (i=0;i<n;i++)
{
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++)
{
sum= sum+*(x+i);
}
printf("sum=%f\n",sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;
deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
}
Output:
Viva Questions:
1. Define pointer
2. Define array of pointer
3. Difference between (x+i) and *(x+i)
4. Define array
Program-12
Write a C program to copy a text file to another, read both the input file name and target
file name.
Flow chart
Start
Declaration
*fp1,*fp2,ch,fname1[100],fnam22[100]
Read fname1
Initialize
fp1=fopen(fname1,”r”)
If fp1==NULL
Initialize fp2=fopen(fname2,”w”)
while ch=fgetc(fp1)!
=EOF
fputc(ch,fp2)
fclose fp1
fclose fp2
stop
Dept. of ISE, KNSIT, Bangalore
Principles of Programming using C [22POP13\23]
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *fp1,*fp2;
int ch;
char fname1[100],fname2[100];
printf(“\n enter file name to be copied \n”);
scanf(“%s”,fname1);
fp1=fopen(fname1,”r”);
if(fp1==NULL)
{
printf(“\n input file %s doesn’t exist \n”,fname1);
exit(0);
}
printf(“\n enter target file name\n”);
scanf(“%s”,fname2);
fp2=fopen(fname2,”w”);
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp2);
}
printf(“\n file %s successfully created\n”,fname2);
fclose(fp1);
fclose(fp2);
return 0;
}
Output:-
Viva Questions:-
1. Define file?
2. Write syntax to open a file with example
3. Write syntax to read a file with example.
4. Write syntax to close a file with example.
5. What are the ways to copy from one file to another file?