Showing posts with label Basics of C. Show all posts
Showing posts with label Basics of C. Show all posts

Stack as an array Data Structure

Stack as an array Data Structure

Stack contains an ordered collection of elements. An array is used to store ordered list of elements. Hence, it would be very easy to manage a stack if we represent it using an array. However, the problem with an array is that we are required to declare the size of the array before using it in a program. This means the size of an array should be fixed. Stack on the other hand does not have any fixed size. It keeps on changing, as the elements in stack are popped or pushed.

Though an array and a stack are totally different data structures, an array can be used to store the elements of a stack. We can declare the array with a maximum size large enough to manage a stack. As a result, the stack can grow or shrink within the space reserved for it.

Related Links :

How to Declare Union in C ? | Using Union in C | Union Example in C | C Program to Explain use of union

How to Declare Union in C ? | Using Union in C | Union Example in C | C Program to Explain use of union



union tag
{
union_member1;
union_member2;
union_member3;
..
..
..
union_memberN;
}instance;





    Union Members that compose a union , all share the same storage area within the computers memory
    Each member within a structure is assigned its own unique storage area
    Thus unions are used to observe memory.
    Unions are useful for application involving multiple members , where values need not be assigned to all the members at any one time.

Related Links :

why we put #include in C?

why we put #include in C | # Include stands for | include working | Include in C

If a line starts with a hash, denoted by #, kit tells the compiler that a command should be sent to the C PREPROCESSOR. The C preprocessor is a program that is run before compilation takes place ( hence the name).

Basically, when the preprocessor finds #include it looks for the file specified and replaces #include with the contents of that file. This makes the code more readable and easier to maintain if you needed to use common library functions.


Header files have the extension .h and the full filename follows from the #include directive . They contain functions that you may or may not have used in your program.

for e.g, the stdio.h file is required if you have used functions like printf() and scanf() in your program.

Related Links :

delay function in c

To use delay function in your program you should include the dos.h header file.



#include
#include

main()
{
printf("This c program will exit in 10 seconds.\n");

delay(10000);

return 0;
}

Related Links :

c assignment to find the sum of primary diagonal of a matrix

c assignment to find the sum of primary diagonal of a matrix




#include
void main()
{
int A[5][5],i,j,m,n,sum = 0;
clrscr();
printf("\n\n\t ENTER A ORDER OF THE MATRIX M,N...: ");
scanf("%d,%d",&m,&n);
printf("\n\n\t ENTER THE ELEMENTS OF THE MATRIX..:\n\n");
if(m == n)
{
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
gotoxy(20+j*4,12+i*2);
scanf("%d",&A[i][j]);
}
printf("\n");
}
for(i=1;i<=m;i++)
sum = sum + A[i][i];
printf("\n\t THE SUM OF PRIMARY DIAGONAL OF A MATRIX IS...: %d", sum);
}
else
{
printf("\n\t THE ORDER OF THE MATRIX IS NOT CORRECT");
printf("\n\n\t HELP : 'M' SHOULD BE EQUAL TO 'N'");
}
getch();
}

Related Links :

C Program to find the sum of the sine series


C Program to find the sum of the sine series , Sine Series In C, Sum of Sine Series In C Language, C Assignment to find Sin Series in C .

#include
#include
#include
void main()
{
int i = 2, n, s = 1, x, pwr = 1, dr;
float nr = 1, x1, sum;
clrscr();
printf("\n\n\t ENTER THE ANGLE...: ");
scanf("%d", &x);
x1 = 3.142 * (x / 180.0);
sum = x1;
printf("\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d", &n);
while(i <= n)
{
pwr = pwr + 2;
dr = dr * pwr * (pwr - 1);
sum = sum + (nr / dr) * s;
s = s * (-1);
nr = nr * x1 * x1;
i+= 2;
}
printf("\n\t THE SUM OF THE SINE SERIES IS..: %0.3f",sum);
getch();
}



Related Links :

C Program to calculate Discount in Bill



#include
#include
main()
{
float ttamt,qnt,rate,dis;
clrscr();
printf ("enter the value of rate and qty");
scanf ("%f%f",&rate,&qnt);
ttamt=qnt*rate;
if(ttamt>2000)
{
ttamt=ttamt-(ttamt*10)/100;
printf(" \n ttamt=%f",ttamt);
}
else
{


printf(" \n ttamt=%f",ttamt);
}
getch();
}


Related Links :

Convert string to integer without using library functions in c

Convert string to integer without using library functions in c




#include
int stringToInt(char[] );
int main(){

char str[10];
int intValue;

printf("Enter any integer as a string: ");
scanf("%s",str);

intValue = stringToInt(str);

printf("Equivalent integer value: %d",intValue);

return 0;
}

int stringToInt(char str[]){
int i=0,sum=0;

while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}

}

return sum;

}


Related Links :

C Program to find twin prime numbers, twin Prime Numbers by using C language

C Program to find twin prime numbers, twin Prime Numbers by using C language



void main()
{
int n,i,k,r,ary[50],p;
clrscr();
printf(" Enter Maximum Value For Numbers: ");
scanf("%d",&r);
i=1;
p=0;
while(i<=r)
{
k=0;n=1;
while(n<=i)
{
if(i%n==0)
k++;
n++;
}
if(k==2)
{
ary[p]=i;
p++;
}
i++;
}

for(n=0;n<p;n++)
{
if(ary[n+1]-ary[n]==2)
printf("\n %d and %d are TWIN PRIME Numbers ",ary[n],ary[n+1]);
}
getch();
}



Related Links :

Most c compilers Most widely used

Most c compilers Most widely used

There are various c compilers are variables. Some of these are:



S.N.
Name
Microprocessor
OS
1
Turbo c 3.0
8086
MS DOS
2
ANSIC C
80386
LINUX
3
Borland C 4.0
80386
WINDOW
4
Microsoft C
8086
MS DOS
5
Visual C++
80386
WINDOW






Related Links :

while loop without any Loop body



main()
{
int i=0;

while(i++,i<=8);
printf("%d ",i);
return 0;
}



Related Links :

c program to create dos command DIR, Program to create a DIR Dos Command

c program to create dos command DIR, Program to create a DIR Dos Command


#include “stdio.h”
#include “dos.h”
void main(int count,char *argv[]){
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0){
while (!a){
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else{
printf("File not found");
}
}



Related Links :

c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)

c program which display mouse pointer and position of pointer.(In x coordinate, y coordinate)


#include”dos.h”
#include”stdio.h”
void main()
{
union REGS i,o;
int x,y,k;
//show mouse pointer
i.x.ax=1;
int86(0x33,&i,&o);
while(!kbhit()) //its value will false when we hit key in the key board
{
i.x.ax=3; //get mouse position
x=o.x.cx;
y=o.x.dx;
clrscr();
printf("(%d , %d)",x,y);
delay(250);
int86(0x33,&i,&o);
}
getch();
}



Related Links :

C code to print or display lower triangular matrix

C code to print or display lower triangular matrix



#include
int main(){
int a[3][3],i,j;
float determinant=0;

printf("Enter the 9 elements For the matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
printf("\n Setting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}


return 0;
}



Related Links :

C Program to print or display lower triangular matrix

C Program to print or display lower triangular matrix


#include
int main(){
int a[3][3],i,j;
float determinant=0;

printf("Enter the 9 elements of the matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

printf("\nSetting zero in upper triangular matrix\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
if(i<=j)
printf("%d\t",a[i][j]);
else
printf("%d\t",0);
}


return 0;
}



Related Links :

C program to find inverse of a matrix , inverse of a matrix in C

C program to find inverse of a matrix , inverse of a matrix in C


#include

int main(){

int a[3][3],i,j;
float determinant=0;

printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);

printf("\nThe matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}

for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

printf("\nInverse of matrix is: \n\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
printf("\n");
}

return 0;
}



Related Links :

C Program to insert and delete a node from the binary search tree

C Program to insert and delete a node from the binary search tree



#include
#include
#include
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8) { insert(&bt,a[i]); i++; } clrscr(); printf(" Binary tree before deletion :\n"); inorder(bt); del(&bt,11); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,10); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,6); printf("\n Binary tree after deletion :\n"); inorder(bt); del(&bt,16); printf("\n Binary tree after deletion :\n"); inorder(bt); getch(); } /* inserts a new node in a binary search tree */ void insert(struct btreenode **sr,int num) { if(*sr==NULL) { (*sr)=malloc(sizeof(struct btreenode)); (*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}

/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}



Related Links :

C PROGRAMS CONVERT TEMPERATURE FROM DEGREE CELSIUS TO DEGREE FAHRENHEIT

C PROGRAMS CONVERT TEMPERATURE FROM DEGREE CELSIUS TO DEGREE FAHRENHEIT


#include
#include
main()
{
int c;
float f;
clrscr();
printf("Enter temp in degree Celsius");
scanf("%d",&c);
f=(float)9/5*c+ 32;
printf("\n temp in deg. Celsius=%d C and in deg. fah =%fF",c,f);
getch();
}



Related Links :

C program to convert Days into Year months, weeks and leftover days

C program to convert Days into Year months, weeks and leftover days



#include
#include
main()
{
int days,month,week,year;
clrscr();
year=0;
printf("enter days");
scanf("%d" ,&days);
month=days/30;
days%=30;
week=days/7;
days%=7;
year=month/12;
printf("equivalent months=%d weeks=%d and·
leftoverdays=%d" ,month,week,days);
if (year>0)
{
printf("\n The Years are %d",year);
}
getch();
}



Related Links :

C PROGRAM TO CREATE Triangle USING FOR LOOP TO OBTAIN THE Triangle

C PROGRAM TO CREATE Triangle USING FOR LOOP TO OBTAIN THE Triangle


#include



main()
{
int i,j;
for(i=7;i>0;i–)
{

for(j=i;j>0;j–)



{

printf(“*\t”);

}

printf(“\n\v”);

}

}





Related Links :


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!