Beruflich Dokumente
Kultur Dokumente
1. Q. WHAT IS PREPROCESSOR?
Ans: The C preprocessor is a macro processor that is used automatically by
the C compiler to transform the program before actual compilation. It is called a
macro processor because it allows the programmer to define macros, which are
brief abbreviations for longer constructs. All preprocessor commands begin
with a pound symbol (#). It must be the first nonblank character, and for
readability, a preprocessor directive should begin in first column.
2. Q. WHAT IS ADT?
Ans: In computer science, an abstract data type (ADT) is a mathematical model
for data types where a data type is defined by its behavior (semantics) from the
point of view of a user of the data, specifically in terms of possible values,
possible operations on data of this type, and the behavior of these operations. A
data type can be considered abstract when it is defined in terms of operations
on it, and its implementation is hidden.
3. Q. Quick Sort :- (Short notes)
Quicksort is an efficient sorting algorithm, serving as a systematic method for
placing the elements of an array in order. It is still a very commonly used
algorithm for sorting. When implemented well, it can be about two or three
times faster than its main competitors, merge sort and heapsort.
Quicksort is a comparison sort, meaning that it can sort items of any type for
which a "less-than" relation (formally, a total order) is defined. In efficient
implementations it is not a stable sort, meaning that the relative order of equal
sort items is not preserved. Quicksort can operate in-place on an array, requiring
small additional amounts of memory to perform the sorting.
Quick sort is a divide and conquer algorithm. Its divided large list in mainly
three parts:
1.
2.
Pivot element.
3.
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
4.Q. Differentiate between iteration and recussion?
For recursion to terminate, each time the recursion method calls itself
with a slightly simpler version of the original problem, the sequence of
smaller and smaller problems must converge on the base case. When the
method recognizes the base case, the result is returned to the previous
method call and a sequence of returns ensures all the way up the line until
the original call of the method eventually returns the final result.
Both iteration and recursion are based on a control structure: Iteration
uses a repetition structure; recursion uses a selection structure.
3
Example:
Example:
1.
2.
#include<stdio.h>
3.
#include<conio.h>
4.
#include<stdlib.h>
5.
struct link
6.
7.
int data;
8.
9.
};
10.
int i=0;
11.
12.
13.
14.
15.
char ch;
16.
start->next=NULL;
17.
node=start;
18.
fflush(stdin);
19.
20.
ch=getchar();
21.
while(ch!='n')
22.
23.
24.
node=node->next;
25.
26.
27.
scanf("%d",&node->data);
28.
node->next=start;
29.
fflush(stdin);
30.
31.
ch=getchar();
32.
i++;
33.
34.
}
}
35.
36.
37.
38.
int count;
39.
node=start->next;
40.
count=i;
41.
42.
while(count)
43.
44.
printf("\t %d",node->data);
45.
node=node->next;
46.
count--;
47.
48.
}
}
49.
7
50.
void main()
51.
52.
char ch;
53.
clrscr();
54.
create_link(node);
55.
56.
display(node);
57.
getch();
58.
creating a new stack frame on top of the current, finished, frame is a waste. A
compiler is said to implement Tail Recursion if it recognizes this case and
replaces the caller in place with the callee, so that instead of nesting the stack
deeper, the current stack frame is reused. This is equivalent in effect to a
"GoTo", and lets a programmer write recursive definitions without worrying
about space inefficiency during execution.
12.Q. C Program to Find GCD of given Numbers using Recursion.
/*C Program to find GCD of given Numbers using Recursion*/
#include <stdio.h>
int gcd(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
}
Input / Output:
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.
Input /Output:
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.
14.Q. Explain Switch statement in C.
A switch statement allows a variable to be tested for equality against a list
of values. Each value is called a case, and the variable being switched on is
checked for each switch case. The expression used in a switch statement
must have an integral or enumerated type, or be of a class type in which the
10
Syntax:
The syntax for a switch statement in C programming language is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
Structure
1.The keyword struct is used to define a
structure
Union
1. The keyword union is used to define a
union.
11
members of union.
5 Altering the value of a member will not affect 5. Altering the value of any of the member will
other members of the structure.
alter other member values.
6. Individual member can be accessed at a time
12
13