Sunday, 16 September 2012

C program without main function

Every one knows that C program execution starts from main() function. But I ll like to show u one program that can run without main() function.


#include<stdio.h>
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()

{
printf(” hello “);
}


Here we are using preprocessor directive #define with arguements to show that program runs without main() function. But in reality it runs with a hidden main function.
The '##' operator is called the token pasting or token merging operator. That is we can merge 2 or more characters with it.
Note : A preprocessor is program which processess the source code before compilation.
So the moral is that we can never run a program without main() function here we are playing just a trick.

Make Your computer speak what you type



1.Open your notepad and type below commands

Dim message, sapi
message=InputBox(“Enter the text you want spoken”,”Speak This”)
Set sapi=CreateObject(“sapi.spvoice”)
sapi.Speak message

2.Save this notepad file with extension .VBS
3.Double click this file, a window will appear with title "SPEAK THIS" enter the text below which you want computer to speak.

Self destructing program in C


This Program will destruct itself after execution.....

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
printf(“This program will destroy itself if u press any key!!!n”);
getch();
remove(_argv[0]);/*array of pointers to command line arguments*/
}

What is the difference between 32 bit and 64 bit operating system?

People ask me on daily basis,"What is the difference between 32 bit and 64 bit Operating System?" Most of you are running Windows XP or Vista in its 32-bit iteration. But as hardware gets cheaper, people are curious as to what the 64-bit operating system has to offer. First let’s see if we can grasp the difference between 32- and 64-bit.
Think of your computer as a series of tubes that can either be 32 or 64 bits wide. When you have the smaller 32-bit size, there is more potential for bottlenecks to occur. Bottlenecks slow down your system because one process has to wait for another to finish before it can begin. But if you want to have 64-bit wide tubes, your computer needs to be thinking in 64-bit so your software and hardware all need to support 64-bit.

If you do not know the difference between 32-bit and 64-bit, I would have told you in the past that you are running a 32-bit version of Windows. But now with Windows 7 I am seeing more and more 64-bit operating systems shipped by default without the end users knowledge. Don’t get me wrong, a 64-bit system is better but you also need to be running 64-bit programs and have a 64-bit processor or else all the trouble of setting up the 64-bit operating system would be worthless.
On a 32-bit operating system, you are restricted to a maximum of 4 gigabytes of RAM. On a 64-bit operating system, you really do not have a limit. Let’s look at Wikipedia and find out the maximum amount of RAM for a 64-bit operating system:
That is a huge amount of RAM! Normally when you exhaust your physical RAM on a 32-bit system, it has to use virtual memory or hard disk space to pick up the slack. On a 64-bit system, you can install as much RAM as you can to cover your overhead. From here on, 32-bit operating systems will be referred to as x86 and 64 bit operating systems as x64. You can tell what you are running by right clicking on My Computer and choosing Properties.  Below is a shot of a 64-bit machine using 12GB of memory.

And in this shot, we see a 32-bit machine trying to use 7GB of RAM.. Not going to happen!
If you are running 3D modeling systems or AutoCAD systems, you can benefit from a x64 bit architecture but remember, you need to be running ALL x64 applications, print drivers and anything else you are setting up on your system to realize its full potential. Not all programs have been created for x64 yet and you will find yourself installing applications to your Program Files x86 directory. On a x64 machine, you will have two Program File directories — one for 32-bit and one for 64-bit applications.
So after reading through that and you still want to run a x64 operating system, you will need to make sure your processor supports x64. Most new servers and new computers bought this year or beyond will support x64 but you will still need to check. Here are some facts you should know (taken from ZDNet):
Almost all new servers sold within the last two years from AMD or Intel will have x64 capability.
Most mid- to high-end desktop processors from AMD or Intel within the last year have x64 capability.
Some higher-end Semprons have x64; lower-end Semprons do not.
No AMD Durons have x64.
All AMD Opteron processors have x64.
All AMD X2, FX, and Athlon64 chips have x64.
All Intel Pentium D and Celeron D chips have x64.
All AMD Turion notebook processors have x64.
All Intel Core 2 processors (mobile, desktop, and server) have x64.
No Intel Core Duo notebook processors have x64
No Intel Pentium M notebook processors have x64.
If you are still not sure if your processor can support x64 check out GRC’s SecurAble and let them help you figure it out! You might also want to check out Mahendra’s post How To Choose Between 32-bit & 64-bit Windows 7 Operating Systems.
If you are running a server that has all its hardware and software certified for x64, then you should install the 64-bit version but beware of device drivers and any 32-bit environments because if I used the word difficult, it would be an understatement!



Saturday, 15 September 2012

Learn C with me Chapter 2


Chapter 2.

In Previous chapter we saw about basics of program interfacing with computer.
Now you might be knowing that every C code start with an header file. But very few of us might be knowing that why are this Header files used??
Lets start with it....
Start Your C programming software with me.

#include<stdio.h>//Header
#include<conio.h>//Header
/*
A header file is a group of
pre defined, general purpose,
functions.
        Examples of predefined functions are "Printf(), Scanf(), etc"

        We can include a header file in
our prg and can use its functions
directly.
*/

void main()
{
clrscr();//clear output window
//and postion cursor on top left.
printf("\nHello f");
printf("\nHello f1");
printf("\nHello f2");
printf("\a\a\a\a");//beep (alert)

//printf support outputting
//special characters  like \n,\t that
//are not actually displayed
//but are for cursor movement
//and other purposes.
        //Semicolon (;) indicates end of the predefined function.
}

Learn C with Me


Chapter 1.


To Know about any Languages u must first know the basics
So My first Question to u is..
What is a Computer?
//As soon as u see this question u will be thinking that am i gone mad to ask u this question ? But cant answer  it in perfect meaning.//
So basically,  a Computer is a device that can
exceute programs.

What is a program?
A set of instructions that performs
some specific task.

A program contains
1) Code  (Instructions)
2) Data (Information)
==================================
Code : Set of instructions.

The instructions are grouped into one
unit, called as a "function".
Every function has a unique name.



==================================
Data is classified as Input and Output.
Input : Information that we give in to a program
Ouput : Information that we get from a program

A program stores the data into "variables".

A variable is a block of memory.
A varible is identified by a unique name.
Further the variable is read/written
using the variables name.
Value of a variable can change.
A variable can hold only one value at
a time.
==================================
Thats all basic U have learned in this chapter we will continue to the next chapter....

Computer ASCII Codes for symbols and characters


ASCII

Hex

Symbol

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
TAB
LF
VT
FF
CR
SO
SI

ASCII

Hex

Symbol

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US

ASCII

Hex

Symbol

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
(space)
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/

ASCII

Hex

Symbol

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?

ASCII

Hex

Symbol

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O

ASCII

Hex

Symbol

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
50
51
52
53
54
55
56
57
58
59
5A
5B
5C
5D
5E
5F
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_

ASCII

Hex

Symbol

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o

ASCII

Hex

Symbol

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
70
71
72
73
74
75
76
77
78
79
7A
7B
7C
7D
7E
7F
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~


Snake game in C


//Build Your own Snake game//


#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<dos.h>


#define ESC 27
#define UPARR 72
#define LEFTARR 75
#define DOWNARR 80
#define RIGHTARR 77
#define SAVE 60
#define LOAD 61


main()
{
void starting(void);
void make_xy(char **,char **);
void getrand(char *,char *,char *,char *,char *,int,char);
char getkey(char,char);
void savegame(char *,char *,int,char);
int loadgame(char *,char *,char *);
void win_message(void);


char *x,*y,pos_x,pos_y,num,out=0,old_ch=0,ch=0,new_ch,new_x,new_y,old_num=0;
int i,length=6;


starting();
make_xy(&x,&y);
getrand(&pos_x,&pos_y,&num,x,y,length,ch);


while(!out){
if((new_ch=getkey(old_ch,ch))==ESC)
out=2;
if(out)
break;
if(new_ch==SAVE)
savegame(x,y,length,old_ch);
else if(new_ch==LOAD){
length=loadgame(x,y,&ch);
getrand(&pos_x,&pos_y,&num,x,y,length,ch);
}
else
ch=new_ch;
new_x=x[0];
new_y=y[0];
if(ch==UPARR)
new_y=y[0]-1;
else if(ch==LEFTARR)
new_x=x[0]-1;
else if(ch==DOWNARR)
new_y=y[0]+1;
else if(ch==RIGHTARR)
new_x=x[0]+1;
old_ch=ch;
if((new_x<2)|(new_y<2)|(new_x>79)|(new_y>22))
out=1; /* HIGHEST POSSIBLE SCORE ÷ (78*21-6)*5 = 8160 ÷ 10,000 */
for(i=1;i<length-!old_num;i++) /* NOT "length": TAIL-END MAY MOVE AWAY! */
if((new_x==x[i])&(new_y==y[i])){
out=1;
break;
}
if((pos_x==new_x)&(pos_y==new_y)){
old_num+=num;
/* x=(char *)realloc(x,(score+6)*sizeof(char));
y=(char *)realloc(y,(score+6)*sizeof(char)); */
/* if((x==0)|(y==0)) */ /* PROBLEM IS NOT HERE */
/* x=x;*//* SOMEHOW realloc ISN'T COPYING PROPERLY */
getrand(&pos_x,&pos_y,&num,x,y,length,ch);
}
if(!old_num){
gotoxy(x[length-1],y[length-1]);
putchar(' ');
}
else{
length++;
if(length==1638){
win_message();
return 0;
}
gotoxy(67,25);
printf("Score = %5d",length-6);
old_num--;
x[i+1]=x[i];
y[i+1]=y[i];
}
for(i=length-1;i>0;i--){
x[i]=x[i-1];
y[i]=y[i-1];
if(i==1){
gotoxy(x[i],y[i]);
putchar('Û');
}
}
x[0]=new_x;
y[0]=new_y;
gotoxy(x[0],y[0]);
printf(" \b"); /* USE THE FUNCTION _setcursortype() */
if(out)
break;
delay(99);
}
if(out==1){
gotoxy(1,24);
printf("The snake collided with the wall or with itself!\n"
"GAME OVER!!\t\t(Press 'q' to terminate...)");
gotoxy(x[0],y[0]);
while(toupper(getch())!='Q');
}
clrscr();
printf("Hope you enjoyed the game\n\n\t\tBye!\n");
return 0;
}


/*-------------------------------------------------------------------------*/


void starting()
{
char i;


clrscr(); /* FIRST TO DRAW A BOUNDARY for THE GAME */
putchar('É');
for(i=0;i<78;i++)
putchar('Í');
putchar('»');
gotoxy(1,23);
putchar('È');
for(i=0;i<78;i++)
putchar('Í');
putchar('¼');
window(1,2,1,23);
for(i=0;i<21;i++)
cprintf("º");
window(80,2,80,23);
for(i=0;i<21;i++)
cprintf("º"); /* THE BOUNDARY IS DRAWN */
window(1,1,80,25);
gotoxy(38,12);
printf("ÛÛÛÛÛ "); /* THE "SNAKE" IS PUT for THE FIRST TIME */
gotoxy(1,24);
printf("Welcome to the game of SNAKE!\n(Press any arrow key to start now,"
" Escape to leave at any time...)"); /* WELCOME MESSAGE */
gotoxy(43,12);
while(!kbhit());
gotoxy(30,24);
delline();delline(); /* REMOVING MESSAGE */
cprintf("\n( EAT THE NUMBER !! ) Score = 0");
gotoxy(43,12); /* GO TO THE HEAD OF THE SNAKE */
}


void make_xy(char **px,char **py)
{
char i;


*px=(char *)malloc(1638*sizeof(char)); /*EARLIER IT WAS 6, NOT 1638; BUT*/
*py=(char *)malloc(1638*sizeof(char)); /*realloc IS NOT COPYING PROPERLY*/
for(i=0;i<6;i++){
(*px)[i]=43-i;
(*py)[i]=12;
} /* THE TWO ARRAYS for COORDINATES OF THE SNAKE ARE SIMULATED */
}


void getrand(char *px,char *py,char *pn,char *x,char *y,int length,char ch)
{
int allowed=0,i; /* i AND length MUST BE int */


while(!allowed){
allowed=1;
srand((unsigned)time(0));
*px=rand()%78+2; /* GENERATING RANDOM POSITIONAL COORDINATES for */
srand((unsigned)time(0));
*py=rand()%21+2; /* PUTTING A RANDOM NUMBER */
if(ch==UPARR){
if((*px==x[0])&(*py==y[0]-1))
allowed=0;
}
else if(ch==DOWNARR){
if((*px==x[0])&(*py==y[0]+1))
allowed=0;
}
else if(ch==LEFTARR){
if((*px==x[0]-1)&(*py==y[0]))
allowed=0;
}
else if((ch==RIGHTARR)&(*px==x[0]+1)&(*py==y[0]))
allowed=0;
for(i=0;(i<length)&&allowed;i++)
if((*px==x[i])&(*py==y[i]))
allowed=0;
} /* THE RANDOM NUMBER GENERATED SHOULD NOT BE PUT ON SNAKE'S BODY */
srand((unsigned)time(0));
*pn=rand()%9+1; /* THE NUMBER */
gotoxy(*px,*py);
putchar(*pn+48);
gotoxy(x[0],y[0]);
}


char getkey(char old_ch,char ch)
{
char i;


if(kbhit())
for(i=0;i<5;i++){ /* if i too low, takes too many keystrokes */
while((ch=getch())==0);
if(ch==27){
/* out=2;
i=5;
break;*/return ch;
}
if((ch!=LOAD)&(ch!=SAVE)&(ch!=UPARR)&(ch!=DOWNARR)&
(ch!=LEFTARR)&(ch!=RIGHTARR))
continue;
if((ch!=old_ch)|(!kbhit()))
break;
}
else
for(i=0;(i<12)&(!kbhit());i++)
delay(100);
return ch;
}


void savegame(char *px,char *py,int length,char ch)
{
FILE *fp;
int i;


rename("snake.sav","snake.bak");
fp=fopen("snake.sav","wb");
fprintf(fp,"%d %c",length,ch);
for(i=0;i<length;i++)
fprintf(fp,"%c%c",px[i],py[i]);
fclose(fp);
}


int loadgame(char *px,char *py,char *pch)
{
FILE *fp;
int length,i;


fp=fopen("snake.sav","rb");
if(!fp){
clrscr();
puts("ERROR: no saved game found in current directory!!!\n\n\t\t"
"Exiting...\n");
sleep(3);
exit(1);
}
window(2,2,79,22);
clrscr();
/* fscanf(fp,"%d %c ",&length,pch);*/
fscanf(fp,"%d %c",&length,pch);
for(i=0;i<length;i++){
/* fscanf(fp,"%d %d ",&px[length],&py[length]);*/
fscanf(fp,"%c%c",&px[i],&py[i]);
gotoxy(px[i]-1,py[i]-1);
putchar('Û');
}
window(1,1,80,25);
gotoxy(30,24);
delline();delline(); /* REMOVING MESSAGE */
cprintf("\n( EAT THE NUMBER !! ) Score = %5d",length-6);
gotoxy(px[0],py[0]);
printf(" \b");
fclose(fp);
return length;
}


void win_message()
{
window(1,1,80,25);
gotoxy(1,24);
delline();delline();
textcolor(14);
cprintf("CONGRATULATION!! YOU HAVE COMPLETED THE GAME!!\r\n"
"(Press any key to terminate...)");
clrscr();
textcolor(7);
}
//cscanf: WHEN YOU PRESS ENTER, CURSOR MOVES TO BEGINNING OF current LINE!!

Enjoy Ur Game!!!!!!!!!!!!!!!!!!!!!

Shutdown Your PC using VB


 Const EWX_LogOff As Long = 0
       Const EWX_SHUTDOWN As Long = 1
       Const EWX_REBOOT As Long = 2
       Const EWX_FORCE As Long = 4
       Private Declare Function ExitWindows _
               Lib "User32" Alias "ExitWindowsEx" _
               (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
     
     Private Sub Command1_Click()

          'Shut down windows
          ExitWindows EWX_SHUTDOWN, &HFFFFFFFF
     
     
     End Sub
     
     Private Sub Command2_Click()
   
         
          ExitWindows EWX_REBOOT, &HFFFFFFFF
     
     
     End Sub
     
     

Aakash Pokharkar's blog: Student Database project using Linked List.

Student Database project using Linked List.: #include #include #define SIZE 10 struct Student { int rno; char name[20]; float marks; }; typedef struc...

How to Hack Administrator Password in Windows 7

How to Hack Administrator password in xp

simplified Singly Linked List Program in C


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


struct node
{
int data;//to hold information
struct node * next;//to hold address of sequentially next node
};

typedef struct node node;

node * create()//create a linked list
{
node *h, *p, *n;
char ch;

h = NULL;//no address

do
{
//allocate memory for a node

n = (node*) malloc(sizeof(node));

//initialize the node
printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//connect
if(h == NULL)
{//true for 1st execution
h = n;
p = n;
}
else
{//true for rest of the executions
p->next = n;
p = n;
}

printf("\n continue ? ");
fflush(stdin);
scanf("%c", &ch);

}while(ch == 'y');

return h;
}

void disp(node *h)
{
node *p;

printf("\n");

p = h;
while(p != NULL)
{
printf("  %d", p->data);
p = p->next;
}//while
}

int count(node *h)
{
int c = 0;
node *p;
p = h;

while(p != NULL)
{
c++;
p = p->next;
}

return c;
}

//one imp feature of a linked list
//is dynamic expansion and shrink

node * addNodeAtHead(node *h)
{
node *n;

//1 create a node
n = (node*) malloc(sizeof(node));

//init the node
printf("\n enter data for node ");
scanf("%d", &n->data);
//n->next to initialize while connection

//2 connect node as head
n->next = h;

//3 mark n as new head node
h = n;

//update head in main
return h;

}

node * delNodeAtHead(node *h)
{
node *p;
if(h != NULL)
{
//catch the existing head node
p = h;
//set next node as head
h = h->next;
//deallocate the old head node
free(p);
}
else
{
printf("\n List not created");
}
return h;
}

void addNodeInBetween(node *h)
{
int tot, pos;
int i;
node *n;
node *p, *q;

tot = count(h);//tot number of nodes in list

printf("\n enter pos of new node ");
scanf("%d", &pos);

if(pos <=1 || pos >tot)
{
printf("\n Invalid intermediate pos");
}
else
{
//we have a valid pos node
//so we can add a node in between

//1 create a node
n = (node*) malloc(sizeof(node));

//init the node
printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//2 connect at pos
p = h;

for(i = 1; i< pos; i++)
{
q = p ;
p = p->next;
}

//now p is at node : pos
//and q is at node : pos -1

q->next = n;
n->next = p;


}//else
}

void delNodeInBetween(node *h)
{
int tot, pos;
int i;
node *p, *q, *r;

tot = count(h);//tot number of nodes in list

printf("\n enter pos of new node ");
scanf("%d", &pos);

if(pos <=1 || pos >=tot)
{
printf("\n Invalid intermediate pos");
}
else
{
//we have a valid pos node
//so we can del a node

//catch the node

p = h;
for(i = 1; i< pos; i++)
{
q = p ;
p = p->next;
}

//now p is at node : pos
//and q is at node : pos -1

r = p->next;

//deallocate the node
free(p);
q->next = r;

}//else
}

void addNodeAtTail(node *h)
{
node *n, *p;

if(h != NULL)
{
//1. create a node
//memory allocation
n = (node*) malloc( sizeof(node));
//init the node

printf("\n enter data for node ");
scanf("%d", &n->data);
n->next = NULL;

//2. connect as tail
p = h;
while(p->next != NULL)
{
p = p->next;
}

//now p is at the last node
p->next = n;

}
else
{
printf("\n List Not Created");
}
}


node* delNodeAtTail(node *h)
{
node *p,*q;

if(h != NULL)
{
if(h->next == NULL)
{//single node in list
p = h;
h = NULL;//set h as NULL
free(p);//deallocate p
}
else
{//multiple nodes in list

//1. catch the last node

p = h;
while(p->next != NULL)
{
q = p;
p = p->next;
}

//now p is at last node
//and q is at last -1 node

//2. set new tail node
q->next = NULL;//set q as tail node
free(p);//deallocate node p

}//else

}
else
{
printf("\n list not created ");
}
return h;
}

void dispReverse(node *h)
{
int i, j,tot;
node *p;

tot = count(h); //count no of nodes in list

printf("\n");

for(i = 1; i<=tot; i++)
{//for every node

p = h; //p is at head/first node
//jumps
for(j = 1; j<= tot-i; j++)
{
p = p->next;
}

//disp
printf(" %d", p->data);
}//for(i

}

node* search(node *h, int val)
{
node *p;
p = h;

while(p != NULL)
{
if(p->data == val)
{//found
return p; //address of node where found
}

p = p->next;

}//while

return NULL;//not found
}

reverseList(node *h)
{
node *a, *b, *c;

a = h;
c = NULL;


while(a != NULL)
{
b = a;
a = a->next;
b->next = c;
c = b;
}
return c; //new head node
}

void main()
{
node *head, *p;
int x;
int ch;
head = NULL;

clrscr();

do
{
printf("\n 1. create list ");
printf("\n 2. disp list ");
printf("\n 3. disp list in reverse ");
printf("\n 4. count nodes in list ");
printf("\n 5. search a val in list ");
printf("\n 6. add node at head ");
printf("\n 7. add node in between ");
printf("\n 8. add node at tail ");
printf("\n 9. del node at head ");
printf("\n 10. del node in between ");
printf("\n 11. del node at tail ");
printf("\n 12. reverse linked list");
printf("\n 13. exit ");
printf("\n enter choice ");
scanf("%d", &ch);


switch(ch)
{
case 1:
head = create();
break;
case 2:
disp(head);
break;
case 3:
dispReverse(head);
break;
case 4:
x = count(head);
printf("\n No of nodes : %d ",x);
break;
case 5:
printf("\n enter data to search ");
scanf("%d", &x);
p = search(head, x);
if(p != NULL)
{
printf("\n %d found", x);
}
else
{
printf("\n %d not found",x);
}
break;

case 6:
head = addNodeAtHead(head);
break;
case 7:
addNodeInBetween(head);
break;
case 8:
addNodeAtTail(head);
break;
case 9:
head = delNodeAtHead(head);
break;
case 10:
delNodeInBetween(head);
break;
case 11:
head = delNodeAtTail(head);
break;
case 12:
head = reverseList(head);

}//switch
}while(ch != 13);
}//main

Student Database project using Linked List.


#include<stdio.h>
#include<conio.h>
#define SIZE 10

struct Student
{
int rno;
char name[20];
float marks;
};
typedef struct Student Student;

void add(Student arr[], int pos)
{
float temp;
printf("\n Enter Student data ");
printf("\n Rno ");
scanf("%d", &arr[pos].rno);
printf("\n Name ");
flushall();
scanf("%s", arr[pos].name);
printf("\n Marks ");
scanf("%f", &temp);
arr[pos].marks = temp;

}//add

void dispAll(Student arr[], int tot)
{
int i;

printf("\n Rno    Name    Marks ");

for(i =0; i<tot; i++)
{
printf("\n %3d %10s %3.2f ", arr[i].rno, arr[i].name, arr[i].marks);
}
}//dispAll

int search(Student arr[], int tot, int r)
{//linear /sequentail search
int i;
for(i =0; i<tot; i++)
{
//x = strcmp(arr[i].name, n);
//if(x == 0)
//if(arr[i].marks ==m)
if(arr[i].rno == r)
{//match
return i; //index where found
}
}//for
return -1; //not found
}//search


void modify(Student arr[], int tot)
{
int r, x;
float temp;

printf("\n Enter rno of record to modify ");
scanf("%d", &r);

//look for the record
x = search(arr, tot, r);

if(x != -1)
{//modify
printf("\n Existing Data ");
printf("\n %3d %10s %3.2f ", arr[x].rno, arr[x].name, arr[x].marks);

printf("\n Enter new data ");

printf("\n Rno ");
scanf("%d", &arr[x].rno);
printf("\n Name ");
flushall();
scanf("%s", arr[x].name);
printf("\n Marks ");
scanf("%f", &temp);
arr[x].marks = temp;
}//if
else
{
printf("\n Record Not found");
}
}//Modify


int del(Student arr[], int tot)
{
int r, x, j;

printf("\n Enter rno of record to delete ");
scanf("%d", &r);

//look for the record
x = search(arr, tot, r);

if(x != -1)
{//delete
printf("\n Deleting ");
printf("\n %3d %10s %3.2f ", arr[x].rno, arr[x].name, arr[x].marks);

//slow shift method
for(j = x +1; j< tot; j++)
{
arr[j-1] = arr[j];
}
return 1;
}//if
else
{
printf("\n Record Not found");
return 0;
}
}//Modify

void sort(Student arr[], int tot)
{
Student temp;
int i, j;
for(i = tot-1; i >0; i--)
{
for(j =0 ; j< i;j++)
{
if(arr[j].rno > arr[j+1].rno)
{ //swap
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}//for(j
}//for(i
}//sort

void main()
{
Student sarr[SIZE];
int ch;
int indx= 0;
int r, flag;

do
{
printf("\n 1. Add Student ");
printf("\n 2. Modify Student ");
printf("\n 3. Delete Student ");
printf("\n 4. Search Student ");
printf("\n 5. Sort Student ");
printf("\n 6. Display All Students ");
printf("\n 7. Exit ");
printf("\n Enter choice ");

scanf("%d", &ch);

switch(ch)
{
case 1:
if(indx < SIZE)
{
add(sarr,indx);
indx++;
}
else
{
printf("\n Student Table Is FULL");
}
break;
case 2:
modify(sarr, indx);
break;
case 3:
flag = del(sarr, indx);
if(flag == 1)
{//deletion success
indx--;
}
break;
case 4://search
printf("\n Enter rno to search ");
scanf("%d", &r);
flag = search(sarr, indx, r);
if(flag != -1)
{
printf("\n Record found at index %d", flag);
printf("\n %3d %10s %3.2f ", sarr[flag].rno, sarr[flag].name, sarr[flag].marks);
}
else
{
printf("\n Record Not Found");
}
break;
case 5:
sort(sarr, indx);
break;
case 6:
dispAll(sarr, indx);
break;
}//switch

}while(ch != 7);
}