Search This Blog

Thursday, October 13, 2011

Write a program to read my number x and to evaluate x*x-2. use a function to evaluate x*x-2 and call this function through its pointer

#include<stdio.h>
#include<conio.h>
#include<math.h>
float expr(float);
void main()
{
float x,value;
printf("Enter the value of x ");
scanf("%f",&x);
value=(*expr)(x);
printf("value =%f\n",value);
getch();
}
float expr(float x)
{
return(x*x-2);
}

The trace of a square is given by the sum of the diagonal elements. Write a program to evaluate the trace of a matrix. Read the elements of matrix from keyboard and pass on its address to a function to evaluate and return the traces

   1:   #include<stdio.h>
   2:   #include<conio.h>
   3:   float trace(int n,float **x);
   4:   void main()
   5:   {
   6:   int i,j;
   7:   float *xx[10];
   8:   float t,x[10][10],num;
   9:   int n;
  10:   printf("Enter the size of matrix:\n");
  11:   scanf("%d",&n);
  12:   printf("Enter the elements:\n");
  13:   for(i=0;i<n;i++)
  14:   {
  15:      for(j=0;j<n;j++)
  16:      {
  17:      scanf("%f",&num);
  18:      x[i][j]=num;
  19:      }
  20:   }
  21:   for(i=0;i<n;i++)
  22:   xx[i]=x[i];
  23:   t=trace(n,xx);
  24:   printf("trace of matrix is: %f",t);
  25:   getch();
  26:   }
  27:   
  28:   float trace(int n,float **x)
  29:   {
  30:   int i;
  31:   float t=0.0;
  32:   for(i=0;i<n;i++)
  33:   t=t+*(*(x+i)+i);
  34:   return(t);
  35:   }

Illustrate working of increment operator(++)

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  int a,b,x,y;
   6:  a=b=2;
   7:  x=a++;
   8:  y=++b;
   9:  printf("At this point \n");
  10:  printf("Value of x= %d\n",x);
  11:  printf("Value of y= %d\n",y);
  12:  getch();
  13:  }

Write a program to read elements of matrix and display them through pointers

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:  float x[10][10];
   6:  int i,j,nr,nc;
   7:  printf("Enter the num of rows:\n");
   8:  scanf("%d",&nr);
   9:  printf("Enter the num of columns:\n");
  10:  scanf("%d",&nc);
  11:  printf("Enter the elements of matrix:\n");
  12:  for(i=0;i<nr;i++)
  13:  {
  14:      for(j=0;j<nc;j++)
  15:      {
  16:          scanf("%f",&x[i][j]);
  17:      }
  18:  printf("\n");
  19:  }
  20:  printf("The matrix is:\n\n");
  21:  for(i=0;i<nr;i++)
  22:  {
  23:      for(j=0;j<nc;j++)
  24:      {
  25:          printf("%.2f ",*(*(x+i)+j));
  26:      }
  27:  printf("\n");
  28:  }
  29:  getch();
  30:  }

Write a program to sort an array of real Num using separate functions. Access array through pointers

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void readdata(int *size,float *addx)
   4:  {
   5:  int i;
   6:  float n;
   7:  char ch;
   8:  printf("Enter the elements of array:\n");
   9:  i=0;
  10:  do
  11:  {
  12:  scanf("%f%c",&n,&ch);
  13:  *(addx+i)=n;
  14:  i++;
  15:  }while(ch!='\n');
  16:  *size=i;
  17:  }
  18:   
  19:  void displayarray(int size,float *addx)
  20:  {
  21:  int i;
  22:  for(i=0;i<size;i++)
  23:  printf("%.2f ",*(addx+i));
  24:  printf("\n");
  25:  }
  26:   
  27:  void sort(int size,float *addx)
  28:  {
  29:  int i,j;
  30:  for(j=0;j<size-1;j++)
  31:  for(i=0;i<size-1;i++)
  32:  if(*(addx+i)>*(addx+i+1))
  33:      {
  34:      int p,q;
  35:      p=*(addx+i);
  36:      q=*(addx+i+1);
  37:      *(addx+i)=q;
  38:      *(addx+i+1)=p;
  39:      }
  40:  }
  41:   
  42:  void main()
  43:  {
  44:  int i,size;
  45:  float x[50];
  46:  readdata(&size,x);
  47:  printf("Array is:\n");
  48:  displayarray(size,x);
  49:  sort(size,x);
  50:  printf("sorted array is:\n");
  51:  displayarray(size,x);
  52:  getch();
  53:  }

Write a program to find the maximum element of an array of real num. Read the element of the array in function readdata() and find maximum in another function. Access the array elements through pointers.

   1:  #include<conio.h>
   2:  #include<stdio.h>
   3:  void readdata(int *size,float *addx)
   4:  {
   5:  int i;
   6:  float n;
   7:  char ch;
   8:  printf("\nEnter the elements of the array:\n");
   9:  i=0;
  10:  do
  11:  {
  12:  scanf("%f%c",&n,&ch);
  13:  *(addx+i)=n;
  14:  i++;
  15:  }while(ch!='\n');
  16:  *size=i;
  17:  }
  18:  float maxno(int size,float *addx)
  19:  {
  20:  int i;
  21:  float max=-1.0E38;
  22:  for(i=0;i<size;i++)
  23:      {
  24:      if((*addx+i)>max)
  25:      {
  26:      max=*(addx+i);
  27:      }
  28:      }
  29:  return(max);
  30:  }
  31:  void main()
  32:  {
  33:  int size;
  34:  float max;
  35:  int i;
  36:  float x[50];
  37:  readdata(&size,x);
  38:  max=maxno(size,x);
  39:  printf("\nThe array is: ");
  40:  for(i=0;i<size;i++)
  41:      printf("%.2f ",x[i]);
  42:  printf("\nThe maximum No is: %f",max);
  43:  getch();
  44:  }

write a program to read a set of real nums in one line of the keyboard and using the function maxno(), display the largest num entered.

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  float maxno(int size,float *addx);
   4:  void main()
   5:  {
   6:  int i,size;
   7:  float x[50],max;
   8:  char ch;
   9:  printf("\nEnter the elements of array:\n");
  10:  i=0;
  11:  do
  12:  {
  13:  scanf("%f%c",&x[i],&ch);
  14:  i++;
  15:  }while(ch!='\n');
  16:  size=i;
  17:  max=maxno(size,x);
  18:  printf("\nThe maximum No is: %.2f",max);
  19:  getch();
  20:  }
  21:  float maxno(int size,float *addx)
  22:  {
  23:  int i;
  24:  float max=-1.0E38;
  25:  for(i=0;i<size;i++)
  26:  if(*(addx+i)>max)
  27:  max=*(addx+i);
  28:  return(max);
  29:  }