c program for quick sorting

#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
int main()
{
 int arr[30];
 int i,size;
 printf(“Enter total no. of the elements : “);
 scanf(“%d”,&size);
 printf(“Enter total %d elements : \n”,size);
 for(i=0; i<size; i++)
    scanf(“%d”,&arr[i]);
 qsort(arr,0,size-1);
 printf(“Quick sorted elements are as  : \n”);
 for(i=0; i<size; i++)
    printf(“%d\t”,arr[i]);
getch();
 return 0;
}
void qsort(int arr[20], int fst, int last)
{
 int i,j,pivot,tmp;
 if(fst<last)
 {
   pivot=fst;
   i=fst;
   j=last;
   while(i<j)
   {
     while(arr[i]<=arr[pivot] && i<last)
        i++;
     while(arr[j]>arr[pivot])
        j–;
     if(i<j)
     {
        tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
     }
   }
   tmp=arr[pivot];
   arr[pivot]=arr[j];
   arr[j]=tmp;
   qsort(arr,fst,j-1);
   qsort(arr,j+1,last);
 }
}

a program for stack sort (push and pop)

#include <stdio.h>
#define MAX 5
int top, status;
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{
status = 0;
}
else
{
status = 1;
ret = stack [top];
–top;
}
return ret;
}
void display (int stack[])
{ int i;
printf (“\nThe Stack is: \n”);
if (top == -1)
printf (“empty”);
else
{
for (i=top; i>=0; –i)
printf (” \n%d “,stack[i]);
}
printf (“\n”);
}
int main()
{
int stack [MAX], item;
int ch;
top = -1;
do
{ do
{
printf (“\nNMAIN MENU:\n”);
printf (“\n 1.PUSH :”);
printf (“\n 2.POP :”);
printf (“\n 3.Exit :”);
printf (“\nEnter Your Choice:\n “);
scanf (“%d”, &ch);
if (ch<1 || ch>3)
printf (“\nInvalid Choice, Please try again:\n”);
} while (ch<1 || ch>3);
switch (ch)
{
case 1:
printf (“\nEnter the Element to be pushed : \n”);
scanf (“%d”, &item);
push (stack, item);
if (status)
{ printf (“\nAfter Pushing :\n”);
display (stack);
if (top == (MAX-1))
printf (“\nThe Stack is Full:\n”);
}
else
printf (“\nStack overflow on Push:\n”);
break;
case 2:
item = pop (stack);
if (status)
{ printf (“\nThe Popped item is %d \n After Popping: “,item);
display (stack);
}
else
printf (“\nStack underflow on Pop:\n”);
break;

}
}while (ch != 3);
}

What is difference between gets() and scanf() function?

Answer : scanf() uses printf()’s format strings. Thus you have to tell it for example that you want a string, a float, an integer character of some kind or a char. In fact, the first difference is you can enter any of the basic types that way and the second difference is that you use the format string to tell it what you enter. Gets() enters strings period.

Scanf() does format your input. If you enter a whitespace character like a space or — anything else — your string ends whether you want it to or not. Gets() does not. You type until you reach a newline character (hit enter). When it encounters that, that is your string.

One similarity is kinda dangerous, and leads to the next difference I can think of. Neither checks, when using strings, that the strings are valid pointers to arrays of sufficient length to hold the characters you are sending to them. C tends to treat arrays and pointers the same way, so this behavior can cause crashes or worse if you send your data to or try to read it from uninitialized pointers.

Gcc likes scanf(). If you compile a program with gets you get the following in GCC:
/tmp/ccmTbwvW.o: In function `main’:
get.c:(.text+0x24): warning: the `gets’ function is dangerous and should not be used.
You don’t get that “problem” with scanf.

Finally you can do a:
HelloString=gets(Hellostring);

Gets() returns another pointer to your array of char which you can throw away if you like. Scanf() also has a return. It returns the number of items successfully read or EOF.

How can you read a character from keyboard and writing a character to the screen?

we can use getchar function to read a single character from the keyboard. We will try to take one sample character and display by using printf function. Once the program encounters the getchar() function it will wait for the user to enter data by keyboard. Once it is entered it will be stored in a variable and we can display by using printf function.

#include <stdio.h>
int main(void){
char var;
var = getchar();

printf(” Welcome to %c”,var);
return 0;
}

Using getchar function we can interact with user and execute some conditional statements. WE will ask user if C programming is easy or difficult. Based on the input data we will display a message using one if condition checking.

#include <stdio.h>
int main(void){
char var;
printf( “Do you like C programming ? Press Y or N “);
var = getchar();
if(var == ‘Y’)
{
printf(” Yes C is very interesting language”);
}
else
{
printf (” Why ? It is easy to learn here, need practice only”);
}

return 0;
}

Describe about conditional operators.

The conditional operator in C is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.

Syntax for conditional operator in C:

1 condition ? result1 : result2;

If the condition is true, result1 is returned else result2 is returned.

C Conditional Operator Examples:

1

2

3

10==5 ? 11: 12; // returns 12, since 10 not equal to 5.

10!=5 ? 4 : 3; // returns 4, since 10 not equal to 5.

12>8 ? a : b; // returns the value of a, since 12 is greater than 8.

Program in C using Conditional Operator

/*Conditional operator*/

#include

#include

void main()

{

int a = 10, b = 11;

int c;

c = (a < b)? a : b;

printf(“%d”, c);

}

Output:
10

In the above program, if the value of a is less than b then the value of a would be assigned to the variable b. Else, the value of b would be assigned to variable c. In this case, the value of a is 10 and value of b is 11 i.e., a < b is true. Hence, the value of a i.e., 10 will be assigned to variable c.

Write about ++m and m ++ operators.

The  operator ++ adds 1 to the operand.

++m, or m++,

++ m; is equivalent to m=m+1;(m+=1)

We use the increment statements in for and while loops extensively. While ++m and m++ mean the same thing when they form statements independently, they behave differently when they are used in expressions on the right hand side of an assignment statement. Consider the following :

m=5;

y=++m;

in this case the value of y and m would be 6; suppose if we rewrite the above statements as

m=5;

y=m++;

then the value of y would be 5 and m would be 6. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left. On the other hand, a postfix operator first assigned the value to the variable on left and then increments the operaned.

Describe about increment (++) and decreament (–) operator.

C offers two unusual and unique operators ++  and — called  increment and decrement operator respectively. The use of these two operators will results in the value of the variable being incremented or decremented by unity. i=i+1 can be written as i++ and j=j-1 can be written as j—.

             The operators can be place either before or after the operand. If the operator is placed before the variable (++I or –I) it is known as pre incrementing or pre decrementing.

             If the operator is placed after the variable (I++ or I–) it is known as post incrementing or post decrementing.

             Pre incrementing or pre decrementing or post incrementing or post decrementing have different effect when used in expression. In the pre increment or pre decrement first the value of operand is incremented or decremented then it is assigned.

Example:
x=100;
y=++x;
After the execution the value of y will be 101 and the value of x will be 100.

            In the post increment or post decrement first the value of operand is assigned to the concern variable and then the increment or decrement perform.

Example:
x=100;
Y=x++;