Bpops103-C Lab Manual
Bpops103-C Lab Manual
Bpops103-C Lab Manual
Of
Artificial Intelligence & Machine Learning
LABORATORY MANUAL
Principles of Programming using C – BPOPS103
Semester: I
Vision
• To emerge as one of the finest technical institutions of higher learning to develop engineering
professionals who are technically competent, ethical and environment friendly for betterment of
the society.
Mission
• Accomplish stimulating learning environment through high quality academic instruction,
innovation and industry-institute interface.
Vision
• To develop professionals equipped to build sustainable and intelligent solutions that
effectively interact with the natural intelligence towards creating a digitally empowered
environment for future generations, safeguarding social
ethics.
Mission
• To enable students with the spirit and power of interdisciplinary acumen by Integrating a
world of knowledge into a world of intelligent systems and subsystems.
• Boost academic outcome through place-based education and collaborations with established
research labs and industries.
• Encourage entrepreneurship efforts among students and develop them into great leaders
Course Objectives:
These are sample Strategies; which teachers can use to accelerate the attainment of the various course
outcomes.
1. Lecturer method (L) need not to be only traditional lecture method, but alternative effective
teaching methods could be adopted to attain the outcomes.
2. Use of Video/Animation to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Ask at least three HOT (Higher order Thinking) questions in the class, which promotes
critical thinking.
5. Adopt Problem Based Learning(PBL), which fosters students’ Analytical skills, develop
design thinking skills such as the ability to design, evaluate, generalize, and analyse
information rather than simply recall it.
6. Introduce Topics in main folder presentations.
7. Showthedifferentwaystosolvethesameproblemandencouragethestudentstocome up with their
own creative ways to solve them.
8. Discuss how every concept can be applied to their al world-and when that's possible, it helps
to improve the students' understanding.
9. Use https://pythontutor.com/visualize.html#mode=edit in order to visualize the operations of C
Programs
Functions: Introduction using functions, Function definition, function declaration, function call, return
statement, passing parameters to functions, scope of variables, storage classes, recursive functions. Arrays:
Declaration of arrays, accessing the elements of an array, storing values in arrays, Operations on arrays,
Passing arrays to functions, two dimensional arrays, operations on two-dimensional arrays, two dimensional
arrays to functions, multidimensional arrays, applications of arrays.
Textbook: Chapter 11.1-11.10, 12.1-12.10,12.12
CO4. Explore user-defined data structures like structures, unions and pointers in implementing
solutions
CO5.Design and Develop Solutions to problems using modular programming constructs using
functions
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
121
12321
1234321
n th 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.
SEE shall be conducted jointly by the two examiners of the same institute, examiners are appointed by the
University
All laboratory experiments are to be included for practical examination.
(Rubrics) Breakup of marks and the instructions printed on the cover page of the answer script to be strictly
adhered to by the examiners. OR based on the course requirement evaluation rubrics shall be decided jointly
by examiners.
Students can pick one question (experiment) from the questions lot prepared by the internal /external
examiners jointly.
Evaluation of test write-up/ conduction procedure and result/viva will be conducted jointly by examiners.
General rubrics suggested for SEE are mentioned here, writeup-20%, Conduction procedure and result in -
60%, Viva-voce 20% of maximum marks. SEE for practical shall be evaluated for 100 marks and scored
marks shall be scaled down to 50 marks (however, based on course type, rubrics shall be decided by the
examiners)
Students can pick one experiment from the questions lot with equal choice to all the students in a batch.
Student should develop an algorithm, program, execute and demonstrate the results with appropriate output
for the given problem.
Change of experiment is allowed only once and 15% Marks allotted to the procedure part to be made zero.
The duration of SEE is 02 hours
• Three Tests each of 20 Marks; after the completion of the syllabus of 35-40%, 65-70%, and 90100%
respectively.
• Two Assignments/two quizzes/ seminars/one field survey and report presentation/one course project
totalling 20 marks.
Total Marks scored (test + assignments) out of 80 shall be scaled down to 30 marks
CIE for the practical component of the IC
• On completion of every experiment/program in the laboratory, the students shall be evaluated and
marks shall be awarded on the same day. The 15 marks are for conducting the experiment and
preparation of the laboratory record, the other 05 marks shall be for the test conducted at the end of
the semester.
• The CIE marks awarded in the case of the Practical component shall be based on the continuous
evaluation of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all
experiments’ write-ups are added and scaled down to 15 marks.
• The laboratory test (duration 03 hours) at the end of the 15th week of the semester /after completion of
all the experiments (whichever is early) shall be conducted for 50 marks and scaled down to 05 marks.
1. The question paper will have ten questions. Each question is set for 20 marks. Marks scored shall
beproportionally reduced to 50 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a maximum of
3sub-questions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module
Textbooks
1. Computer fundamentals and programming in c, “Reema Thareja”, Oxford University, Second edition, 2017.
Reference Books:
1. E. Balaguru swamy, Programming in ANSI C, 7th Edition, Tata McGraw-Hill.
2. Brian W. Kernighan and Dennis M. Ritchie, The ‘C’ Programming Language, Prentice Hall of India.
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:
• Input: It may accept a zero or more inputs.
• Output: It should produce at least one output (result).
• Definiteness: Each instruction must be clear, well defined and precise. There should not be any
ambiguity.
• 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:
Flowchart:
Connector
Flow of control
Algorithm Flowchart
peri
Program Output
Algorithm:
Step 1: Start
Step 2: Read the values of a and b.
Step 3: Read Ch
Step4: Verify ch value by using switch statement if ch=1 then goto step 5 or if ch=2 then goto step
6 or if read ch is 3 then goto step 7 or if read ch is 4 then goto step 8 or if read ch is 5 then
goto step 9 or otherwise goto step 10.
Step 5: if ch=1 then compute res=a+b and goto step 11.
Step 6: if ch=2 then compute res=a-b and goto step 11.
Step 7: if ch=3 then compute res=a*b and goto step 11.
Step 8: if ch=4 then compute res=a/b and goto step 11.
Step 9: if ch=5 then compute res=a%b and goto step 11.
Step 10: if ch value is other than above options then print wrong choice entered then
goto step 12.
Step 11: Print res and goto step 12
Step 12: stop.
Flowchart
Start
Input a,b,ch
Switch(ch)
1 2 3 4 5 Default
Print res
Stop
Program
#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\”);
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=%d\n”,res);
}
Output
Enter two numbers: 10 20
1=Add, 2=Sub, 3=Mul, 4=Div, 5=Rem
Enter your choice:1
Result=30
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?
2) Develop a program to compute the roots of a quadratic equation by accepting the coefficients.
Print the appropriate messages.
ALGORITHM
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
root1 = (-b)/(2*a)
root2=root1
Step 6: Check if disc is greater than zero or not. If true, then go to Step 7.
root1 = (-b+sqrt(disc))/(2*a)
root2 = (-b-sqrt(disc))/(2*a)
Step 9: Stop.
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
Program
#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);
}
else
{ 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);
}
}
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:
a. What is quadratic Equation?
b. What is math.h ? why it is used in C program?
c. What are decision making statements in C language?
d. Write the syntax of “ if ”statement?
e. Difference between if and switch statements?
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 rupees 1 per unit. All users are charged a minimum of rupees 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 .
Flowchart
Start
Inputname
Inputunits
If(units<=200) amt=100+(0.8*units);
True
False
True
amt=100+(200*0.8)+(100*0.9)+(un-i3ts00)*1;
If(units>400)
True
amt=amt*1.15
Printname,units,charge
Stop
Program
#include <stdio.h> void main()
{ char name[10];
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;
Output
First Run
Enter your name and unit Consumed: Siri 52
Second Run
Enter your name and unit Consumed: Rajesh 460
Viva Questions:
a. Difference between float and double data types.
b. Write syntax of for loop?
c. What is the use of break statement?
d. Difference between continue and break statement?
1
121
12321
1234321
nth row
Program
#include<stdio.h>
int main()
{ int n, r, c, k, a;
printf("Enter number of rows: ");
scanf("%d",&n);
for(r=1;r<=n;r++) {
for(c=1;c<=n-r;c++)
printf(" ");
for(k=1;k<=(2*r-1);k++) {
if(k<r) printf("%d",k);
else if(k==r {
printf("%d",k);
a=k;}
else printf("%d",--a);
}
printf("\n");
}
return 0;
}
Output
Enter number of rows:4
1
121
12321
1234321
ALGORITHM:
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
Program
#include<stdio.h>
void main()
{
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{ mid=(low+high)/2;
if(key==a[mid])
{
loc = mid+1;
break;
}
else
{
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
printf("\nThe search is unsuccessful");
}
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:
a. What is an array/definition of array?
b. What are the types of array?
c. What is a multidimensional array?
d. How to declare and initialize one dimensional array?
e. What are the advantages of an array?
f. What is the difference between array & string?
g. Write the syntax of declaring an array.
Algorithm
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.
C2
False
For(row=0;row<p;row++)
True
False
For(col=0;col<q;col++)
True
Printb[row][col]
False
For(row=0;row<m;row++)
False
True
For(col=0;col<n;col++)
True
Printa[row][col]
False
For(row=0;row<m;row++)
False True
For(col=0;col<q;col++)
True
Printc[row][col] Stop
Program
#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 matrixB\n");
scan("%d%d",&p,&q);
if(n!=p)
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];
{ for(col=0;col<n;col++)
{ printf("%3d",a[row][col]);
}
printf("\n");
DEPARTMENT OF AIML BMSIT&M, BANGALORE 30
Principles of Programming using C BPOPS103
}
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");
for(row=0;row<m;row++)
{ for(col=0;col<q;col++)
printf("%3d",c[row][col]);
printf("\n");
Output:
Enter the order of matrix A
2 2
Enter the order of matrix B
2 2
Enter the elements into matrix A
1234
Enter the elements into matrix B
5678
Elements of matrix A are
12
34
Elements of matrix B are
56
78
Product of matrix A and B is
19 22
43 50
Viva Questions:
a. How to initialize two dimensional arrays?
b. How to pass a two dimensional array as function parameter?
c. How the memory is allocated for two dimensional array
d. Write the program to add and subtract two matrices.
e. Program to find the transpose of a matrix.
f. Program to find determinants of a matrix.
g. Program to find the diagonal elements of a matrix.
7) Develop a Program to compute Sin(x) using Taylor series approximation. Compare your result
with the built- in Library function. Print both the results with appropriate messages.
Algorithm:
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: sum = x t= x i=1
Step 6: Compute: i = i + 2
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 got 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
Flowchart
Start
Input angle x
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)
True
While(fabs(t)>0.00005)
False
Print sum
res=sin(x)
Print sum1
Stop
Program
#include <stdio.h>
#include <math.h>
int main()
{
int i,degree,n;
float x,sine=0,num,den=1;
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);
return 0;
Output:
Enter the angle 30 sine by series=0.500000
Viva Questions:
8) Develop a program to sort the given set of N numbers using Bubble sort.
Algorithm:
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.
Step7: 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
Readn
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
Program
#include<stdio.h>int
main()
{ Int i,j,n,temp,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++)
{
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:
a) Why the name bubble sort?
9) Write functions to implement string operations such as compare, concatenate, string length.
Convince the parameter passing techniques.
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.
Read digit
loop while(digit==1
False
Stop
Program
#include<stdio.h>
#include<string.h>
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 secondstring=");
scanf("%s",str2);
concat(str1,str2);
break;
case 3:
printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default:
printf("wrong choice"); break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}
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 secondstring=");
scanf("%s",str2);
concat(str1,str2);
break;
case 3:
printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default:
printf("wrong choice"); break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}
{ int i;
i=strcmp(str1,str2);
if(i==0)
else
}
void concat(char str1[ ],char str2[ ])
{ strcat(str1,str2);
printf("concatenate string=%s",str1);
}
void length(char *str1[ ])
{ int len;
len=strlen(str1);
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
Viva-Voce Question
a. What is string?
b. How to declare string?
c. What are the string manipulation function?
d. What is gets() and puts() function in string?
10) Implement structures to read, write, compute average- marks and the students scoring
above and below the average marks for a class of N students.
Algorithm:
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
Program
#include<stdio.h>
struct student
{ char usn[10];
char name[10];
floatm1,m2,m3;
float avg,total;
};
void main()
of student=");scanf("%d",&n);
for(i=0;i<n;i++)
else
printf("\n %s has scored below the average marks",s[i].name);
}
}
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
a. What is structure?
b. How to declare a structure?
c. What is structure member?
d. What is difference between array and structure?
e. What is nested structure?
f. What is typedef?
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
FLOW CHART
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
Program
#include<stdio.h>
#include<math.h>
int main()
{
int n , i;
float x[20],sum,mean;
scanf("%d",&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:
Entre the value of n
5
Enter 5 real values
3
7
23
1
4
sum=38.000000
mean(Average)=7.600000
variance=63.039997
standard deviation=7.939773
Viva Questions:
a. Define pointer
d. Define array
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>
int main()
{
char ch, source_file[25], target_file[25];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
Firstly, you will specify the file to copy and then you will enter the name of target file, You will have to
mention the extension of file also. We will open the file that we wish to copy in read mode and target file
in write mode.