0% found this document useful (0 votes)
17 views

2022 IA2 SETB Scheme of Evaluation C

Uploaded by

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

2022 IA2 SETB Scheme of Evaluation C

Uploaded by

Vidya Dhanush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

K.S.

SCHOOL OF ENGINEERING AND MANAGEMENT, BANGALORE - 560109


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SESSION: 2022-2023 (EVEN SEMESTER)
II SESSIONAL SCHEME OF EVALUATION
SET-B

Degree : B.E Semester : II (D&H)


Branch : CV & ME Course Code : BESCK104E
Course Title : Introduction to C Programming Date : 14-08-2023
Duration : 75 Minutes Max Marks : 25

Note: Answer ONE full question from each part

Q.
Questions with Scheme & Solution Marks
No.

for loop:
1(a)
It is a pretest loop and also known as entry controlled loop.
Sol
“for” keyword is used here.
Here, the head of the for loop contains all the three components that is
initialization, condition and Updation.
General syntax:
for ( initialization; test-condition; increment/decrement)
{
Statement block;
} Syntax &
example
Statement y; [3marks]
The execution of for statement is as follows. Explanat
1. First the control variable will be initialized. (Example i=1) ion
2. Next the value of control variable is tested using test condition. (i<=n) [2marks]
3. If the condition is true the body of the loop is executed else terminated.
4. If loop is executed then control variable will be updated (i++) then, the
condition is checked again.
#include<stdio.h>
int main( )
{
int i, j;
for(i=1;i<=5;i++)
{
printf(“\n”);
for(j=1; j<=i; j++)
printf(“ * ”) ;
}
return 0;
}

1(b) Continue
 It terminates only the current iteration of the loop.
Sol  Continue can appear in looping statements.
 Keyword used is continue
Syntax Example
while(condition) #include<stdio.h> void main( )
Syntax
{ { with
Statements; int i; example
if(condition) for(i=1; i<=5; i++) [4mark]
continue; { Explanat
Statements; if(i= =3) ion
} continue; printf(“%d”, i) [1marks]
}
}
OUTPUT 1 2 4 5
f do…. while loop: It is a post-test loop (also called exit controlled loop) it
2(a)
Sol has two keywords do and while.
The General syntax:
do {
statement-block;
} while (condition);

In this loop the body of the loop is executed first and then test condition
is evaluated.
If the condition is true, then the body of loop will be executed once
again. This process continues as long as condition is true.
When condition becomes false, the loop will be terminated and control
comes out of the loop.
Example: #include<stdio.h> Syntax &
void main( ) example
{ [3marks]
int i=1, sum=0; Explanat
do
ion
{
sum=sum+i; [2marks]
i=i++;
} while (i<=5);
printf(“%d”, sum);
}
2(b) Break Statement
Sol In C, break statement is used to terminate the execution of the nearest en-
closing loop in which it appears. Syntax &
 It terminates the execution of remaining iteration of loop. example
 A break can appear in both switch and looping statements. [4marks]
Explanati
 Keyword is break on
[1mark]

Syntax Example
while(condition) #include<stdio.h> void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
break; {
Statements; if(i= =3)
} break; printf(“%d”, i)
}
}
OUTPUT 1 2
PART B
A recursive function is defined as a function that calls itself to solve a
smaller version of its task until a final call is made which does not require
a call to itself.
#include <stdio.h>
int Fact(int);
int main( )
{
int num, factorial; Declaratio
printf(“\n Enter the number: “); n of
variables
scanf(“%d”, &num);
3(a) and logic
factorial = Fact(num); [3marks]
sol
printf(“\n Factorial of %d %d”, num, factorial); To print
return 0; output
} [2marks]
int Fact(int n)
{
if (n== -1)
return 1;
else
return (n*Fact (n-1));
}
3(b) Storage class defines the scope (visibility) Explanati
#include <stdio.h>
sol and lifetime of variables and/or functions void func1()
on with
declared within a C program. { example
auto Storage Class int a = 10 ; [5marks]
printf(“\n a =\%d”,a) ;
The auto storage class specifier is used to // auto integer local to func1()
explicitly declare a variable with automatic }
storage. It is the default storage class for void func2()
int a = 20 ;
variables declared inside a block. printf(“\n a =\%d”, a);
register Storage class // auto integer local to func2()
When a variable is declared using register }
void main()
as its storage class, it is stored in a CPU {
register instead of RAM. Since the variable int a = 30 ;
is stored in a register, the maximum size // auto integer local to main()
func1();
of the variable is equal to the register size. func2();
int exp(int a, int b) printf(“\n a = %d”, a);
{ }
register int res=1;
int I;
for ( I = 1 i<=b;i++)
res = res*a;
return res;
}
extern Storage Class
The extern storage class is used to give a reference of a global variable
that is visible to all the program files. Such global variables are declared
like any other variable in one of the program files. When there are
multiple files in a program and you need to use a particular function or
variable in a file apart from which it is declared, then use the extern
keyword. To declare a variable x as extern write,
extern int x;
#include <stdio.h>
#include <FILE2.C>
// Programmer’s own header file
int x;
void print(void);
int main()
{
x = 10;
printf(“\n x in FILE1 = %d”, x); return 0;
print();
}
#include <stdio.h>
extern int x;
void print()
printf(“\n x in FILE 2 = %d”, x);
}
Main()
{
// Statements
}
static Storage Class
While auto is the default storage class for all local variables, static is the
default storage class for all global variables. Static variables have a
lifetime over the entire program, i.e., memory for the static variables is
allocated when the program begins running and is freed when the
program terminates. To declare an integer x as static, write
static int x = 10;
3(c) 1. Pass by value: 2 Types
sol In pass-by-value the calling function sends the copy of parameters (Actual with
example
Parameters) to the called function (Formal Parameters). The changes do not affect [2.5marks
the actual value. each]
2. By Passing parameters as address (Pass by address)
In pass by reference the calling function sends the address of parameters (Actual
Parameters) to the called function (Formal Parameters). The changes affect the
actual value.
In this method the values a and b have been changed or swapped.
4(a) Traversing an Array
sol Traversing an array means accessing every element of the array for a
specific purpose IA is an array of homogeneous data elements, then
traversing the data elements can include printing every clement, counting
the total number of elements, or performing any process on these
elements. Since an array IS a linear data structure (because all its
elements form a sequence), traversing its elements is straightforward.
Inserting an Element in an Array
Inserting an element in an array means adding a new data element to an
already existing array. If the element has to be insertion at the end of the
existing array, then the task of insertion is quite simple. We just have to
add I to the upper_bound and assign the value. Here we assume that the Explanati
memory space allocated for the array is still available. For example. it an on with
array is declared to contain 10 elements, but currently, it is having only 8 example
elements, then obviously there is a space to accommodate two or more [5 marks]
elements.
Merging Two Arrays
Merging two arrays in a third array means first copying the contents of
the first array into the third array and then copying the contents of the
second array into the third array. Hence, the merged array contains the
contents of the first array followed by the contents of the second
array. If the arrays are unsorted then merging the arrays is very simple as
one just needs to copy the contents of one array into another. But merging
is not a trivial task when the two arrays are sorted and the merged array
also needs to be sorted.
Linear Search
Linear search, also called sequential search, is a very simple method used
for searching an array for a particular value. It works by comparing every
element of the array one by one in sequence until a match is found. Linear
search is mostly used to search an unordered list of elements
4(b) #include<stdio.h>
sol
void main( )
{
int a[50],i,j,temp,n;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The given array elements are \n");
Declaratio
for(i=0;i<n;i++) n of
variables
printf("%d\t",a[i]);
and logic
for(i=1;i<n;i++) { [3marks]
To print
for(j=0;j<n-i;j++) { output
[2marks]
if(a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; }
}
}
printf("\nThe array after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
4(c) ELEMENTS OF USER-DEFINED FUNCTIONS Syntax
sol There are three elements that are related to functions: and
i. Function definition explanatio
ii. Function call n
[5 marks]
iii. Function declaration/Function prototype

1.Function definition: It is an independent program module that is specially


written to implement the requirements of the function.

Function header (First three elements)


Function body (Second three elements)
i.Function Header: It consists of three parts: Function type, function name and
list of paraemeters. The semicolon is not present at the end of header.
Function type:
The function type specifies the type of value that the function is expected to
return to the calling function.
If the return type or function type is not specified, then it is assumed as int.
If function is not returning anything, then it is void.

Function name: The function name is any valid C identifier and must follow
same rules as of other variable.
List of Parameters: The parameter list declares the variables that will receive
the data sent by the calling program which serves as input and known as Formal
Parameter List.
ii. Function Body: The function body contains the declarations and statements
necessary for performing the required task. The body is enclosed in braces,
contains three parts:

1.Local declarations that specify the variables needed by the function.


2. Function statements that perform the task of the function.
3. A return statement that returns the value evaluated by the function.

ii. Function call: The function should be invoked at a required place in


the program which is called as Function call.
Function call statement has the following syntax:
function_name(variable1, variable2, …);
Function declaration: The calling program should declare any function that is
to be used later in the program which is called as function declaration.
Syntax: function_type function_name(list of parameters);
Ex: int sum(int,int);
int isprime(int);

Course Incharge HOD CSE IQAC- Coordinator Principal

You might also like