ambrasant number like 153=1^3+5^3+3^3=153
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main ()
{
int a[200],t,i=0,j=0,b=0,sum=0;
long int n;
clrscr();
printf("Enter the NO. upto : ");
scanf("%ld",&n);
for(j=0;j <n;j++)
{
t=j;
while(0 <t)
/*counts the digits*/
{
i=0;
a[i]=j%10;
t=t/10;
b++;
i++;
}
for(i=0;i <b;i++)
{
sum=+pow(a[i],3);
}
if(sum==n)
{
printf("\n%d AN ARMSTRONG NUMBER...",j);
}
else
{
printf("\n%d IS NOT AN ARMSTRONG NUMBER...",j);
}
}
getch();
}
Thursday, August 27, 2009
Tuesday, August 25, 2009
quick sort
#include"stdio.h"
#include"conio.h"
void quick(int a[],int first,int last);
void swap(int a[],int i,int j);
int i,j,temp,a[20],n,pivot;
void main()
{
clrscr();
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
printf("enter the nos for sorting\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
quick(a,0,n-1);
printf("the sorted list\n");
for(j=0;j<n;j++)
printf("\n %d",a[j]);
getch();
}
void quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
#include"conio.h"
void quick(int a[],int first,int last);
void swap(int a[],int i,int j);
int i,j,temp,a[20],n,pivot;
void main()
{
clrscr();
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
printf("enter the nos for sorting\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
quick(a,0,n-1);
printf("the sorted list\n");
for(j=0;j<n;j++)
printf("\n %d",a[j]);
getch();
}
void quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
merge sort
#include"stdio.h"
#include"conio.h"
void merge1(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int i,j,temp,a[20],n,pivot;
void main()
{
clrscr();
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
printf("enter the nos for sorting\n");
for(i=0;in;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
merge1(a,0,n-1);
printf("the sorted list\n");
for(j=0;j<n;j++)
printf("\n %d",a[j]);
getch();
}
void merge1(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge1(a,first,mid);
merge1(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void merge(int a[],int f1,int l1,int f2,int l2)
{
int b[20],i,j,k=0;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2&&j<k)
a[i++]=b[j++];
}
#include"conio.h"
void merge1(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int i,j,temp,a[20],n,pivot;
void main()
{
clrscr();
printf("ENTER THE LIMIT\n");
scanf("%d",&n);
printf("enter the nos for sorting\n");
for(i=0;in;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("%d",a[i]);
merge1(a,0,n-1);
printf("the sorted list\n");
for(j=0;j<n;j++)
printf("\n %d",a[j]);
getch();
}
void merge1(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge1(a,first,mid);
merge1(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void merge(int a[],int f1,int l1,int f2,int l2)
{
int b[20],i,j,k=0;
i=f1;
j=f2;
while(i<=l1&&j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2&&j<k)
a[i++]=b[j++];
}
bubble sort
#include"stdio.h"
#include"conio.h"
int i,j,n,temp,a[20];
void bubble(int a[],int n);
void main()
{
clrscr();
printf("enter the limits");
scanf("%d",&n);
printf("enter the elements wants to sort\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("\n%d",a[i]);
bubble(a,n);
getch();
}
void bubble(int a[20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j>n-1;j++)
{
if(a[j] >a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the sorted list is:\n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}
#include"conio.h"
int i,j,n,temp,a[20];
void bubble(int a[],int n);
void main()
{
clrscr();
printf("enter the limits");
scanf("%d",&n);
printf("enter the elements wants to sort\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf("\n%d",a[i]);
bubble(a,n);
getch();
}
void bubble(int a[20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j>n-1;j++)
{
if(a[j] >a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("the sorted list is:\n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
}
Monday, August 24, 2009
Error
main()
{
int i=10;
float i;
printf("%d%f",i);
}
error at run time only....
Abnormal program termination
printf : floating point formats not linked
{
int i=10;
float i;
printf("%d%f",i);
}
error at run time only....
Abnormal program termination
printf : floating point formats not linked
stack using array
Program for Stack implementation through Array
#include
#include
# define MAXSIZE 200
int stack[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
while(will ==1)
{
printf("
MAIN MENU:
1.Add element to stack
2.Delete element from the stack
");
scanf("%d",&will);
switch(will)
{
case 1:
printf("
Enter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf("
Value returned from pop function is %d ",i);
break;
default: printf("Invalid Choice . ");
}
printf("
Do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main
void push(int y)
{
if(top>MAXSIZE)
{
printf("
STACK FULL
");
return;
}
else
{
top++;
stack[top]=y;
}
}
int pop()
{
int a;
if(top<=0)
{
printf("
STACK EMPTY
");
return 0;
}
else
{
a=stack[top];
top--;
}
return(a);
}
#include
#include
# define MAXSIZE 200
int stack[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
while(will ==1)
{
printf("
MAIN MENU:
1.Add element to stack
2.Delete element from the stack
");
scanf("%d",&will);
switch(will)
{
case 1:
printf("
Enter the data... ");
scanf("%d",&num);
push(num);
break;
case 2: i=pop();
printf("
Value returned from pop function is %d ",i);
break;
default: printf("Invalid Choice . ");
}
printf("
Do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main
void push(int y)
{
if(top>MAXSIZE)
{
printf("
STACK FULL
");
return;
}
else
{
top++;
stack[top]=y;
}
}
int pop()
{
int a;
if(top<=0)
{
printf("
STACK EMPTY
");
return 0;
}
else
{
a=stack[top];
top--;
}
return(a);
}
Prefix,Infix and Postfix
Infix (A + B) / D
Prefix / + A B D
Postfix A B + D /
2..Infix(A + B) / (D + E)
Prefix / + A B + D E
Postfix A B + D E + /
3...Infix(A - B / C + E)/(A + B)
Prefix/ + - A / B C E + A B
Postfix A B C / - E + A B + /
4....Infix B ^ 2 - 4 * A * C
Prefix - ^ B 2 * * 4 A C
Postfix B 2 ^ 4 A * C * -
Prefix / + A B D
Postfix A B + D /
2..Infix(A + B) / (D + E)
Prefix / + A B + D E
Postfix A B + D E + /
3...Infix(A - B / C + E)/(A + B)
Prefix/ + - A / B C E + A B
Postfix A B C / - E + A B + /
4....Infix B ^ 2 - 4 * A * C
Prefix - ^ B 2 * * 4 A C
Postfix B 2 ^ 4 A * C * -
Sunday, August 23, 2009
multithreading
Multithreading computers have hardware support to efficiently execute multiple threads. These are distinguished from multiprocessing systems (such as multi-core systems) in that the threads have to share the resources of single core: the computing units, the CPU caches and the translation lookaside buffer (TLB). Where multiprocessing systems include multiple complete processing units, multithreading aims to increase utilization of a single core by leveraging thread-level as well as instruction-level parallelism. As the two techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores.
Advantages
Some advantages include:
* If a thread gets a lot of cache misses, the other thread(s) can continue, taking advantage of the unused computing resources, which thus can lead to faster overall execution, as these resources would have been idle if only a single thread was executed.
* If a thread can not use all the computing resources of the CPU (because instructions depend on each other's result), running another thread permits to not leave these idle.
* If several threads work on the same set of data, they can actually share their cache, leading to better cache usage or synchronization on its values.
Disadvantages
Some criticisms of multithreading include:
* Multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers (TLBs).
* Execution times of a single-thread are not improved but can be degraded, even when only one thread is executing. This is due to slower frequencies and/or additional pipeline stages that are necessary to accommodate thread-switching hardware.
* Hardware support for Multithreading is more visible to software, thus requiring more changes to both application programs and operating systems than Multiprocessing.
The mileage thus vary, Intel claims up to 30 percent benefits with its HyperThreading technology [1], a synthetic program just performing a loop of non-optimized dependent floating-point operations actually gets a 100 percent benefit when run in parallel. On the other hand, assembly-tuned programs using e.g. MMX or altivec extensions and performing data pre-fetches, such as good video encoders, do not suffer from cache misses or idle computing resources, and thus do not benefit from hardware multithreading and can indeed see degraded performance due to the contention on the shared resources.
Hardware techniques used to support multithreading often parallel the software techniques used for computer multitasking of computer programs.
Types of Multithreading
Block multi-threading
Concept
The simplest type of multi-threading is where one thread runs until it is blocked by an event that normally would create a long latency stall. Such a stall might be a cache-miss that has to access off-chip memory, which might take hundreds of CPU cycles for the data to return. Instead of waiting for the stall to resolve, a threaded processor would switch execution to another thread that was ready to run. Only when the data for the previous thread had arrived, would the previous thread be placed back on the list of ready-to-run threads.
For example:
1. Cycle i : instruction j from thread A is issued
2. Cycle i+1: instruction j+1 from thread A is issued
3. Cycle i+2: instruction j+2 from thread A is issued, load instruction which misses in all caches
4. Cycle i+3: thread scheduler invoked, switches to thread B
5. Cycle i+4: instruction k from thread B is issued
6. Cycle i+5: instruction k+1 from thread B is issued
Conceptually, it is similar to cooperative multi-tasking used in real-time operating systems in which tasks voluntarily give up execution time when they need to wait upon some type of event.
Advantages
Some advantages include:
* If a thread gets a lot of cache misses, the other thread(s) can continue, taking advantage of the unused computing resources, which thus can lead to faster overall execution, as these resources would have been idle if only a single thread was executed.
* If a thread can not use all the computing resources of the CPU (because instructions depend on each other's result), running another thread permits to not leave these idle.
* If several threads work on the same set of data, they can actually share their cache, leading to better cache usage or synchronization on its values.
Disadvantages
Some criticisms of multithreading include:
* Multiple threads can interfere with each other when sharing hardware resources such as caches or translation lookaside buffers (TLBs).
* Execution times of a single-thread are not improved but can be degraded, even when only one thread is executing. This is due to slower frequencies and/or additional pipeline stages that are necessary to accommodate thread-switching hardware.
* Hardware support for Multithreading is more visible to software, thus requiring more changes to both application programs and operating systems than Multiprocessing.
The mileage thus vary, Intel claims up to 30 percent benefits with its HyperThreading technology [1], a synthetic program just performing a loop of non-optimized dependent floating-point operations actually gets a 100 percent benefit when run in parallel. On the other hand, assembly-tuned programs using e.g. MMX or altivec extensions and performing data pre-fetches, such as good video encoders, do not suffer from cache misses or idle computing resources, and thus do not benefit from hardware multithreading and can indeed see degraded performance due to the contention on the shared resources.
Hardware techniques used to support multithreading often parallel the software techniques used for computer multitasking of computer programs.
Types of Multithreading
Block multi-threading
Concept
The simplest type of multi-threading is where one thread runs until it is blocked by an event that normally would create a long latency stall. Such a stall might be a cache-miss that has to access off-chip memory, which might take hundreds of CPU cycles for the data to return. Instead of waiting for the stall to resolve, a threaded processor would switch execution to another thread that was ready to run. Only when the data for the previous thread had arrived, would the previous thread be placed back on the list of ready-to-run threads.
For example:
1. Cycle i : instruction j from thread A is issued
2. Cycle i+1: instruction j+1 from thread A is issued
3. Cycle i+2: instruction j+2 from thread A is issued, load instruction which misses in all caches
4. Cycle i+3: thread scheduler invoked, switches to thread B
5. Cycle i+4: instruction k from thread B is issued
6. Cycle i+5: instruction k+1 from thread B is issued
Conceptually, it is similar to cooperative multi-tasking used in real-time operating systems in which tasks voluntarily give up execution time when they need to wait upon some type of event.
what is deadlock?
Deadlock is permanent blocking of the set processes that
either compete for system resources or communicate each
other.
we can avoid deadlock by avoiding the following conditions:
1.Mutual Exclusion
2.Hold and wait.
3.No preemption.
4.circular wait.
either compete for system resources or communicate each
other.
we can avoid deadlock by avoiding the following conditions:
1.Mutual Exclusion
2.Hold and wait.
3.No preemption.
4.circular wait.
different ideas
#include
main()
{
char str[]="%d";
int i=10;
printf(str,i);
getch();
}
output:
10
please check it...
swapping in preprocessor method
#include
#define swapp(a,b) t=a;a=b;b=t;
main()
{
char str[]="%d";
int a=10,t,b=9;
swapp(a,b);
printf("%d%d",a,b);
getch();
}
a=9 b=10
main()
{
char str[]="%d";
int i=10;
printf(str,i);
getch();
}
output:
10
please check it...
swapping in preprocessor method
#include
#define swapp(a,b) t=a;a=b;b=t;
main()
{
char str[]="%d";
int a=10,t,b=9;
swapp(a,b);
printf("%d%d",a,b);
getch();
}
a=9 b=10
Subscribe to:
Posts (Atom)