Sunday, December 26, 2010

WHICH SORTING TECHNIQUE IS BETTER

Insertion Sort:
* Stable
* O(1) extra space
* O(n2) comparisons, O(n) time when nearly sorted

Selection Sort:
    * Not stable
    * O(1) extra space
    * Θ(n2) comparisons
    * Θ(n) swaps

Bubble Sort: 
    * Stable
    * O(1) extra space
    * O(n2) comparisons and swaps,O(n) when nearly sorted

Quick Sort:
    * Not stable
    * O(lg(n)) extra space (see discussion)
    * O(n2) time, but typically O(n·lg(n)) time

Merge Sort:
    # Stable
    # Θ(n) extra space for arrays (as shown)
    # Θ(lg(n)) extra space for linked lists
    # Θ(n·lg(n)) time

Friday, November 19, 2010

VARIABLES & CONSTANTS IN C .....!!

What is a variable?? Why do we use variables??

Variable is data holder, in which we can store some data and we can modify that data later in the program.

In C programming before we store any data in a variable we need to declare that variable.

declaring a variable:
declaring a variable means telling the compiler that what type of values you want store in that variable. 

datatype variablename ; // declaring a variable 

In C we have 4 types of Data Types:
1. int -  for integer values
2. float - for floating points
3. double - for long floating points such as scientific values
4.char  - for characters

declaring variables example: 

int a;  // tells the compiler that a is a integer type variable in which you want to store integer value.

float f; //

double d;

char c; 

once you declare the variables you can store actual values in those variables.

As I told you variable is data holder. so once you declare any variable C compiler will allocate memory depending on the specified data type.

int a; when do you this one C compiler will allocate 4 bytes of space for this variable called a.

its like this  a
                      

Monday, November 8, 2010

C QUESTION AND ANSWERS: 3


1.Point out the correct statements are correct about the program below?
#include
int main()
{
    char ch;
    while(x=0;x<=255;x++)
        printf("ASCII value of %d character %c\n", x, x);
    return 0;
}
A.
The code generates an infinite loop
B.
The code prints all ASCII values and its characters
C.
Error: x undeclared identifier
D.
Error: while statement missing
Answer: Option D
Explanation:
There are 2 errors in this program.
1. "Undefined symbol x" error. Here x is not defined in the program.
2. Here while() statement syntax error.
Learn more problems on : Control Instructions
Discuss about this problem : Discuss in Forum
2.What will be the output of the program?
#include
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j || ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
A.
1, 2, 0, 1
B.
-3, 2, 0, 1
C.
-2, 3, 0, 1
D.
2, 3, 1, 1
Answer: Option C
Explanation:
Step 1int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;
(++k) is not executed because (-2 && 3) alone return TRUE.
Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one).
Hence the output is "-2, 3, 0, 1".
Learn more problems on : Expressions
Discuss about this problem : Discuss in Forum
3.What will be the output of the program?
#include
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}
A.
6
B.
1
C.
0
D.
-1
Answer: Option B
Learn more problems on : Functions
Discuss about this problem : Discuss in Forum
4.What will be the output of the program?
#include
#define FUN(arg) do\
                 {\
                    if(arg)\
                        printf("IndiaBIX...", "\n");\
                  }while(--i)

int main()
{
    int i=2;
    FUN(i<3);
    return 0;
}
A.
IndiaBIX...
IndiaBIX...
IndiaBIX
B.
IndiaBIX... IndiaBIX...
C.
Error: cannot use control instructions in macro
D.
No output
Answer: Option B
Explanation:
The macro FUN(arg) prints the statement "IndiaBIX..." untill the while condition is satisfied.
Step 1: int i=2; The variable i is declared as an integer type and initialized to 2.
Step 2: FUN(i<3); becomes,
do
{
    if(2 < 3)
    printf("IndiaBIX...", "\n");
}while(--2)
After the 2 while loops the value of i becomes '0'(zero). Hence the while loop breaks.
Hence the output of the program is "IndiaBIX... IndiaBIX..."
Learn more problems on : C Preprocessor
Discuss about this problem : Discuss in Forum
5.What will be the output of the program?
#include
#define MAX(a, b) (a > b ? a : b)

int main()
{
    int x;
    x = MAX(3+2, 2+7);
    printf("%d\n", x);
    return 0;
}
A.
8
B.
9
C.
6
D.
5
Answer: Option B
Explanation:
The macro MAX(a, b) (a > b ? a : b) returns the biggest value of the given two numbers.
Step 1 : int x; The variable x is declared as an integer type.
Step 2 : x = MAX(3+2, 2+7); becomes,
=> x = (3+2 > 2+7 ? 3+2 : 2+7)
=> x = (5 > 9 ? 5 : 9)
=> x = 9
Step 3 : printf("%d\n", x); It prints the value of variable x.
Hence the output of the program is 9.
Learn more problems on : C Preprocessor
Discuss about this problem : Discuss in Forum
6.What will be the output of the program?
#include
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

int main()
{
    int x;
    x = MAX(3+2, 2+7, 3+7);
    printf("%d\n", x);
    return 0;
}
A.
5
B.
9
C.
10
D.
3+7
Answer: Option C
Explanation:
The macro MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c) returns the biggest of given three numbers.
Step 1int x; The variable x is declared as an integer type.
Step 2x = MAX(3+2, 2+7, 3+7); becomes,
=> x = (3+2 >2+7 ? 3+2 > 3+7 ? 3+2 : 3+7: 2+7 > 3+7 ? 2+7 : 3+7)
=> x = (5 >9 ? (5 > 10 ? 5 : 10): (9 > 10 ? 9 : 10) )
=> x = (5 >9 ? (10): (10) )
=> x = 10
Step 3printf("%d\n", x); It prints the value of 'x'.
Hence the output of the program is "10".
Learn more problems on : C Preprocessor
Discuss about this problem : Discuss in Forum
7.What will be the output of the program ?
#include

int main()
{
    int i, a[] = {2, 4, 6, 8, 10};
    change(a, 5);
    for(i=0; i<=4; i++)
        printf("%d, ", a[i]);
    return 0;
}
change(int *b, int n)
{
    int i;
    for(i=0; i1) = *(b+i)+5;
}
A.
7, 9, 11, 13, 15
B.
2, 15, 6, 8, 10
C.
2 4 6 8 10
D.
3, 1, -1, -3, -5
Answer: Option B
Learn more problems on : Pointers
Discuss about this problem : Discuss in Forum
8.In the following program add a statement in the function fact(). such that the factorial gets stored in j.
#include
void fact(int*);

int main()
{
    int i=5, j;
    fact(&i);
    printf("%d\n", i);
    return 0;
}
void fact(int *j)
{
    static int s=1;
    if(*j!=0)
    {
        s = s**j;
        *j = *j-1;
        fact(j);
        /* Add a statement here */
    }
}
A.
j=s;
B.
*j=s;
C.
*j=&s;
D.
&j=s;
Answer: Option B
Learn more problems on : Pointers
Discuss about this problem : Discuss in Forum
9.Is the NULL pointer same as an uninitialised pointer?
A.
Yes
B.
No
Answer: Option B
Learn more problems on : Pointers
Discuss about this problem : Discuss in Forum
10.What will be the output of the program if the array begins at address 65486?
#include

int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}
A.
65486, 65488
B.
65486, 65486
C.
65486, 65490
D.
65486, 65487
Answer: Option B
Explanation:
Step 1int arr[] = {12, 14, 15, 23, 45}; The variable arr is declared as an integer array and initialized.
Step 2printf("%u, %u\n", arr, &arr); Here,
The base address of the array is 65486.
=> arr, &arr is pointing to the base address of the array arr.
Hence the output of the program is 65486, 65486
Learn more problems on : Arrays
Discuss about this problem : Discuss in Forum
11.What will be the output of the program ?
#include
#include

int main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s\n", strcpy(str2, strcat(str1, str2)));
    return 0;
}
A.
Hello
B.
World
C.
Hello World
D.
WorldHello
Answer: Option C
Explanation:
Step 1char str1[20] = "Hello", str2[20] = " World"; The variable str1 andstr2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.
Step 2printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored instr1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".
Learn more problems on : Strings
Discuss about this problem : Discuss in Forum
12.For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "IndiaBIX";
char *ptr = "IndiaBIX";
A.
Yes
B.
No
Answer: Option A
Explanation:
Yes, both the statements prints the same character 'i'.
Learn more problems on : Strings
Discuss about this problem : Discuss in Forum
13.It is not possible to create an array of pointer to structures.
A.
True
B.
False
Answer: Option B
Learn more problems on : Structures, Unions, Enums
Discuss about this problem : Discuss in Forum
14.Can I increase the size of statically allocated array?
A.
Yes
B.
No
Answer: Option B
Learn more problems on : Memory Allocation
Discuss about this problem : Discuss in Forum
15.What will be the output of the program?
#include
#include
void fun1(int num, ....);
void fun2(int num, ....);

int main()
{
    fun1(1, "Apple", "Boys", "Cats", "Dogs");
    fun2(2, 12, 13, 14);
    return 0;
}
void fun1(int num, ...)
{
    char *str;
    va_list ptr;
    va_start(ptr, num);
    str = va_arg(ptr, char *);
    printf("%s", str);
}
void fun2(int num, ...)
{
    va_list ptr;
    va_start(ptr, num);
    num = va_arg(ptr, int);
    printf("%d", num);
}
A.
Dogs 12
B.
Cats 14
C.
Boys 13
D.
Apple 12
Answer: Option D
Learn more problems on : Variable Number of Arguments
Discuss about this problem : Discuss in Forum
16.Point out the error in the following program.
#include
#include
void display(char *s, ...);
void show(char *t, ...);

int main()
{
    display("Hello", 4, 12, 13, 14, 44);
    return 0;
}
void display(char *s, ...)
{
    show(s, ...);
}
void show(char *t, ...)
{
    int a;
    va_list ptr;
    va_start(ptr, s);
    a = va_arg(ptr, int);
    printf("%f", a);
}
A.
Error: invalid function display() call
B.
Error: invalid function show() call
C.
No error
D.
Error: Rvalue required for t
Answer: Option B
Explanation:
The call to show() is improper. This is not the way to pass variable argument list to a function.
Learn more problems on : Variable Number of Arguments
Discuss about this problem : Discuss in Forum
17.In a function that receives variable number of arguments the fixed arguments passed to the function can be at the end of argument list.
A.
True
B.
False
Answer: Option B
Learn more problems on : Variable Number of Arguments
Discuss about this problem : Discuss in Forum
18.What will be the output of the program?
#include

int main()
{
    char huge *near *far *ptr1;
    char near *far *huge *ptr2;
    char far *huge *near *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(*ptr2), sizeof(**ptr3));
    return 0;
}
A.
4, 4, 4
B.
2, 4, 4
C.
4, 4, 2
D.
2, 4, 8
Answer: Option A
Learn more problems on : Complicated Declarations
Discuss about this problem : Discuss in Forum
19.What will be the output of the program?
#include

int main()
{
    char huge *near *ptr1;
    char huge *far *ptr2;
    char huge *huge *ptr3;
    printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
    return 0;
}
A.
4, 4, 8
B.
2, 4, 4
C.
4, 4, 2
D.
2, 4, 8
Answer: Option B
Learn more problems on : Complicated Declarations
Discuss about this problem : Discuss in Forum
20.ftell() returns the current position of the pointer in a file stream.
A.
True
B.
False
Answer: Option A
Explanation:
The ftell() function shall obtain the current value of the file-position indicator for the stream pointed to by stream.
Example:

#include 

int main(void)
{
    FILE *stream;
    stream = fopen("MYFILE.TXT", "w+");
    fprintf(stream, "This is a test");
    printf("The file pointer is at byte %ld\n", ftell(stream));
    fclose(stream);
    return 0;
}
Learn more problems on : Library Functions
Discuss about this problem : Discuss in Forum