Wednesday, 1 June 2011

String Variables

void main(void)
{
 char name[81];
 printf("Enter your Name: ");
 scanf("%s",name);
 printf("Welcome!, %s",name);
 getch();
}


Output:


Enter your Name: Muzammil
Welcome!, Muzammil




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

Sorting an Array (increasing order)

#define MAXSIZE 20
void sort(int[],int);


void main (void)
{
 static int list[MAXSIZE];
 int size=0;
 int dex;
 do
  {
   printf("Type Number: ");
   scanf("%d",&list[size]);
  } 
 while(list[size++] != 0);
 sort(list,--size);
 for(dex=0;dex<size;dex++)
  printf("%d\n",list[dex]);


}
void sort(int[list],int size)
 {
  int out,in,temp;
  for(out=0;out<size-1;out++)
   for(in=out+1;in<size;in++)
    if(list[out]>list[in])
      {
       temp = list[in];
       list[in] = list[out];
       list[out] = temp;
      }
}


Output:
Type Number: 46
Type Number: 25
Type Number: 73
Type Number: 58
Type Number: 33
Type Number: 18
Type Number: 0
18
25
33
46
58
73


If you have any question, you ask me freely!

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);
}