Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Tuesday, February 17, 2015
while loop in C Part 2
Read previous tutorial while loop in C - Part 1.
In the last tutorial we have learnt about the while loops. Loops are used very frequently while writing big programs. And in the last tutorial we have learnt only the basics of the loop control structure. In this tutorial I will not write any program. Instead of it I will tell you about some tips and traps of using while loop in C.
If you have write any program using while loop for the first time. Then it is quite possible that you will get many errors. This tutorial mainly focuses to make you confident while writing loop control structure in programs.
initialise loop counter;
while(condition)
{ do this;
and this;
increment loop counter;
}
We can add any valid condition or expression with while keyword. Any expression which evaluates as non zero is said to be true and the expression which evaluates zero is said to be false.
2. We can use logical operators to describe condition. This is perfectly fine for all loops.
while(x<=60)
while(x>=50&&y<=75)
3. If you want to execute only one statement in while loop then you can also drop the curly braces { }. Default scope of while loop is one statement below the while keyword.
while(x<=20)
i=i+1;
is same as
while(x<=10)
{
i=i+1;
}
However if you want to execute multiple statements in while loop then it is compulsory to use curly braces.
4. Remember to increment or decrement the loop counter. Otherwise it will result in an infinite loop. One common mistake is given below.
main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
}
}
The above program will result in an infinite loop because we are not changing the value of loop counter. Condition is always true as value of loop counter remains same and the loop will get executed infinitely.
Correction of above code is given below.
main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
i=i+1;
}
}
5. Never write a semicolon after while keyword. It will result in an infinite loop. A common error code is given below.
main()
{
int i=1;
while(i<=10);
{
printf(“%d”, i);
i=i+1;
}
}
Checkout I have given semicolon after while keyword. So do not make this mistake.
So i=i+1 is same as i++ (post increment operator).
and i=i-1 is same as i—(post decrement operator).
i=i+1 is same as ++i (pre increment operator).
i=i-1 is same as --i (pre decrement operator).
Sounds confusing? As both are same.
Well write one program with below function to understand difference between them.
i=1;
j=10;
prinf("%d %d
", ++i, i++);
prinf("%d %d
",--i, i--);
prinf("%d %d
", ++j, j++);
prinf("%d %d
",--j, j--);
i=i+5 is same as i+=5.
Or
i=i-5 is same as i-=5.
Or
i=i*6 is same as i*=6.
This operator can be used with arithmetic operators like +, -, *, / and %.
In the last tutorial we have learnt about the while loops. Loops are used very frequently while writing big programs. And in the last tutorial we have learnt only the basics of the loop control structure. In this tutorial I will not write any program. Instead of it I will tell you about some tips and traps of using while loop in C.
If you have write any program using while loop for the first time. Then it is quite possible that you will get many errors. This tutorial mainly focuses to make you confident while writing loop control structure in programs.
Tips and traps of while loop
1. As I told you earlier the general form of while loop is.initialise loop counter;
while(condition)
{ do this;
and this;
increment loop counter;
}
We can add any valid condition or expression with while keyword. Any expression which evaluates as non zero is said to be true and the expression which evaluates zero is said to be false.
2. We can use logical operators to describe condition. This is perfectly fine for all loops.
while(x<=60)
while(x>=50&&y<=75)
3. If you want to execute only one statement in while loop then you can also drop the curly braces { }. Default scope of while loop is one statement below the while keyword.
while(x<=20)
i=i+1;
is same as
while(x<=10)
{
i=i+1;
}
4. Remember to increment or decrement the loop counter. Otherwise it will result in an infinite loop. One common mistake is given below.
main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
}
}
The above program will result in an infinite loop because we are not changing the value of loop counter. Condition is always true as value of loop counter remains same and the loop will get executed infinitely.
Correction of above code is given below.
main()
{
int i=1;
while(i<=10)
{
printf(“%d”, i);
i=i+1;
}
}
5. Never write a semicolon after while keyword. It will result in an infinite loop. A common error code is given below.
main()
{
int i=1;
while(i<=10);
{
printf(“%d”, i);
i=i+1;
}
}
Checkout I have given semicolon after while keyword. So do not make this mistake.
Few more operators
Post Increment/Decrement Operator
This operator is commonly used with loops. You must have noticed that most of the time we use expression i=i+1 to increment the loop counter. To make this a bit easy to write we can use post increment and decrement operator.So i=i+1 is same as i++ (post increment operator).
and i=i-1 is same as i—(post decrement operator).
Pre Increment/Decrement Operator
This operator is similar to post. But with a small difference. This operator gives priority to incrimination in the function.i=i+1 is same as ++i (pre increment operator).
i=i-1 is same as --i (pre decrement operator).
Sounds confusing? As both are same.
Well write one program with below function to understand difference between them.
i=1;
j=10;
prinf("%d %d
", ++i, i++);
prinf("%d %d
",--i, i--);
prinf("%d %d
", ++j, j++);
prinf("%d %d
",--j, j--);
Compound Assignment Operator
It is also similar to the above operators. The best way to understand them is by syntax.i=i+5 is same as i+=5.
Or
i=i-5 is same as i-=5.
Or
i=i*6 is same as i*=6.
This operator can be used with arithmetic operators like +, -, *, / and %.
Monday, February 16, 2015
C tutorial homeandlearn co uk
Read more »
Sunday, February 15, 2015
C program which reads a text and count all occurrences of a particular word

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,count=0;
char str1[100],str2[20],str3[20];
clrscr();
printf("Enter the text: ");
gets(str1);
printf("Enter word to count: ");
gets(str2);
while(str1[i]!=
C program to find sum of series 1 2 3 n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,sum=0;
cout<<"1+2+3+......+n";
cout<<"
Enter the value of n:";
cin>>n;
for(i=1;i<=n;++i)
sum+=i;
cout<<"
Sum="<<sum;
getch();
}
C program to convert given number of days into years weeks and days
#include<stdio.h>
#include<conio.h>
void main()
{
int y,w,d,a;
clrscr(); //to clear the screen
printf("Enter total number of days:");
scanf("%d",&d);
y=d/365;
a=d%365;
w=a/7;
d=a%7;
printf("
Years=%d
Weeks=%d
Days=%d",y,w,d);
getch(); //to stop the screen
}
Saturday, February 14, 2015
C program to find sum of series 1 2 3 2 5 2 n 2
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
long sum=0;
cout<<"1^2+3^2+5^2+......+n^2
Enter Value of n:";
cin>>n;
for(i=1;i<=n;i+=2)
sum+=(i*i);
cout<<"
Sum of given series is "<<sum;
getch();
}
#include<conio.h>
void main()
{
clrscr();
int n,i;
long sum=0;
cout<<"1^2+3^2+5^2+......+n^2
Enter Value of n:";
cin>>n;
for(i=1;i<=n;i+=2)
sum+=(i*i);
cout<<"
Sum of given series is "<<sum;
getch();
}
Thursday, February 12, 2015
C Program to Print Heart Shape With Happy Friendship Day Message Inside it
On this special day I thought to share something unique. So here is the program to print heart shape with happy friendship day message inside it. You can give this as a gift to your programmer friend. If you like it, dont forget to share it!
Also Read: C Program to Print India Map
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, y, size=10;
char ch=3;
string message(" Happy Friendship Day ");
int print_line = 4;
if (message.length() % 2 != 0) message += " ";
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
cout << ch;
}
else cout << " ";
}
cout<<"
";
}
for (x=1;x<2*size;x++)
{
for(y=0;y<x;y++) cout << " ";
for (y=0; y<4*size + 1 - 2*x; y++)
{
if (x >= print_line - 1 && x <= print_line + 1) {
int idx = y - (4*size - 2*x - message.length()) / 2;
if (idx < message.length() && idx >= 0) {
if (x == print_line) cout<<message[idx];
else cout << " ";
}
else cout << ch;
}
else cout << ch;
}
cout<<endl;
}
return 0;
}

Also Read: C Program to Print India Map
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, y, size=10;
char ch=3;
string message(" Happy Friendship Day ");
int print_line = 4;
if (message.length() % 2 != 0) message += " ";
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
cout << ch;
}
else cout << " ";
}
cout<<"
";
}
for (x=1;x<2*size;x++)
{
for(y=0;y<x;y++) cout << " ";
for (y=0; y<4*size + 1 - 2*x; y++)
{
if (x >= print_line - 1 && x <= print_line + 1) {
int idx = y - (4*size - 2*x - message.length()) / 2;
if (idx < message.length() && idx >= 0) {
if (x == print_line) cout<<message[idx];
else cout << " ";
}
else cout << ch;
}
else cout << ch;
}
cout<<endl;
}
return 0;
}

Source: http://stackoverflow.com/questions/20075775/print-heart-shape-with-words-inside
C Program for AVL Tree Implementation
An AVL (Adelson-Velskii and Landis) tree is a height balance tree. These trees are binary search trees in which the height of two siblings are not permitted to differ by more than one.
i.e. [Height of the left subtree – Height of right subtree] <= 1.
A C program is given below which performs various operations like creation, insertion, deletion and printing for an AVL tree.
Also Read: C Program to Create a Binary Tree Using Recursion [Linked Representation]
Also Read: What is Quick Sort? Algorithm and C Program to Implement Quick Sort
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedefstruct node
{ int data;
struct node *left,*right;
intht;
}node;
node *insert(node *,int);
node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);
int main()
{
node *root=NULL;
intx,n,i,op;
do
{
printf("
1)Create:");
1)Create:");
printf("
2)Insert:");
2)Insert:");
printf("
3)Delete:");
3)Delete:");
printf("
4)Print:");
4)Print:");
printf("
5)Quit:");
5)Quit:");
printf("
Enter Your Choice:");
Enter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1:printf("
Enter no. of elements:");
Enter no. of elements:");
scanf("%d",&n);
printf("
Enter tree data:");
Enter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
case 2:printf("
Enter a data:");
Enter a data:");
scanf("%d",&x);
root=insert(root,x);
break;
case 3:printf("
Enter a data:");
Enter a data:");
scanf("%d",&x);
root=Delete(root,x);
break;
case 4: printf("
Preorder sequence:
");
Preorder sequence:
");
preorder(root);
printf("
Inorder sequence:
");
Inorder sequence:
");
inorder(root);
printf("
");
");
break;
}
}while(op!=5);
return 0;
}
node * insert(node *T,int x)
{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}
T->ht=height(T);
return(T);
}
node * Delete(node *T,int x)
{ node *p;
if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2)//Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right !=NULL)
{ //delete its inordersuccesor
p=T->right;
while(p->left != NULL)
p=p->left;
T->data=p->data;
T->right=Delete(T->right,p->data);
if(BF(T)==2)//Rebalance during windup
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}
int height(node *T)
{
intlh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
if(lh>rh)
return(lh);
return(rh);
}
node * rotateright(node *x)
{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * rotateleft(node *x)
{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}
node * RR(node *T)
{
T=rotateleft(T);
return(T);
}
node * LL(node *T)
{
T=rotateright(T);
return(T);
}
node * LR(node *T)
{
T->left=rotateleft(T->left);
T=rotateright(T);
return(T);
}
node * RL(node *T)
{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}
int BF(node *T)
{
intlh,rh;
if(T==NULL)
return(0);
if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;
if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}
void preorder(node *T)
{
if(T!=NULL)
{
printf("%d(Bf=%d) ",T->data,BF(T));
preorder(T->left);
preorder(T->right);
}
}
voidinorder(node *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d) ",T->data,BF(T));
inorder(T->right);
}
}
Subscribe to:
Posts (Atom)

