clanguage07@gmail.com

clanguage07@gmail.com

Sunday, September 8, 2013

BUBBLE SORT USING C PROGRAM




Source code of simple bubble sort implementation using array ascending order in c programming language
#include<stdio.h>
int main(){
  int s,temp,i,j,a[20];
  printf("Enter total numbers of elements: ");
  scanf("%d",&s);
  printf("Enter %d elements: ",s);
  for(i=0;i<s;i++)
      scanf("%d",&a[i]);
  //Bubble sorting algorithm
  for(i=s-2;i>=0;i--){
      for(j=0;j<=i;j++){
           if(a[j]>a[j+1]){
               temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
           }
      }
  }
  printf("After sorting: ");
  for(i=0;i<s;i++)
      printf(" %d",a[i]);
  return 0;
}

Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting:  0 2 6 9 11




5. Write a c program for heap sort.
7. Write a c program for shell sort.

Memory mapping in c

Memory model in c programming



Memory model:

In c there are six type of memory model.
If you want to see all memory model in Turbo C++ IDE then open Turbo C++ IDE and the go:
Options menu -> Compiler -> Code generation

These memory models are:

(a) TINY
(b) SMALL
(c) MEDIUM
(d) COMPACT
(e) LARGE
(f) HUGE

If you want to change the memory model then go to:

Options menu -> Compiler -> Code generation

And select any memory model and click OK button.




Properties of memory mode in C:

(1) Memory model decides the default type of pointer in C.



Note:

Code: A pointer to function is called code.
Data: A pointer to variable is called data.

Examples:

(1)What will be output of following c program?


#include<stdio.h>
int main(){

int *ptr;
printf("%d",sizeof ptr);


return 0;
}

Output: Depends upon memory model.
Explanation: If memory model is TINY, SMALL or MEDIUM then default pointer will near and output will be 2 other wise output will be 4.

(2)What will be output of following c program?


#include<stdio.h>
int main(){

char (*fun)();
printf("%d",sizeof fun);


return 0;
}

Output: Depends upon memory model.
Explanation: fun is pointer to function. If memory model is TINY, SMALL or COMPACT then default pointer will near and output will be 2 other wise output will be 4.

(3)What will be output of following c program?


#include<stdio.h>
int main(){
int near *p,*q;
printf("%d , %d",sizeof(p),sizeof(q));


return 0;
}

Output: 2, Depend upon memory model.
Explanation: p is near pointer while type of pointer q will depend what is default type of pointer.

(4)What will be output of following c program?


#include<stdio.h>
int main(){
char huge **p;
printf("%d , %d",sizeof(p),sizeof(*p));


return 0;
}

Output: 4, Depend upon memory model.
Explanation: p is huge pointer while type of pointer *p will depend what is default type of pointer.

(5)Write a c program to find the memory model of you computer?


#include<stdio.h>
int main(){
   #if defined __TINY__
   printf("Memory model is: TINY");
   #elif defined __SMALL__
   printf("Memory model is:SMALL ");
   #elif defined __MEDIUM__
   printf("Memory model is:MEDIUM ");
   #elif defined __COMPACT__
   printf("Memory model is:COMPACT ");
   #elif defined __LARGE__
   printf("Memory model is:LARGE ");
   #elif defined __HUGE__
   printf("Memory model is:HUGE ");
   #endif


   return 0;

}

(2) Memory models decide the default size of segment.

Huge pointer in c programming


Huge pointer:

The pointer which can point or access whole the residence memory of RAM i.e. which can access all the 16 segments is known as huge pointer.





Size of huge pointer is 4 byte or 32 bit.

(1)What will be output of following c program?


#include<stdio.h>
int main(){
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));


return 0;
}

Output: 4 4 1
Explanation: p is huge pointer, *p is far pointer and **p is char type data variable.

Normalization of huge pointer:

Turbo C compiler is based on 8085 microprocessor in which physical address of memory is represented in 20 bit. Conversion of 4 byte or 32 bit huge address into 20 bit actual physical address is known as normalization.

Formula to calculate physical address:


Example:

(q) What will be physical address of huge address 0X59994444?

Answer:

Huge address: 0X59994444
Offset address: 0x4444
Segment address: 0x5999
Physical address= Segment address * 0X10 + Offset address
=0X5999 * 0X10 +0X4444
=0X59990 + 0X4444
=0X5DDD4
In binary: 0101 1101 1101 1101 0100

Note: Each hexadecimal digit is represented in 4 bit binary number.

When any relation operation is performed between two huge pointers first it normalizes in actual physical address.

Example:

(q)What will be output of following c program?


#include<stdio.h>
int main(){

   int huge*p=(int huge*)0XC0563331;
   int huge*q=(int huge*)0xC2551341;

   if(p==q)
     printf("Equql");
   else
     printf("Not equal");

   return 0;
}

Output: Equal
Explanation:
As we know huge pointers compare its physical address.

Physical address of huge pointer p

Huge address: 0XC0563331
Offset address: 0x3331
Segment address: 0XC056
Physical address= Segment address * 0X10 + Offset address
=0XC056 * 0X10 +0X3331
=0XC0560 + 0X3331
=0XC3891

Physical address of huge pointer q

Huge address: 0XC2551341
Offset address: 0x1341
Segment address: 0XC255
Physical address= Segment address * 0X10 + Offset address
=0XC255 * 0X10 +0X1341
=0XC2550 + 0X1341
=0XC3891
Since both huge pointers p and q are pointing same physical address so if condition will true.

(q)What will be output of following c program?


#include<stdio.h>
int main(){

double near *p,far *q;
printf("%d %d %d",sizeof(q),sizeof(p),sizeof(*p));


return 0;
}

Output: 4 2 8
Explanation: q is far pointer, p is near pointer, *p is double data type constant.

Don’t use huge pointer:

If you will increment huge pointer it will increment both offset and segment address unlike to far pointer which only increments offset address. So if you have little knowledge about huge pointer and you are using huge pointer then you can easily access and modify the IVT, device driver memory, video memory etc. This might be dangerous for your computer.

(q)Why there are three types of pointer in Turbo c compiler?

Answer:

Turbo c compiler is based on Dos operating system which is based on 8085 microprocessors. In 8085 microprocessor actual physical address is represented in 20 bit. But there are not any pointers which can point 20 bit address. Considering simplicity of calculations, access to actual physical address, security etc. c has introduced three type of pointer i.e. near, far and huge pointer.

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true