LED PANEL

Google It, You'll Get It.

Monday, March 28, 2011

Stacks - Coding Structure in C

-STACKS
(im using dashes to maintain the indentation here)

--creating the structure
Struct stack
{
Char data [MAX];
int top;
};

--declaring the functions
char pop (struct stack *stk);
void push (struct stack *stk, char item);


--initializing in Main Program(Function)
main()
{
stack s;
s.top = -1;

push( &s , "A" ) // Calling the Push Function while inserting letter "A" into a stack
pop( &s ) // Removing the element

}


--PUSH Function 
void push (stack *stk, char item)
{
If (stk ->top < MAX -1)
{
stk->top++;
stk->data[stk->top] = item;
}
Else
printf(“stack is full \n”);
}

--POP Function
char pop (stack *stk)
{
If(stk->top > -1 )
{
char x = srk -> data[stk->top];
stk->top--;
return x;
}
Else
{
printf(“stack is empty \n”);
return 0;
}
}

Stack - Real Life Example - Plates in Dispenser




Source :
http://cache1.asset-cache.net/xc/87626928.jpg?v=1&c=IWSAsset&k=2&d=910C62E22B9F47AAEA80970F9EC7FC33E34209546527F31A18ED6AAA293F9C98E30A760B0D811297

Stack - Real Life Example - Idly Maker




Source : http://www.tastypalettes.com/2008/02/pillows-of-heaven.html

EBook to learn stacks _ Free

Title : Stack Computers: the new wave
Author : Philip J. Koopman, Jr.


Link to Download : http://www.ece.cmu.edu/~koopman/stack_computers/stack_computers.zip


i hope this will be a very useful for the people who are learning stacks.

Stack - Real Life Example - Cafeteria tray example




Source : http://www.ece.cmu.edu/~koopman/stack_computers/sec1_2.html

Stacks - Introduction

-Stack is the First Data Structure in Programming.

-It is a method where we use LIFO ( LAST IN - FIRST OUT ).

LIFO stacks, also known as "push down" stacks, are the conceptually simplest way of saving information in a temporary storage location for such common computer operations as mathematical expression evaluation and recursive subroutine calling.

-It has only two fundamental operations called PUSH and POP.

--PUSH > to insert an element ( to the top )
The push operation adds an item to the top of the stack,
hiding any items already on the stack, or initializing the stack if it is empty.

--POP > to remove an element ( from the top )
The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.

Next > > >

I think now its time for me to move into Stacks and Queues..

Saturday, February 5, 2011

String starting with 'b; in a given series of strings


header file stdio.h
header file string.h


main()

{
int i;
const int stringLimit = 5;

char s[stringLimit][50]; //row and column

printf("enter the strings \n");
for(i=0;i gets(s[i]);

/*for(i=0;i // if(str[i][0]=='b';
printf("str %s \n",s[i]);*/

printf("\n strings Starting wIth letter 'b' \n");
for(i=0;i if(s[i][0]=='b')
printf("word %d : %s \n",i+1,s[i]);

}

Pig - Latin phrase from English language phrase ( Converting words into pig latin )

input : computer
output: omputercay ( omputer + c + ay )
-----------------------------------------------
input : jump
output: umpjay ( ump + j + ay )
-----------------------------------------------

//header file stdio.h
//header file string.h

main()

{
int i=0,wordcount=0;
char word[50];
char reorder[50];
char frst;

printf("enter the word : ");
gets(word);


frst = word[0];

for( i=0 ; word[i+1] != 0 ; i++)
{
reorder[i]=word[i+1];
}

reorder[i]=frst;
reorder[i+1]='a';
reorder[i+2]='y';
reorder[i+3]='\0';


printf("the pigLatin word is %s \n",reorder);


}

Monday, January 31, 2011

Scan and delete a desired a word from a sentence



#include

main()
{
int i=0;
int length = 0;
int word = 1;
int remove=0;
int tempw=1;

//read the string
char strng[50];
printf("enter the string : ");
gets(strng);
printf("Saved String : ");
puts(strng);
//printf("Initial values : Length %d, word %d, wotd numb %d, i %d. \n",length, word, wn, i);

//calculate the length
while(strng[i]!='\0')
{
length++;
i++;
}
printf("String Length : %d \n",length);

// calculate the no of words
i=0;
word=1;
while(i {
if(strng[i]==' ')
word++;
i++;
}
printf("nO. of Words : %d \n",word);

//askng for the word to delete
printf("Enter the word number you need to delete : ",word);
scanf("%d",&remove);

//scaning for that word
for(i=0;i { if(strng[i]==' ')
tempw++; // 2nd word count
if(tempw==remove)
printf("%c",strng[i]);
}
}

Scan a Character win a string and replace it if its available

#include<'stdio.h'>

main()
{
char name[20];
//input
printf("enter the name : ");
scanf("%s",&name);

//out
printf("**%s** \n",name);

//count
int count=1;
for(count=0;name[count]!='\0';count++);
printf("count %d \n",count);

flushall();

/*
char n;
printf("enter the letter : ");
scanf("%c",&n);

int ncount = 0, i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
ncount++;

printf("ncount %d \n",ncount);*/


//scanng lettr
char n;
printf("enter the letter (scan) : ");
scanf("%c",&n);
flushall();

//replacing letter
char m;
printf("enter the letter (replace): ");
scanf("%c",&m);

int i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
name[i]=m;

printf("%s",name);




}

Palindrome Word checker

What are palindrome Words? http://rinkworks.com/words/palindromes.shtml



#include
main()
{
char word[10], test='T';
int l = 0, i = 0; // l=length

printf("enetr the word : ");
scanf("%s",&word);

//length checkng
for (i=0;word[i]!='\0';i++)
{
l++; // length of the word
}
printf("wrd count %d \n",l);

//check
/* while(i!=l)
{
while( word[i]==word[l-i] )
{
printf("match");
i++;
//j--;
}

}
*/
//checkz
l=i-1;
for(i=0;i<(l/2);i++)
{
if(word[i]!=word[l-i])
test='F';
}

if(test=='T')
printf("Yes");
else
printf("No");
}

String Counter in Simple C

#include<'stdio.h'>
main()
{
char fname[10];
char lname[10];

int fc=0,lc=0,i=0;

printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);

printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);


i=0;
while(fname[i] != '\0')
{
fc++;
i++;
}

i=0;
while(lname[i] != '\0')
{
lc++;
i++;
}



printf("\nfrst : %d \n",fc);
printf("lst : %d \n",lc);
}

Using %s for strings + char array

#include<'stdio.h'>
main()
{
char fname[10];
char lname[10];

printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);

printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);
}

Larges and the smallest number in a series

#include
main()
{

int in[5], temp=0 ,i=0,j;

//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}

//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;

}
}
}

//-----output
/*printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
} */



//Print the samllest
printf("the smallest number : %d \n",in[0]);


//Print the largest
printf("the largest number : %d \n",in[4]);
}

Sorting

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

int in[5], temp=0 ,i=0,j;

//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}

//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;

}
}
}

//-----output
printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
}
}

using nested for loops to built a numeric triangle

o:
1
22
333
4444
----------------------

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

for(int loop=1; loop<=4;loop++)
{
for(int j=0; j {
printf("%d",loop);
}
printf("\n");
}

}

Using If and Else with decimal points in printf

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

float sal,tot, inc;

printf("enter your salary : ");
scanf("%f",&sal);

if( sal < 10000 )
{inc=sal*1.345/100;
tot=(sal*101.345)/100; }
else if( sal < 25000 )
{inc=sal*0.75/100;
tot=(sal*100.75)/100; }
else if( sal >=25000 )
{inc=sal*0.456/100;
tot=(sal*100.456)/100; }
printf("\n\n");
printf("Basic Sal : %.2f \n",sal);
printf("Increment : %.3f \n",inc);
printf("Total : %.2f \n",tot);

}

Counts Odds and Even Digits in a 5 Digit Number

#include<'stdio.h'>
main()
{
long numb, rem, i, temp = 0, sum, in, count = 0, even=0, odd=0;
printf("Input a 5 digit number \n");
scanf("%ld",&numb);
i = 1;

//printf("Input a digit \n");
//scanf("%ld",&in);

printf("\n");
while(i<=5)
{
temp=temp*10;
rem=numb%10;
//checking
if(rem%2>0)
odd++;
else
even++;

//if(rem==in)
//count++; //c= rem, numb =a
temp=temp+rem;
sum=sum+rem;
i++;
rem=0;
numb=numb/10;

}


//printf("%ld appears in here for %ld time \n",in,count);
printf("Total No. of Odd numberz : %ld \n",odd);
printf("Total No. of Even numberz : %ld \n",even);



// printf("inverse is %ld",temp);

}

Calculator using if and while to run till you need

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

main()
{
int a, b, sum;
char choice = 'y';
char select;
while(choice == 'y')
{
printf("Enter the two values : \n");
scanf("%d %d",&a,&b );
flushall();

printf("/n/n MENU ");
printf(" a. Addition ");
printf(" b. Subtraction ");
printf(" c. Multiplication ");
printf(" d. Division ");
printf(" e. Exit /n/n");

printf("/n Select from above : ");
scanf("%c",&select);
flushall();

while(select != 'e')
{
if(select=='a')
sum = a + b;
if(select=='b')
sum = a - b;
if(select=='c')
sum = a * b;
if(select=='d')
sum = a / b;

printf("/n %d",sum);
sum = 0;
}

}
printf("Continue : ( Y or N )");
scanf("%c",&choice);

}

Inverse and the sum of digits in 5 digit number

I: 52013
O: Invrse : 31025
O: Sum : 11
-------------------------------


#include<'stdio.h'>
main()
{
long numb, rem, i, sum=0;
printf("Input a 5 digit number : ");
scanf("%ld",&numb);
i = 1;

printf("inverse is ");
while(i<=5)
{
rem=numb%10;
numb=numb/10;
printf("%ld",rem);
i++;
sum = sum + rem;
rem=0;


}

printf("\n sum is %ld",sum);
}

Scan and count the times you have used a number within a 5 digit Number + creates the inverse of the input

Input: 59585
Input: Scan for Number 5
Output: 5 have printed 3 times
Output: inverse 58595

----------------------------------------------------------

#include<'stdio.h'>

main()
{
long numb, rem, i, temp = 0, sum, in, count = 0;
printf("Input a 5 digit number \n");
scanf("%ld",&numb);
i = 1;

printf("Input a digit \n");
scanf("%ld",&in);

printf("\n");
while(i<=5)
{
temp=temp*10;
rem=numb%10;
if(rem==in)
count++; //c= rem, numb =a
temp=temp+rem;
sum=sum+rem;
i++;
rem=0;
numb=numb/10;

}


printf("%ld appears in here for %ld time \n",in,count);

printf("inverse is %ld",temp);

}

Taking the Inverse of an Input

Input : 85974
Output : 47958


#include<"stdio.h">
main()
{
int numb, rem, i;
printf("Input a 5 digit number \n");
scanf("%d",&numb);
i = 1;

while(i<=5)
{
rem=numb%10;
numb=numb/10;
printf("%d",rem);
i++;
rem=0;

}


}

Inter-changing the values of two variables

#include

main()
{
int x, y, z;

printf("enter the first value \n");
scanf("%d",&x);
printf("enter the second value \n");
scanf("%d",&y);

z = x;
x = y;
y = z;

printf("\nx = %d", x);
printf("\ny = %d", y);
}

Using a WHILE to continusly run a programme until u wish to terminate

#include

main()
{
int x, y, sum, c=1 ;

while(c==1) // c = choice
{
printf("enter the first value \n");
scanf("%d",&x);
printf("enter the second \n");
scanf("%d",&y);

sum = x + y;

printf("the sum is %d", sum);

printf("retry?? if YES press 1, if NO press 0 \n");
scanf("%d",&c);

}

}

Simple Calculator with Four Operators

#include<"stdio.h">

main()
{
int x, y;
float sum1, sum2, sum3, sum4;

printf("enter the first value \n");
scanf("%d",&x);
printf("enter the second \n");
scanf("%d",&y);

sum1 = x + y;
sum2 = x - y;
sum3 = x * y;
sum4 = x / y;

printf("the x + y is %f", sum1);
printf("\nthe x - y is %f", sum2);
printf("\nthe x * y is %f", sum3);
printf("\nthe x / y is %f", sum4);

}

Testing For a Prime Input(Number)

#include

main()
{
int i=1;
int num;
int choice = 1;

while(choice ==1)
{
printf("please enter the value to identify\n");
scanf("%d",&num);
printf("\n");

while(i<=num)

{
int count=0;
int a=1;

while(a<=i)
{
if(i%a==0)
{ count++; }
a++;
}

if(count==2 && num == i)
{
printf("%d", i);
printf(" a prime number \n\n");
}
i++;


}
printf("----------------------------------------\n"); //
printf("retry?? if YES press 1, if NO press 0\n"); //
scanf("%d",&choice); //
printf("----------------------------------------\n"); //
i=1;
}
// return 0;
}

Finiding a Prime Number or Not

#include<"stdio.h">

main()

{

int input, loop =2, rem, flag = 0;
printf("enter a number : ");
scanf("%d",&input);

if(input==1)
{printf("not a prime");}
else
{
while(loop{
rem=input%loop;
if(rem==0)
{
printf("Not a Prime");
loop=input;
flag =1 ;
}
loop++; // loop = loop + 1
}
if(flag==0)
{printf(Yes its prime number");}
}
}

Multiple Reading and Writing

#include"< stdio.h >"

main()
{
int x,y,z;

// Declaring three Integer variables where u can store numbers within range

printf("Enter Three Numbers : (Press Enter after each number)\n");
scanf("%d %d %d",&x,&y,&z);

// %d says the input is an integer. (it have used three times since three inputs)
// &x point gives the address of variable x to store the value on x
// &y point gives the address of variable x to store the value on y
// &z point gives the address of variable x to store the value on z

printf("the values you entered is : %d %d %d ",x,y,z);

// %d says the input is an integer (it have used three times since three outputs)
// x gives the value which have stored in it
// y gives the value which have stored in it
// z gives the value which have stored in it

}

Read and Write

#include"< stdio.h >"

main()
{
int x; // Declaring an Integer variable where u can store numbers within range

printf("Enter A Number : ");
scanf("%d",&x);
// %d says the input is an integer.
// &x point gives the address of variable x to store the value of x

printf("the value you entered is : %d ",x);
// %d says the input is an integer
// x gives the value which have stored in it

}

Fundamental - "Welcome to C"

#include"< stdio.h >"

main()
{
printf("Welcome to C Programming \n"); // displays output
printf("Bye Bye\n");
}



========================================
When You Consider about the above coding, it is the simplest coding you can write using C.

#include"< stdio.h >" :
the header file which have used to complete this coding. basically it is a very common header file which we have to use.

"stdio" : stands for STandarD Input Output
".h" : indicates that it is a header file in the library

so the stdio.h means standard input output header file.

it uses to read a input from a user and to display or print anything for the user.

main():
display or commands the compiler saying that it is the main programme of the coding.
since its a small coding you wont feel the advantage and purpose of writing it. but when u learn the 'functions', im sure u'll undrstand it very well.

{}:
curly braces are neccesary or in other words its a must in a coding to seperate each parts, and also to seperate functions(or for now just think it like processes).

printf:
it have used to display a the message which is in between the Parentheses
[ Ex: (xxx_message_xxx) ].
It is a pre defined functioned which is actuly defined in the header file called stdio.h. to use that function only we need to use this header file. So since the display or the output is necessary in a programe we use this header file most of the time.

"\n" :
command to use the next line.

//comment(or a message about the code) :
that how u make a comment, and i'm gonna use it in further codings also.... all the things which you are typing after two forward slashes wont get compile from the compiler. it consider all those after // as comments.