Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Saturday, 7 September 2013

C Program to Print Floyds Triangle with Simple Example and Explanation

Floyd's triangle lists the natural numbers in a right triangle aligned to the left where
the first row is just 1
successive rows start towards the left with the next number followed by successive naturals listing one more number than the line above.
The first few lines of a Floyd triangle looks like this:
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
Program

#include<stdio.h>
#include<stdio.h>
void main()
{
int i,j,n,num;
clrscr();
printf("How many lines to print");
scanf("%d",&n);
num=1;
for(i=0;i<n;i++)
{
          for(j=0;j<=i;j++)
{
 printf("\t%d",num++);
 }
 printf("\n");
 }
 getch();
 }

Output:

How many lines to print
4

1  
2   3
4   5   6
7   8   9  10

Friday, 6 September 2013

C Program to Find the Area and Perimeter of Rectangle and Square [With Output]

Here is a simple program to find the area of the rectangle and square. Also the below program will find the perimeter of the rectangle.
Program

  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int l,,area,peri,sqr,side;
    clrscr();
    printf("\nEnter the length, width of rectangle & side of Square");
    scanf("%d%d%d",&l,&b,&side);
    area=l*b;
    peri=2*l+2*b;
    sqr=4*sqr;
    printf("\nArea of Rectangle=%d",area);
    printf("\n Perimeter=%d",peri);
    printf("\nSquare=%d",sqr);
    getch();
    }

Output:

Enter the length, width of rectangle & side of Square
3
5
6
Area of rectangle=15
Perimeter=16
Square=24

[Write a] C Program to Print the Transpose of Matrix

This c program prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. For example if a matrix is
1 2
3 4
5 6
then transpose of above matrix will be
1 3 5
2 4 6
When we transpose a matrix then the order of matrix changes, but for a square matrix order remains same.



Program

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

   void main()
    {
      int m1[10][10],tr[10][10],i,j,r,c;
      clrscr();
      printf("\n How many rows & columns in the matrix");
      scanf("%d%d",&r,&c);
      puts("\n Enter the elements:");
      for(i=0;i<r;i++)
for(j=0;j<c;j++)
 {
   scanf("%d",&m1[i][j]);
   tr[j][i]=m1[i][j];
 }
printf("\nThe transpose is:\n");
for(i=0;i<c;i++)
 {
   for(j=0;j<c;j++)
    printf("%d",tr[i][j]);
    printf("\n");
 }
printf("\n");
getch();
      }

Output:
How many rows and columns of matrix
2
3
Enter the elements 
1
2
3
4
4
5
The transpose is:
140
240
340

Tuesday, 18 December 2012

[PL 1]A Discrete Structure Program Using C Programming To Perform Various Set Operation


/*Program for various operations on set.
Operations covered :
1) Create() : for creating a new set with initial members
of the set
2) print() : diaplays all members of the set
3) Union() : finds union of two sets, set1[] and set2[] and
stores the result in set3[]
4) intersection() : finds intersection of two sets, set1[] and set2[]
and stores the result in set3[]
5) difference() :finds difference of two sets, set1[] and set2[]
and stores the result in set3[]
6) member() :function returns 1 or 0 ,depending onwhether the
element x belongs or not to a set.

7) symmdiff() : Finds Symmetric difference of two sets
Representation of a set
-----------------------
A set is representrd using an array of integers.
It may be declared as:
int set[30];
set[0] - gives number of elements in a set.
set[1] to set[29] are for storing set members.

Example :
A set,[2,11,3,5 6],when represebted will appear as:
[5][2][3][5][6][11][ ][ ][ ] 0 1 2 3 4 5 6 7 8 */

#define MAX 30
#include"STDIO.H"
#include"CONIO.H"
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set3[]);
void difference(int set1[],int set2[],int set3[]);
void symmdiff(int set1[],int set2[],int set3[]);
int member(int set[],int x);


void main()
{
int set1[MAX],set2[MAX],set3[MAX];
int x,op;
clrscr();
flushall();
set1[0]=set2[0]=set3[0]=0;
do
{
printf("\n1)Create\n2)Print\n3)Union\n4)Intersection\n5)Difference");
printf("\n6Symmetrec Difference \n7)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nCreting First Set*******");
create(set1);
printf("\nCreating Second Set*****");
create(set2);
break;
case 2: printf("\nFirst Set :\n");
print(set1);
printf("\n\nSecond Set :\n");
print(set2);
printf("\n\nThird Set :\n");
print(set3);
break;
case 3: Union(set1,set2,set3);print(set3);break;
case 4: intersection(set1,set2,set3);print(set3);break;
case 5: difference(set1,set2,set3);print(set3);break;
case 6: symmdiff(set1,set2,set3);print(set3);break;
}
printf("\npress a key............");
getch();
}while(op!=7);
}
/*creates set[] with initial elements*/
void create(int set[])
{ int n,i,x;
set[0]=0;/*make it a null set*/
printf("\n No. of elements in the set:");
scanf("%d",&n);
printf("\n enter set elements :");
for(i=1;i scanf("%d",&set[i]);
set[0]=n; //Number of elements.

}
void print(int set[])
{ int i,n;
n=set[0];/* number of elements in the set */
printf("\Members of the set :-->");
for(i=1;i printf("%d ",set[i]);
}
/* union of set1[] and set2[] is stored in set3[]*/
void Union(int set1[],int set2[],int set3[])
{ int i,n;
/* copy set1[] to set3[]*/
set3[0]=0;/*make set3[] a null set */
n=set1[0];/* number of elements in the set*/
//Union of set1,set2= set1 + (set2-set1)
for(i=0;i set3[i]=set1[i];

n=set2[0];
for(i=1;i if(!member(set3,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements
}
/*function returns 1 or 0 depending on whether x belongs
to set[] or not */
int member(int set[],int x)
{ int i,n;
n=set[0]; /* number of elements in the set*/
for(i=1;i if(x==set[i])
return(1);

return(0);
}
/*intersection of set1[] and set2[] is stored in set3[]*/
void intersection(int set1[],int set2[],int set3[])
{
int i,n;
set3[0]=0; /* make a NULL set*/
n=set1[0];/* number of elements in the set*/
for(i=1;i if(member(set2,set1[i])) /* all common elements are inserted in set3[]*/
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
/*difference of set1[] and set2[] is stored in set3[]*/
void difference(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
void symmdiff(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
//Calculate set1-set2
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
//Calculate set2-set1
n=set2[0];
for(i=1;i if(!member(set1,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements

}

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine