Tuesday, 31 May 2011

Which Number is Largest? / Array

#define MAXSIZE 20
int max(int[], int);
void main()
{
 int list[MAXSIZE];
 int size=0;
 int num;
 do
  {
   printf("Type number: ");
   scanf("%d",list[size]);
  }
 while(list[size++] != 0);
 num=max(list,size-1);
 printf("Largest Number is %d",num);
}
int max(int list[],int size)
{
  int dex,max;
  max = list[0];
  for(dex=1;dex<size;dex++)
   if(max < list[dex])
    max = list[dex];
  return(max);
}


Output:


Type Number: 42
Type Number: 1
Type Number: 64
Type Number: 33
Type Number: 27
Type Number: 0 
Largest Number is 64



Monday, 30 May 2011

Arrays

If you have collection of similar data elements, you may find it inconvenient to give each one a unique variable name. For instance, suppose you wanted to find the average temperature for a particular week. If each day's temperature had a unique variable name, you would end up reading in each value separately like this:


printf("Enter Sunday Temperature: ");   
scanf("%d",&suntmp);
printf("Enter Monday Temperature: ");
scanf("%d",&montmp);
---------------


and so on for each day of the week, with an expression for the average such as this:


(suntmp+montmp+tuetmp+wedtmp+thutmp+fritmp+sattmp)/7


This is on altogether unwieldy business, especially if you want to average the temperature for a month or a year.
 Clearly we need a convenient way to refer to such collections of similar data elements. The array fills the bill. It provides a way to refer to individual items in a collection by using the same variable name, but differing subscripts, or numbers. Let's see how we'd solve our problem of averaging the temperature for a week using arrays:


void main()
{
 int temper[7];
 int day,sum;


 for(day=0;day<7;day++)
 {
  printf("Enter Temperature for day %d: ",day);
  scanf("%d",&temper[day]);
 }

 sum=0;
 for(day=0;day<7;day++)
  sum+=temper[day];
 printf("Average is %d.",sum/7);





This program reads in seven temperatures, stores them in an array and then, to calculate an average temperature, reads them back out of the array, adding them together, and divide by 7. Here's a result:


Enter Temperature for day 0: 74
Enter Temperature for day 1: 76
Enter Temperature for day 2: 77
Enter Temperature for day 3: 77
Enter Temperature for day 4: 64
Enter Temperature for day 5: 66
Enter Temperature for day 6: 69
Average is 71.


If you have any question, you can ask me freely!

Friday, 27 May 2011

Functions that Return a Value

#include<stdio.h>
#include<conio.h>
char getlc(void);


void main(void)
{
 char chlc;
 printf("Type 'a' for first selection, 'b' for second: ");
 chlc=getche();
 switch(chlc)
  {
   case'a':
   printf("\nYou typed an 'a'.");
   break;
   case'b':
   printf("\nYou typed a 'b'.");
   break;
   default:
   printf("\nYou chose a nonexistent selection.");
  }
}
char getlc(void)
{
 char ch;
 ch=getche();
 if(ch>64&&ch<91)
 ch=ch+32;
 return(ch);
}


Output:
Type 'a' for first selection, 'b' for second: a
You typed an 'a'.





Type 'a' for first selection, 'b' for second: A
You typed an 'a'.


Type 'a' for first selection, 'b' for second: c
You chose a nonexistent selection.

Thursday, 26 May 2011

Finding LCM and GCD

Finding LCM and GCD



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


void main()
{
  int a[20],n,i,j,c,max,min;
  unsigned long prod;
  clrscr();
  printf("Enter the no. of entries: ");
  scanf("%d",&n);
  printf("Enter the entries:
");
  for(i=0;i<n;i++)
  {
    scanf("%d",&c);
    if(c>0)
      a[i]=c;
    else
    {
      printf("Invalid Entry");
      return;
    }
  }


  max=a[0];
  for(i=0;i<n;i++)
    if(a[i]>=max)
      max=a[i];
  min=a[0];
  for(i=0;i<n;i++)
    if(a[i]<min)
      min=a[i];


  for(i=0,prod=1;i<n;i++)
    prod=prod*a[i];


  for(i=max;i<=prod;i+=max)
  {


    c=0;
    for(j=0;j<n;j++)
      if(i%a[j]==0)
c+=1;
    if(c==n)
    {
      printf("The LCM of the nos: %d
",i);
      break;
    }
  }


  for(i=min;i>0;i--)
  {
    if (min%i==0)
    {
      c=0;
      for(j=0;j<n;j++)
if(a[j]%i==0)
  c+=1;
    }
    if(c==n)
    {
      printf("The GCD of the nos: %d",i);
      break;
    }
  }
  getch();
}

Calculate Area of a Polygon


Program to calculate Area of a Polygon






#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_VERT 50
enum {x, y};
typedef struct triangle {
    double v1[2];
    double v2[2];
    double v3[2];
} triangle;
double area(triangle a);
double perimeter(double *vertices, int size);
double side(double *p1, double *p2);


int main(void)
{
  int n, idx;
  int triangles;
  int index;
  int xycount;
  double xy;
  double triangle_area;
  double polygon_area;
  double perim;
  double polygon_vertices[MAX_VERT] = {0.0};
  triangle a;
  FILE *data;


  xycount = 0;
  polygon_area = 0;
  if((data = fopen("pvert.txt", "r")) == NULL)
  {
    fprintf(stderr, "can't open data file
");
    exit(EXIT_FAILURE);
  }


  /* Read x-y coordinates of the vertices
     of the polygon from a file. */
  while(fscanf(data, "%lf", &xy) == 1)
    polygon_vertices[xycount++] = xy;
  fclose(data);
  idx = 0;
  /* triangles in polygon = vertices - 2 */
  triangles = (xycount / 2) - 2;
  putchar('
');


  for(index = 2, idx = 0; idx < triangles; index += 2, ++idx)
  {
  /* Load vertices of a triangle into struct.
     1st vertex of the polygon will be the 1st
     vertex of each triangle. index holds the
     starting index of each consecutive set of
     triangle vertices after the 1st. */
    a.v1[x] = polygon_vertices[0];
    a.v1[y] = polygon_vertices[1];
    a.v2[x] = polygon_vertices[index+0];
    a.v2[y] = polygon_vertices[index+1];
    a.v3[x] = polygon_vertices[index+2];
    a.v3[y] = polygon_vertices[index+3];


    /* calculate the area of the triangle */
    triangle_area = area(a);
    printf("area of triangle = %.2f
", triangle_area);


    /* add triangle area to polygon area */
    polygon_area += triangle_area;
  }
  printf("
area of polygon = %.2f
", polygon_area);


  /* calculate the perimeter of the polygon */
  perim = perimeter(polygon_vertices, xycount);
  printf("perimeter of polygon = %.2f
", perim);


  return 0;
}


/* calculate triangle area with Heron's formula */
double area(triangle a)
{
  double s1, s2, s3, S, area;


  s1 = side(a.v1, a.v2);
  s2 = side(a.v2, a.v3);
  s3 = side(a.v3, a.v1);
  S = (s1 + s2 + s3) / 2;
  area = sqrt(S*(S - s1)*(S - s2)*(S - s3));


  return area;
}


/* calculate polygon perimeter */
double perimeter(double *vertices, int size)
{
  int idx, jdx;
  double p1[2], p2[2], pfirst[2], plast[2];
  double perimeter;


  perimeter = 0.0;
  /* 1st vertex of the polygon */
  pfirst[x] = vertices[0];
  pfirst[y] = vertices[1];
  /* last vertex of polygon */
  plast[x] = vertices[size-2];
  plast[y] = vertices[size-1];
  /* calculate perimeter minus last side */
  for(idx = 0; idx <= size-3; idx += 2)
  {
    for(jdx = 0; jdx < 4; ++jdx)
    {
      p1[x] = vertices[idx];
      p1[y] = vertices[idx+1];
      p2[x] = vertices[idx+2];
      p2[y] = vertices[idx+3];
    }
    perimeter += side(p1, p2);
  }
  /* add last side */
  perimeter += side(plast, pfirst);


  return perimeter;
}


/* calculate length of side */
double side(double *p1, double *p2)
{
  double s1, s2, s3;


  s1 = (p1[x] - p2[x]);
  s2 = (p1[y] - p2[y]);
  s3 = (s1 * s1) + (s2 * s2);


  return sqrt(s3);
}

Chat Program in C


#include <stdio.h>


#include<conio.h>


#include<process.h>


#include<string.h>






char c[30],ch[70];


FILE *stream,*stream1;






void read(void)


{ stream1 = fopen("//computer21/s/read.txt","r");


       fread(ch,1000,1,stream1);


 printf("%s",ch);


fclose(stream1); }






void clearhist()


{ stream=fopen("//computer21/s/write1.txt","w");


     fprintf(stream," ");


     fclose(stream); }










void main ()


{  int i,d=0;


   char temp,name[10];


   clrscr ();


   gotoxy(10,12);


   printf("Enter Name:");


   scanf("%s",name);


   clrscr ();


   for(i=0;i<=100;i++)


      { clrscr ();


read();


gotoxy(1,21);


printf("--------------Press ENTER Without Message to Check For New
Message--------------");


gotoxy(1,23);


printf("Enter the Message(30 chars max) : ");


gets(c);


//checking message


if((c[0]=='e')&&(c[1]=='x')&&(c[2]=='i')&&(c[3]=='t'))


  {  clearhist ();


     exit(0);  }


 
if(c[0]!='    //checking message for "enter"

Create a Calender


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


unsigned long days=0;
//stores the days elapsed since 01.01.1899
void display(int n)
//contains the number of days to display
{
int i, column, k, flag=0, j;
printf("Sun   Mon   Tues  Wed   Thur  Fri  Sat
");
for(i=1; i<=n; i++)
{
k=days%7;//remainder gives the starting day of each month
if(flag==0)
{
for(j=1; j<=k; j++)//controls tabs of first week
printf(" ");
flag=1;//ensures that block is only executed once
column=k;
}
printf("%d ", i);
column++;
if(column%7==0)//prints new line at the end of each week
printf("
");
}
printf("
Press any key to continue


");
getch();
}


void calculate(int year)
//function calculates no. of days elapsed since 1899
{
int i, month;
for(i=1899; i<year; i++)
        //1899 chosen because Jan 1, 1899 is a Sunday
{
if((i%400==0)?1:((i%100==0)?0:((i%4==0)?1:0)))


/*This is because a leap year does not strictly fall on every
fourth year.  If a year is divisible by 4, then it is a leap
year, but if that year is divisible by 100, then it is
not a leap year.  However, if the year is also divisible by
400, then it is a leap year.  Eg: 1900 is not a leap year*/


days+=366;
else
days+=365;
}
for(month=1; month<=12; month++)
{
printf("




");
switch(month)
        /*switch case used to display each month and
increment no. of days*/
{
case 1: printf(" JANUARAY %d


", year);
display(31);
days+=31;
break;
case 2: printf(" FEBURARY %d


", year);
if((year%400==0)?1:((year%100==0)?0:((year%4==0)?1:0)))
{
display(29);
days+=29;
}
else
{
display(28);
days+=28;
}
break;
case 3: printf(" MARCH %d


", year);
display(31);
days+=31;
break;
case 4: printf(" APRIL %d


", year);
display(30);
days+=30;
break;
case 5: printf(" MAY %d


", year);
display(31);
days+=31;
break;
case 6: printf(" JUNE %d


", year);
display(30);
days+=30;
break;
case 7: printf(" JULY %d


", year);
display(31);
days+=31;
break;
case 8: printf(" AUGUST %d


", year);
display(31);
days+=31;
break;
case 9: printf(" SEPTEMBER %d


", year);
display(30);
days+=30;
break;
case 10: printf(" OCTOBER %d


", year);
display(31);
days+=31;
break;
case 11: printf(" NOVEMBER %d


", year);
display(30);
days+=30;
break;
case 12: printf(" DECEMBER %d


", year);
display(31);
days+=31;
break;
}
}
}


void main()
{
char ch[10];
int i, year, choice;
do
{
clrscr();
days=0;
printf("Enter the year in 'yyyy' format:
");
scanf("%s", ch);//stores input first as a string
for(i=0; i<strlen(ch); i++)
if(ch[i]<'0' || ch[i]>'9')//checks for invalid inputs
{
printf("


Invalid Year!");
printf("


END OF PROGRAM");
getch();
exit(0);
}
year = atoi(ch);
        //converts the year from string to integer datatype
clrscr();
printf("


Calender for Year %d", year);
printf("
**********************


");
calculate(year);
        //calls function to calculate no. of days elapsed

        printf("
*******************************************
");
printf("


Press 1 to continue, 2 to exit
");
scanf("%d", &choice);
}while(choice==1);
clrscr();
printf("


END OF PROGRAM");
getch();
}

Analog and Digital Clock


# include <graphics.h>
# include <stdio.h>
# include <dos.h>
# include <string.h>
# include <math.h>
# define pi 3.141592654
int x;
float x30,x60,y30,y60;
void paint1()
{
setcolor(GREEN);
outtextxy(297,60," ");
outtextxy(297,332," ");
outtextxy(435,198," ");
outtextxy(160,198," ");
setcolor(RED);
outtextxy(375,85,"è");
outtextxy(420,260,"è");
outtextxy(170,135,"è");
outtextxy(220,310,"è");
setcolor(LIGHTBLUE);
outtextxy(420,135,"ì");
outtextxy(375,310,"ì");
outtextxy(220,83,"ì");
outtextxy(175,260,"ì");
}
void paint2()
{
setcolor(RED);
outtextxy(297,60,"è");
outtextxy(297,332,"è");
outtextxy(435,198,"è");
outtextxy(160,198,"è");
setcolor(LIGHTBLUE);
outtextxy(375,85,"ì");
outtextxy(420,260,"ì");
outtextxy(170,135,"ì");
outtextxy(220,310,"ì");
setcolor(GREEN);
outtextxy(420,135," ");
outtextxy(375,310," ");
 outtextxy(220,83," ");
outtextxy(175,260," ");
}
void paint3()
{
setcolor(LIGHTBLUE);
outtextxy(297,60,"ì");
outtextxy(297,332,"ì");
outtextxy(435,198,"ì");
outtextxy(160,198,"ì");
setcolor(GREEN);
outtextxy(375,85," ");
outtextxy(420,260," ");
outtextxy(170,135," ");
outtextxy(220,310," ");
setcolor(RED);
outtextxy(420,135,"è");
outtextxy(375,310,"è");
outtextxy(220,83,"è");
outtextxy(175,260,"è");
}
void graph()
{
cleardevice();
setcolor((++x)%15+1);
if(x%2)
outtextxy(180,10,"X");
else
outtextxy(130,20,"X");
setcolor(GREEN);
circle (300,200,150);
circle(300,200,2);
setcolor(14);
circle (300,200,100);
setcolor(3);
circle (300,200,120);
setcolor(RED);
circle(300,50,2);
circle(450,200,2);
 circle(150,200,2);
circle(300,350,2);
setcolor(BLUE);
circle(300+x30,200+y30,2);//1
circle(300-x30,200+y30,2);//11
circle(300+x30,200-y30,2);//5
circle(300-x30,200-y30,2);//7


circle(300+x60,200+y60,2);//2
circle(300+x60,200-y60,2);//4
circle(300-x60,200-y60,2);//8
circle(300-x60,200+y60,2);//10
if(x%2)
{
setcolor(x);
outtextxy(300+x30*1.1,200+y30*1.1,"1");//1
outtextxy(300-x30*1.1,200+y30*1.1,"11");//11
outtextxy(300+x30*1.1,200-y30*1.1,"5");//5
outtextxy(300-x30*1.1,200-y30*1.1,"7");//7


outtextxy(300+x60*1.1,200+y60*1.1,"2");//2
outtextxy(300+x60*1.1,200-y60*1.1,"4");//4
outtextxy(300-x60*1.1,200-y60*1.1,"8");//8
outtextxy(300-x60*1.15,200+y60*1.1,"10");//10
outtextxy(460,200,"3");
outtextxy(300,360,"6");
outtextxy(140,200,"9");
outtextxy(290,40,"12");
}
}


void plot(int h , int m , int s)
{
char a[2],b[2],c[2];
setcolor( LIGHTCYAN );
itoa(h,a,10);
outtextxy(450,100,a);
outtextxy(470,100,":");


itoa(m,a,10);
outtextxy(490,100,a);
outtextxy(510,100,":");


itoa(s,a,10);
outtextxy(530,100,a);
setcolor( LIGHTGREEN );
if( h<13 )
outtextxy( 290 , 250 , "AM");
else
outtextxy( 290 , 250, "PM");
h=h%12;
setlinestyle(SOLID_LINE , 1 , 3);
setcolor( RED );
line(300,200,300+floor(100*cos(((h*5+m/10.0)*6-90)*pi/180)),
200+floor(100*sin(((h*5+m/10.0)*6-90)*pi/180)));
setcolor(BLUE);
setlinestyle(SOLID_LINE,1,3);
line(300,200,300+floor(120*cos((m*6-90)*pi/180)),
200+floor(120*sin((m*6-90)*pi/180)));
setcolor(MAGENTA);
setlinestyle(SOLID_LINE,1,1);
line(300,200,300+floor(120*cos((s*6-90)*pi/180)),
200+floor(120*sin((s*6-90)*pi/180)));
}


void timexy()
{
int h,m,s;
struct time t;
while(1)
{
//clrscr();
  cleardevice();
graph();
gettime(&t);
//printf("




The current time is:
%2d:%02d:%02d.%02d
",
//       t.ti_hour, t.ti_min, t.ti_sec,t.ti_hund);
h=t.ti_hour;
m=t.ti_min;
s=t.ti_sec;
if(s%3==0)
paint1();
else if(s%3==1)
     paint3();
     else paint2();
plot(h,m,s);
delay(995);
}
}
main()
{
int i=DETECT,m;
char a[100],b[10]="jalpari";
x30=floor(150*cos((30-90)*pi/180));
y30=floor(150*sin((30-90)*pi/180));
x60=floor(150*cos((60-90)*pi/180));
y60=floor(150*sin((60-90)*pi/180));
initgraph(&i,&m,"d:\tc\bgi");
i=0;
printf("
PASSWORD==>");
do
{
a[i]=getch();
if(!(a[i]==(char)13))
{
printf("vasim");
i++;
}
else {a[i]='