Thursday, August 27, 2009

ambrasant number

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();
}

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&&ii++;

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++];
}

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]);
}

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

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);

}

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 * -

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.

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.

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

sorting type

1.quick ,

2.heap,

3.shell,

4.merge,

5.bubble

6.insertion

Fibonacci series

This program prints the Fibonacci series

#include
#include
void main(void)
{
int i,j,k,n;
clrscr();
i=0;
j=1;
printf("%d %d ",i,j);
for(n=0;n<=5;n++)
{
k=i+j;
i=j;
j=k;
printf("%d ",k);
}
getch();
}

Thursday, August 13, 2009

display reverse content and save it in another file

/*reverse of file content display and stored in another file */
#include
main()
{
FILE *fp,*ft;
char c;
long int pos;
/*assume that a file ,conten.c and rev.c exit already*/
fp=fopen("conten.c","r");
ft=fopen("rev.c","w");
pos=ftell(fp);
/*printf("%ld",pos);*/
fseek(fp,-1,2);
while(ftell(fp)>pos)
{

c=fgetc(fp);
fseek(fp,-2,1);
fputc(c,ft);
printf("%c",c);
}
c=fgetc(fp);
printf("%c",c);
fputc(c,ft);
fclose(fp);
fclose(ft);

}

Appends one file to another

/*appends one file to another*/
#include
main()
{
char c;
FILE *fp1,*fp2;
/*assume that files,source1.c and source2.c exit already*/
fp1=fopen("source1.c","a");
fp2=fopen("source2.c","r");
c=fgetc(fp2);
while(c!=EOF)
{
fputc(c,fp1);
c=fgetc(fp2);
}
fclose(fp1);

fclose(fp2);
}

Wednesday, August 12, 2009

C++

CORRECT ANSWER
1. When dynamically allocated memory is lost to the C++ program then
Answer: memory leak occurs
2. The private member in derived class
Answer: Can be inherited only if the derived class is inheriting from base class with private access level
3. The operator that denotes address of a variable in C++ program is
Answer: &
4. Which of the following denote stream classes in C++?
Answer: All the Above
5. If an array in C++ is declared as exforsys[10] then exforsys is equal to
Answer: &exforsys[0]
6. Using pointers to call a function is called as
Answer: call by reference
7. The notation of logical NOT operator in a C++ program is
Answer: !
8. What is the notation used to place block of statements in a looping structure in C++?
Answer: { }
9. Which of the following denote types of polymorphism in C++?
Answer: All the Above
10. Which of the looping structure in C++ check condition at the beginning of loop?
Answer: while
11. Which of the following is mandatory for all C++ program?
Answer: main()
12. The notation of member access operator in structures is
Answer: .
13. If a function in C++ does not return a value then its return type is denoted as
Answer: void
14. The inserting of the code of a called function at the point where the function gets called is achieved by using
Answer: Inline functions

Incorrect Questions (16 / 30)
1. The method that is used for writing characters in C++ program is
Answer: put
2. Which of the following allocates memory but does not initialize it?
Answer: operator new
3. Index of an array starts from
Answer: Zero
4. A function named as exforsys has three implementations associated with it. This means the function exforsys is
Answer: overloaded
5. Strict parameter type checking is followed with which of the following?
Answer: Inline
6. The block of memory allocated by the new is released by using
Answer: delete
7. Class that reads and writes to an array in memory is
Answer: strstream
8. The function named as exforsys declared as int exforsys(float x, double y) has return type as
Answer: int
9. Which of the following is used to group a set of global classes, objects and functions under a name?
Answer: Namespaces
10. The vtable entry for a pure virtual function in C++ is
Answer: NULL
11. The overriding method must have the which of the following same as that of the method in the super class definition
Answer: Both A and B
12. A variable modified by multiple threads should be declared as
Answer: volatile
13. Which of the following denote bitwise operators of C++?
Answer: All the Above
14. The value of EOF is
Answer: -1
15. Which of the following is used in C++ to create a copy of an object?
Answer: Copy constructor

BASIC C

Correct Questions (24 / 30)
1. What does the following C program results?
main()
{
int exforsys=100;
int test[exforsys];
for(j=1;j<=exfosys;j++)
{
scanf("%d",&exforsys[j]);
}
}
Answer: Gives Error
2. Which of the following can be used to append one string at the end of another?
Answer: strcat
3. Which of the following is not a BITWISE operator in C program?
Answer: &&
4. malloc is declared in
Answer: stdlib.h
5. The notation for preprocessor directive is
Answer: #
6. Which of the following denote exforsys pointing to a character array?
Answer: char *exforsys;
7. Which takes the first precedence in evaluation among the following operators?
Answer: !
8. How is a structure variable named as test declared for a structure named as typedef struct exforsys?
Answer: struct exforsys test;
9. The address of variable ex is got by using
Answer: &ex;
10. Array of structure is
Answer: An array in which each element is a structure
11. The exit() function is present in the header file
Answer: stdlib.h
12. Which of the following is used to call a function named as exforsys?
Answer: exforsys()
13. What is the Boolean value of a false statement resulting in a C program?
Answer: 0
14. Which of the following denote special operators in C?
Answer: ,
15. The integer array exforsys declared as
int exforsys[30];
In the above array the thirtieth element is present in
Answer: exforsys[29]
16. What is the notation that appears after the case statement?
Answer: :
17. Which of the following function is present in the header file
Answer: isalpha
18. Which of the following data type occupied 1 byte?
Answer: char
19. A function calling itself repeatedly until a specific condition is satisfied is called as
Answer: recursion
20. Which of the following are used with switch statement?
Answer: All the Above
21. Which of the following deals with single character?
Answer: Both A and B
22. Which of the following denotes the two-dimensional array named as exforsys with integer elements?
Answer: int exforsys[10][10];
23. Which of the following are used in dynamic memory allocation?
Answer: All the Above
24. What is the output of the following program?
main()
{
printf("%d",-5/-4);
}
Answer: 1

Incorrect Questions (6 / 30)
1. The modifier used for carriage return is
Answer: r
2. Which of the following can return a value?
Answer: function
3. Which of the following reads a single character but does not display the character on screen?
Answer: getch
4. Which of the following denote array of character strings?
Answer: argv
5. Which of the following denote invalid character constants in C?
Answer: 'wer'



CORRECT ANSWER



1. The left shift operator in C program is denoted by
Answer: <<
2. Which of the following statement is FALSE?
Answer: There is semicolon at the end of a #define
3. Which of the following is not a BITWISE operator in C program?
Answer: &&
4. Which of the following is used to receive a single string?
Answer: gets
5. Which of the following is present in the function declaration?
Answer: Both A and B
6. How is the integer pointer ex declared?
Answer: int *x;
7. The function in a C program can be placed
Answer: Both A and B
8. Which of the following is used for defining a macro?
Answer: #define
9. What is the notation that appears after the case statement?
Answer: :
10. Strings in C are
Answer: arrays of characters
11. The modifier used for carriage return is
Answer: r
12. The integer array exforsys declared as
int exforsys[30];
In the above array the thirtieth element is present in
Answer: exforsys[29]
13. What is the output of the following program?
main()
{
int exf=25;
int test;
test=++exf++;
printf("exf=%d test=%d",exf,test);
}
Answer: Gives Error
14. What is the output of the statement?
printf("Exforsys Training nis n Very Good");
Answer: Exforsys Training
is
Very Good
15. The goto statement mentioned as
goto exforsys;
would have the corresponding notation of label as
Answer: exforsys:
16. An array declared as
int exforsys[100];
The first element of the array is in
Answer: exforsys[0]
17. Which of the following denote special operators in C?
Answer: ,
18. What does the following C program results?
main()
{
int exforsys=100;
int test[exforsys];
for(j=1;j<=exfosys;j++)
{
scanf("%d",&exforsys[j]);
}
}
Answer: Gives Error
19. Which of the following is used for comparing two strings?
Answer: strcmp();
20. Which of the following areas is C language used?
Answer: All the Above
21. How is a structure variable named as test declared for a structure named as typedef struct exforsys?
Answer: struct exforsys test;
22. The default return value if omitted in a C function is
Answer: int
23. What value is returned by following C statement?
strcmp("EXFORSYS", "EXFORSYS");
Answer: Zero
24. Which of the following deals with single character?
Answer: Both A and B
25. Which of the following denote primary data types in C program?
Answer: All the Above
26. Which of the following are components of command line arguments?
Answer: Both A and B

Incorrect Questions (4 / 30)
1. Which of the following operating system was C originally developed?
Answer: UNIX
2. Which of the following denotes a new line character in C program?
Answer: n
3. The NULL in C is equal to
Answer: None of the Above
4. If a string literal in an initializer contains less character than the array has elements, the remaining elements are set to
Answer: 0

Sunday, August 9, 2009

MySQL basic database commands for student table

DDL:
CREATE DATABASE <Datbase_name>;

use <database_name<;

CREATE TABLE Student(rno int, name varchar(40), mark1 integer, mark2 integer, PRIMARY KEY (rno));

ALTER TABLE student modify rno INT NOT NULL AUTO_INCREMENT;

ALTER TABLE student add course varchar(40);


DROP TABLE student;



Drops the table and recreates the structure:
Truncate student;





To know table metadata:

desc student;




DML:

Insert

select

Delete

update





insert into student(rno,name,mark1,mark2) values(1, 'a',34,45);


insert into student(name,mark1,mark2) values('b',34,45);

insert into student values(null, 'b',34,45);


insert into student values(null, 'aa',37,46);




Select * from student;

Select name,mark1 from student where rno=2;


Select name,mark1 from student where name like 'a%';



update student set mark2 = 70 where rno=2;

Select name,mark2 from student;



delete from student where rno=3;





Select avg(mark1) from student;

Select course, avg(mark1) from student group by course;

Saturday, August 8, 2009

Find and Replace Any String Outside of The File


#include"stdio.h"
#include "string.h"

void updateblank(FILE *, int);

void main(void)
{
FILE *ft;
static char k,ch,replace[30],find[30];
int l1,l2,j,i,noc=0,nf=0;
printf("enter the string want to be find\n");
gets(find);
printf("enter the string WITH SAME CHAR LENGTH want to be replace\n ");
gets(replace);
l1=strlen(find);
l2=strlen(replace);
printf("%d%d\n",l1,l2);

k=' ';

ft=fopen("C:\\KARTHI\\tc\\tmp\\record.txt","r+");
if(ft==NULL)
{
puts("cannot open file\n");
exit(-1);
}
puts("file is available\n");

while(1)
{

ch=getc(ft);
if(ch==EOF)
break;
if(ch==find[0]){
int isSuccess=1; //true
for(i=1;i<l1;i++)
{
if(find[i]!=getc(ft))
{
fseek(ft,-i,SEEK_CUR);
isSuccess=0;
break;
}
}

if(isSuccess)
{
nf++;
printf("\nfound\n") ;
fseek(ft,-(i),SEEK_CUR);
if(l1>l2)
{
for(i=0;i<l2;i++)
fputc(replace[i],ft);
for(i=l2;i<l1;i++)
fputc(k,ft);
}
else{
for(i=0;i<l1;i++)
fputc(replace[i],ft);

updateblank(ft,l2-l1);

for(i=l1;i<l2;i++)
fputc(replace[i],ft);
}
}

}
noc++;
}
printf("%s to %s\n",find,replace);

fclose(ft);
if(nf==0)
printf("\nno match found\n");
else printf("\nno. of match found=%d",nf);
printf("\ntotal number of character%d\n",noc);



}



void updateblank(FILE *ft, int len)
{
char ch;
int noc=0;
while(1)
{

ch=getc(ft);
if(ch==EOF)
break;
noc++;
}

char rch[noc];

fseek(ft,-(noc),SEEK_CUR);

for(int i=0;i<noc;i++)
{
rch[i]= getc(ft);
}

fseek(ft,-(noc),SEEK_CUR);

for(int i=0;i<len;i++)
{
fputc('\0',ft);
}

for(int i=0;i<noc;i++)
{
fputc(rch[i],ft);
}

fseek(ft,-(noc+len),SEEK_CUR);


}

Thursday, August 6, 2009

Output for Find and Replace String from Outside of the File.

find and replace particular string from outside of the file

input:
1.open a .txt file .
2. type something with "hello" word.
3.close the file.
4.copy their file path.
5.paste it in fopen line.
6.replaceable word is "hi"..note: if you change this word .you should alter the for loop condition.
7.run this program with conio.h file function like clrscr();getch(); to the appropriate place.

//find and replace string from outside of the file.
#include"stdio.h"
void main(void)
{
FILE *ft;
char k,ch,replace[3],find[6];
find="hello";
replace="hi";
k=' ';
int i=1,noc=0;
ft=fopen("C:\\KARTHI\\tc\\tmp\\record.txt","r+");
if(ft==NULL)
{
puts("cannot open file\n");
exit(-1);
}else
puts("file is available\n");
while(1)
{
ch=getc(ft);
if(ch==EOF)
break;
else if(ch==find[0])
{
for(i=1;i<=4;i++)
{
if(find[i]!=getc(ft))
{
fseek(ft,-i,SEEK_CUR); goto flag;
}
}
fseek(ft,-(i),SEEK_CUR);
for(i=0;i<=1;i++)
fputc(replace[i],ft);
for(i=2;i<=4;i++) fputc(k,ft);
}
flag:noc++;
}
printf("%s",find);
fclose(ft);
printf("\ntotal number of character%d\n",noc);
}

output: 1.after run this program;
2.open that .txt file. if file exit any "hello" string, then that will replaced as "hi ".
3.Otherwise no change in the contents.

Tuesday, August 4, 2009

swapping without third variable and single line

main()
{
int a=15,b=5;
clrscr();
a=(a+b)-(b=(a+b)-b);
printf("a=%d b= %d",a,b);
getch();
}

output: a=5 b=10

Monday, August 3, 2009

pointers variable types

#include
main()
{
int j=10,*ptr1;
void *ptr2;
ptr1=&j;ptr2=&j;
printf("%d %d ",*ptr1,ptr2);
getch();
}
output:
10 and some memory address..
note:we can not print the value of "ptr2"....it is not a allowed function type.

Find and replace program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char nam[50][50],sea[50];
int i,k=0,j,n;
int f=1;
char nam1[50];
clrscr();
printf("ENTER THE NO. OF NAMES\n");
scanf("%d",&n);
printf("ENTER THE NAME\n");
for(i=1;i<=n;i++)
{
scanf("%s",nam[i]);
}
/*internal*/
printf("memory\n");
for(i=1;i<=n;i++)
puts(nam[i]);
printf("\nENTER NAME TO BE replace\n\t\t");
scanf("%s",sea);

printf("........searching .....\n\t");
j=1;
while(j<=n)
{
if(strcmp(nam[j],sea)==0)
{
printf("\n\n\n===========found==============\n\n\n\n\n");
/*replacing */
printf("ENTER THE NAME WANT TO BE insert\n\t\n");
scanf("%s",nam1);
strcpy(nam[j],nam1);
f=0;
j=j+n;
}
else
j++;
}
if(!f)
{
printf("-----------------------------\n\n\n");
printf("displaying names\n\n\n\n");
for(k=1;k<=n;k++)
{ printf("no %d \t is ",k); puts(nam[k]);
}
}
else
{
printf("not found") ;
}
getch();
}

output:

ENTER THE NO. OF NAME

2

ENTER THE NAME

KARTHI

KARTHIKEYAN

memory

KARTHI

KARTHIKEYAN

ENTER THE NAME WANT TO BE replace

KARTHI

...............searching...............

==========found=========

ENTER THE NAME WANT TO BE insert

KARTHIKEN07@GMAIL.COM

..........................................

displaying names

NO 1 IS KARTHIKEN07@GMAIL.COM

NO 2 IS KARTHIKEYAN