APJ
ABDUL KALAM TECHNOLOGICAL UNIVERSITY
SECOND
SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017
CS
100 COMPUTER PROGRAMMING (CS, IT)
SCHEME
OF EVALUATION
PART
A
1
|
An
identifier is a sequence of characters invented by the programmer or to
identify or name a specific object. The sequence of characters may be
letters, digits, and special character ‘_’known as an underscore
Rules:
i)
Identifiers
should start with alphabets.
ii)
Identifiers are case sensitive
iii)
A numeric digit should not be the first character
iv)
Identifier name should not be a keyword
v)
Identifier may be of any reasonable length
|
1mark
2mark
|
||||||||||||||||||||||||||||||
2
|
Associativity
defines the direction, left to right or right to left in which operator act
upon its operands
Unary
operators have associativity is from right to left.
For
example in the expression &--x,
pre decrement works first and then address of operator works
Direction
+ example
|
1mark
1mark
|
||||||||||||||||||||||||||||||
3
|
Continue
is a loop control statement used in for, while or do-while. After a continue
statement is executed in for, its increment and conditional expression are
evaluated. If condition is true, nested statement are executed again
Example:
for(i=0;
i<5; i++)
{
if(i==2)
continue;
printf(“%d”, i);
}
Output:
0 1 3 4
Here
if i=2, printf statement after continue not executing and it increment the I
and check condition
Working
of continue with example
|
1mark
2mark
|
||||||||||||||||||||||||||||||
4
|
Library
functions are inbuilt functions which are grouped together and placed in a
common place called library files. All library functions are declared in many
header files which are saved as filename.h
Example:
In
header file string.h, there are some string manipulation functions
strlen()-
find string length
strcmp()-
compare strings etc.
Example
|
1mark
1mark
|
||||||||||||||||||||||||||||||
5
|
Before
a function is used, C must know about the parameter types the function
expects to receive and the data type it returns. Therefore in C program, a
user defined functions should normally be declared prior to its use to allow
the compiler to perform type checking on the arguments used in its call
statement or calling construct. Syntax is:
return_typefunction_name(data_type
variable1, data_type variable2, ……);
float[]
function_name(float, int[]);
|
2mark
1mark
|
||||||||||||||||||||||||||||||
6
|
#include<stdio.h>
void
main()
{
char a[10];
char *b;
int length(char *);
printf(“Enter string\n”);
scanf(“%s”,
a);
b=a;
printf(“length is %d”, length(b));
}
int
length(char *p)
{
int i=0;
while(*p!=’\0’)
{
i++;
p++
}
return(i);
}
|
3mark
|
||||||||||||||||||||||||||||||
7
|
data_typearray_name
[row][column]={value1,value2,………};
Example:
int a[2][2]={1,2,3,4};
Or
int
a[2][2]={(1,2),(3,4)};
if
the initializer list include all the values similar to size of array then we
can omit inner braces
|
2mark
|
||||||||||||||||||||||||||||||
8
|
Enumeration
data types are data items whose values may be any member of a symbolically
declared set of values. These values are integer constants.
enumtag_name{member1,member2,…..,memberN}variable1,…..variableX;
Eg:
enum days{mon, tues, wed, thrs, fri, sat} d1;
In
this example the values for mon, tues,……are 0,1,2…… We can assign values to
variables
Eg:
enum days {mon=10, tues=60, wed=20, thrs, fri, sat}
Here
the values for thrs=60+1=61, fri=62, sat=63
Value
assignment with example
|
1.5mark
1.5mark
|
||||||||||||||||||||||||||||||
9
|
*ptr++ - ++ has higher precedence than *. ptr++
increment the pointer to second location of array. *ptr++ returns second
element in array
(*ptr)++ - expression inside parenthesis evaluated
first, it return first element in array and increment the value by one
|
1.5mark
1.5mark
|
||||||||||||||||||||||||||||||
10
|
For
a structure pointer
structtag_name
{
data_type
member1;
data_type
member2;
…..
data_typememberN
;
}*ptr;
To
access member variable using pointer ptr we use (*ptr).member1. Bracket is needed to avoid confusion between .and
* operator. To shorthand this operation we use ptr->member1
|
2mark
|
||||||||||||||||||||||||||||||
11
|
To
open a file we use the function fopen()
FILE *fopen (const char *fname, const
char *mode)
Example
FILE
*fp;
fp=
fopen(“a.txt”,”r”)
r-
used to open a text file for reading
w-
create a file for writing
a-
Append to a text file
Any
3 modes
|
2mark
|
||||||||||||||||||||||||||||||
12
|
size_typefread(void
*buffer, size_type size, size_typenum, FILE *fp);
It
read from the file associated with fp, num number of objects, each of size
size into buffer pointed to buffer and the number of objects actually read.
size_typefwrite(void
*buffer, size_type size, size_typenum, FILE *fp);
It
write to the file associated with fp, num number of objects, each of size
size from buffer pointed to buffer and the number of objects actually write.
|
1.5mark
1.5mark
|
||||||||||||||||||||||||||||||
13
|
Static
storage class makes a variable permanent within the specified region. Local
variables and global variables are allowed in static variables. The default
value of static variable is zero.
void
show()
{
static
int i;
printf(“%d”,i);
i++;
}
void
main()
{
show();
show();
show();
}
Output:
0 1 2
Since
static variable is permanent for a function block each time of call it
increment the previous value of i
|
2mark
|
||||||||||||||||||||||||||||||
14
|
main()
function can be defined using formal parameters. These parameters are called
command line arguments. Arguments are specified when the program is executed.
int
main(int argc, char* argv[])
argc-
no of command line arguments including the command itself. i.e, argc must be
atleast 1
argv-
array of command line arguments
argv[0]
contain the program name
|
3mark
|
||||||||||||||||||||||||||||||
15
|
A
null pointer is a special pointer value that points nowhere.
data_type *ptr= NULL
or data_type *ptr=0
|
2mark
|
||||||||||||||||||||||||||||||
16
|
Indirection
opertator is *. It is called value at address operator. It returns the value
stored at a particular address.
int
i=5;
int
*ptr;
ptr=&i;
printf(“%d”,
*ptr) – returns the value at &i. i.e., 5
|
2mark
|
||||||||||||||||||||||||||||||
PART B
|
||||||||||||||||||||||||||||||||
17
|
a)
int fact (int n )
{
int k, f=1;
for (k=1;k<=n; k++)
{
f=f*k;
}
return f;
}
void
main()
{
int
i, n;
float
sum=0;
printf(“Enter
limit”);
scanf(“%d”,
&n);
for(i=0;i<n;
i++)
sum=sum+((i+1)/fact(2*i+1));
printf(“Sum is %d”,sum);
}
b)
Type casting tell the compiler to represent the
value of expression in certain way. Type casting is under the control of
programmer. To cast a variable from one type to another
(new_ type)
variable;
For
example to type cast int x to float
(float)x;
Let
int a=5,b=2;
c=a/b
results 2
if
c=(float)a/b, it results 2.500000
|
5mark
3mark
|
||||||||||||||||||||||||||||||
18
|
a)
Call by value-value of actual parameter is passed
to formal parameter. Any change in formal parameter not affect the value of
actual parameters.
void
swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
void
main()
{
int x=2,y=5;
printf(“X and Y before swap is %d %d”, x,
y);
swap(x, y); // call by value
printf(“X and Y after swap is %d %d”, x,
y);
}
Output:
X and Y before swap is 2 5
X and Y after swap is 2 5
Call
by reference-address of actual parameter is passed to formal parameter. Any
change in formal parameter affect the value of actual parameters.
void
swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
* y=t;
}
void
main()
{
int x=2,y=5;
printf(“X and Y before swap is %d %d”, x,
y);
swap(&x, &y); // call by address
printf(“X and Y after swap is %d %d”, x,
y);
}
Output:
X and Y before swap is 2 5
X and Y after swap is 5 2
b)
Bitwise AND(&)- true only if both bits are
set.
2&3=10&11=10=2
Bitwise
OR(|)- true if either of the two bits
set.
2&3=10|11=11=3
|
6mark
2mark
|
||||||||||||||||||||||||||||||
19
|
a)void
main()
{
int
n, i, j, min;
int *a;
scanf
(“%d”, &n);
a=(int*)malloc(n*sizeof(int));
printf(“Enter
elements\n”);
for(i=0;i<n; i++)
scanf(“%d”,(a+i));
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1; j<n; j++)
if(*(a+j)<*(a+min)
min=j;
if (min!=i)
{
t=*(a+min);
*(a+min)=*(a+i);
*(a+i)=t;
}
}
for(i=0;i<n; i++)
printf(“%d”,*(a+i))
}
Selection
sort with array
b)typedef
keyword allows the programmer to create a new data type name for an existing data
type
typedef
existing_type new_type;
Example:
typedef int id;
To
declare new int variables :
id x, y;
|
6mark
3mark
2mark
|
||||||||||||||||||||||||||||||
20
|
a)
void main()
{
char a[10], b[10], c[10];
int i, j;
printf(“Enter first string\n”);
scanf(“%s”,
a);
printf(“Enter
second string\n”);
scanf(“%s”,
b);
for(i=0;a[i]!=’\0’;i++)
c[i]=a[i];
for(j=0;b[j]!=’\0’;j++)
{
c[i]=b[j];
i++;
}
c[i]=’\0’;
printf(“Concatenated
string\n”);
printf(“%s”,c);
}
b)
Structure and union are user defined data
structures. Both are store different type data in single unit. In both the
data members are accessed using dot operator.
Structure
use the keyword struct and union use keyword union in declaration. Size of
structure is sum of size of all member variables. Size of union is the size
of largest member function. Therefore
union keep only single member in its memory
Explanation
with example
|
5mark
3mark
|
||||||||||||||||||||||||||||||
21
|
a)int
search(int x[], int k, int l,int h)
{
int
mid;
if(l>h)
return -1;
mid=(l+h)/2;
return(k==x[mid]?mid:k<x[mid]?search(x,k,l,mid-1):search(x,k,mid+1,h));
}
void main()
{
int
i, k, l, h, n, x[10];
printf(“Enter limit”);
scanf(“%d”,
&n);
for(i=0; i<n; i++)
scanf(“%d”,
&x[i]);
printf(“Enter key”);
scanf(“%d”,&k);
l=0;
h=n-1;
printf(“Element present in %d “, search(x,k,l,h);
}
b)using
keyword const we declare constants in C
constdata_type
variable=value;
Example: const int x=5;
using preprocessor directive #define
# define
pi 3.14
|
5mark
3mark
|
||||||||||||||||||||||||||||||
PART C
|
||||||||||||||||||||||||||||||||
22
|
a)#include<conio.h>
void
main()
{
int
**a,**b,**c,i,j,t,m1,n1,m2,n2,k;
clrscr();
printf("Enter
the order of first matrix:");
scanf("%d%d",&m1,&n1);
a=(int**)malloc(m1*sizeof(int*));
for(j=0;j<m1;j++)
{
*(a+j)=(int*)malloc(n1*sizeof(int));
}
printf("Enter
the order of second matrix:");
scanf("%d%d",&m2,&n2);
b=(int**)malloc(m2*sizeof(int*));
for(j=0;j<m2;j++)
{
*(b+j)=(int*)malloc(n2*sizeof(int));
}
c=(int**)malloc(m1*sizeof(int*));
for(j=0;j<m1;j++)
{
*(c+j)=(int*)malloc(n2*sizeof(int));
}
printf("Enter
elements of first matrix");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
scanf("%d",(*(a+i)+j));
}
printf("Enter
elements of second matrix");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
scanf("%d",(*(b+i)+j));
}
printf("\n Matrix A\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
{
printf("%d\t", *(*(a+i)+j));
}
printf("\n");
}
printf("\n Matrix B\n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t", *(*(b+i)+j));
}
printf("\n");
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
*(*(c+i)+j)=0;
for(k=0;k<n1;k++)
*(*(c+i)+j)=*(*(c+i)+j)+ (*(*(a+i)+k) * *(*(b+k)+j));
}
}
printf("\n Matrix A * B\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d\t", *(*(c+i)+j));
}
printf("\n");
}
free(a);
free(b);
free(c);
}
|
10mark
4mark
|
||||||||||||||||||||||||||||||
23
|
a)
void main()
{
FILE *fp1,*fp2,*fp3;
int c;
fp1=fopen (“first.txt”,”r”);
fp3=fopen (“third.txt”,”w”);
c=getc(fp1);
while (c!=EOF)
{
putc(c,fp3);
c=getc(fp1);
}
fclose(fp1);
fclose(fp3);
fp2=fopen (“second.txt”,”r”);
fp3=fopen (“third.txt”,”a”);
c=getc(fp2);
while (c!=EOF)
{
putc(c,fp3);
c=getc(fp2);
}
fclose(fp2);
fclose(fp3);
}
b) Dynamic memory allocation is a way to defer the decision of how much memory is necessary until the program is actually running or give back memory that the program no longer needs.
void*
malloc(size_t size) – request a contiguous block of memory of the given size
in the heap. malloc() returns a pointer to the heap block or NULL if the
request is not satisfied.
calloc()-
works like malloc, but initializes the memory to zero if possible. The
prototype is
void* calloc(size_t count, size_t size)
This
fuction allocates a block long enough to contain an array of count element,
each of size size. Its contents are cleared to zero before calloc returns
|
10 mark
4mark
|
||||||||||||||||||||||||||||||
24
|
a)
struct Student
{
int
rno;
char name[10];
char gender[10];
float cgpa;
};
void
main()
{
struct
Student s[20];
struct
Student t;
int i, j, n;
printf(“Enter count\n”);
scanf(“%d”,
&n);
printf(“Enter details\n”);
for(i=0; i<n; i++)
{
scanf
(“%d”, &s[i]. rno);
scanf
(“%s”, s[i]. name);
scanf
(“%s”, s[i]. gender);
scanf
(“%f”, &s[i]. cgpa);
}
printf
(“Ranklist…………….”);
for(i=0; i<n; i++)
for(j=0; j<n-1-i; j++)
if(s[j].cgpa>s[j+1].cgpa)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
for(i=0;
i<n; i++)
{
printf
(“%d”, s[i]. rno);
printf
(“%s”, s[i]. name);
printf
(“%s”, s[i]. gender);
printf
(“%f”, s[i]. cgpa);
}
printf(“CGPA less than 7”)
for(i=0; i<n; i++)
if(s[i].cgpa<7)
{
printf (“%d”, s[i]. rno);
printf (“%s”, s[i]. name);
printf (“%s”, s[i]. gender);
printf (“%f”, s[i]. cgpa);
}
}
b)
Scope of a variable relate to the accessibility,
duration of existence, and boundary of usage of the variable. Based on scope
variables are classified into 2:
Local
variables: variables are declared within the function body. They are
automatically created at the point of their declaration within the function
body. They are unknown to other functions and to the main program.
Global
variables: Declared outside of all functions of a program and accessible by
any of these functions
int x=10,y=20; // global variable
void fnsum()
{
int sum; //local
variable
sum=x+y;
printf("Sum=%d",sum);
}
void main()
{
fnsum();
}
|
10mark
4mark
|
Download computer Programming ebooks for free
ReplyDeletewww.khanbooks.net