WELCOME
 

 C-LAB PROGRAMES FOR JNTU Ist YEAR

  
   week 1

1)
/* A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent
terms are found by adding the preceding two terms in the sequence. Write a C program to generate
the first n terms of the sequence.
*/

#include <stdio.h>

void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();

printf("<===========PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES=========>");
printf("nnnttENTER LENGTH OF SERIES (N) : ");
scanf("%d",&no);

printf("nnttt<----FIBONACCI SERIES---->");
printf("nntt%d  %d",num1,num2);

//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf("  %d",fab);
num1=num2;
num2=fab;
}
getch();
}


2)/* Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. */

#include <stdio.h>

void main()
{
int no,counter,counter1,check;
clrscr();
printf("<-----------------------PRIME NO. SERIES------------------------>");
printf("nnntttINPUT THE VALUE OF N: ");
scanf("%d",&no);
printf("nnTHE PRIME NO. SERIES B/W 1 TO %d : nn",no);

for(counter = 1; counter <= no; counter++)
{
 check = 0;
 //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.

 for(counter1 = counter-1; counter1 > 1 ; counter1--)
   if(counter%counter1 == 0)
   {
    check++;        // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
    break;
   }
   if(check == 0)
   printf("%dt",counter);
}
getch();
}

3)/* Write a C program to find the sum of individual digits of a positive integer.*/

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

void main()
{
  int num, k=1, sum=0;
  clrscr();
  printf("Enter the number whose digits are to be added:");
  scanf("%d",&num);
while(num!=0)
{
  k=num%10;
  sum=sum+k;
  k=num/10;
  num=k;
}
printf("Sum of the digits:%d",sum);
getch();
}


week 2

1) /* Write a C program to calculate the following Sum:
 Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
*/

#include <stdio.h>
#include <math.h>

void main()
{
int counter,f_coun;
float sum=0,x,power,fact;
clrscr();

printf("<-----------------------PROGRAM FOR SUM OF EQ. SERIES----------------------->");
printf("nntEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!");

printf("nnntENTER VALUE OF X : ");
scanf("%f",&x);

for(counter=0, power=0; power<=10; counter++,power=power+2)
{
fact=1;
//CALC FACTORIAL OF POWER VALUE
for(f_coun=power; f_coun>=1; f_coun--)
        fact *= f_coun;
//EQ. FOR SUM SERIES
sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
}

printf("SUM : %f",sum);
getch();

}

2)/* Write a C program toe find the roots of a quadratic equation. */

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

void main()
{
float a,b,c,root1,root2;
clrscr();
printf("n Enter values of a,b,c for finding roots of a quadratic eq:n");
scanf("%f%f%f",&a,&b,&c);

/*checking condition*/
if(b*b>4*a*c)
{
 root1=-b+sqrt(b*b-4*a*c)/2*a;
 root2=-b-sqrt(b*b-4*a*c)/2*a;
 printf("n*****ROOTS ARE*****n");
 printf("n root1=%fn root2=%f",root1,root2);
}
else
 printf("n Imaginary Roots.");
 getch();
}


week 3

1) /* Write C programs that use both recursive and non-recursive functions
   To find the factorial of a given integer.*/

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

unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);

void main()
{
  int n,i;
  long fact;
  clrscr();
  printf("Enter the number: ");
  scanf("%d",&n);

  if(n==0)
    printf("Factorial of 0 is 1n");
  else
  {
    printf("Factorial of %d Using Recursive Function is %dn",n,recr_factorial(n));
    printf("Factorial of %d Using Non-Recursive Function is %dn",n,iter_factorial(n));
   }
   getch();
}

/* Recursive Function*/
unsigned int recr_factorial(int n) {
    return n>=1 ? n * recr_factorial(n-1) : 1;
}

/* Non-Recursive Function*/
unsigned int iter_factorial(int n) {
    int accu = 1;
    int i;
    for(i = 1; i <= n; i++) {
 accu *= i;
    }
    return accu;
}

2)/* Write C programs that use both recursive and non-recursive functions
   To find the GCD (greatest common divisor) of two given integers.*/

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

unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);

int main(void)
{
  int a,b,iGcd;
  clrscr();

  printf("Enter the two numbers whose GCD is to be found: ");
  scanf("%d%d",&a,&b);

  printf("GCD of %d and %d Using Recursive Function is %dn",a,b,GcdRecursive(a,b));
  printf("GCD of %d and %d Using Non-Recursive Function is %dn",a,b,GcdNonRecursive(a,b));

  getch();
}

/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
 if(n>m)
        return GcdRecursive(n,m);
 if(n==0)
         return m;
 else
     return GcdRecursive(n,m%n);
}

/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
 unsigned remainder;
 remainder = p-(p/q*q);

 if(remainder==0)
     return q;
 else
     GcdRecursive(q,remainder);
}

3)/* Write C programs that use both recursive and non-recursive functions
   To solve Towers of Hanoi problem.*/

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

/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
  char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
  int top,add;
  top=NULL;

  one:
 if(num==1)
 {
   printf("nMove top disk from needle %c to needle %c ",sndl,dndl);
   goto four;
 }
  two:
 top=top+1;
 stkn[top]=num;
 stksndl[top]=sndl;
 stkindl[top]=indl;
 stkdndl[top]=dndl;
 stkadd[top]=3;
 num=num-1;
 sndl=sndl;
 temp=indl;
 indl=dndl;
 dndl=temp;

 goto one;

  three:
 printf("nMove top disk from needle %c to needle %c ",sndl,dndl);
 top=top+1;
 stkn[top]=num;
 stksndl[top]=sndl;
 stkindl[top]=indl;
 stkdndl[top]=dndl;
 stkadd[top]=5;
 num=num-1;
 temp=sndl;
 sndl=indl;
 indl=temp;
 dndl=dndl;

 goto one;

  four:
 if(top==NULL)
   return;
 num=stkn[top];
 sndl=stksndl[top];
 indl=stkindl[top];
 dndl=stkdndl[top];
 add=stkadd[top];
 top=top-1;
 if(add==3)
   goto three;
 else if(add==5)
   goto four;
}

/* Recursive Function*/
void  hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)
{
    if ( num == 1 ) {
 printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
 return;
     }

     hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );
     printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
     hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}

void main()
{
  int no;
  clrscr();
  printf("Enter the no. of disks to be transferred: ");
  scanf("%d",&no);

  if(no<1)
     printf("nThere's nothing to move.");
  else
     printf("Non-Recursive");
     hanoiNonRecursion(no,'A','B','C');
     printf("nRecursive");
     hanoiRecursion(no,'A','B','C');

  getch();
}

 week 4

1) /* Write a C program, which takes two integer operands and one operator form the user,
   performs the operation and then prints the result.
   (Consider the operators +,-,*, /, % and use Switch Statement)
*/

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

void main()
{
int a,b,res,ch;
clrscr();
printf("t   *********************");
printf("ntMENUn");
printf("t********************");
printf("nt(1)ADDITION");
printf("nt(2)SUBTRACTION");
printf("nt(3)MULTIPLICATION");
printf("nt(4)DIVISION");
printf("nt(5)REMAINDER");
printf("nt(0)EXIT");
printf("nt********************");
printf("nntEnter your choice:");
scanf("%d",&ch);

if(ch<=5 & ch>0)
{
printf("Enter two numbers:n");
scanf("%d%d",&a,&b);
}

switch(ch)
{
 case 1:
 res=a+b;
 printf("n Addition:%d",res);
 break;

 case 2:
 res=a-b;
 printf("n Subtraction:%d",res);
 break;

 case 3:
 res=a*b;
 printf("n Multiplication:%d",res);
 break;

 case 4:
 res=a/b;
 printf("n Division:%d",res);
 break;

 case 5:
 res=a%b;
 printf("n Remainder:%d",res);
 break;

 case 0:
 printf("n Choice Terminated");
 exit();
 break;

 default:
 printf("n Invalid Choice");
}
getch();
}

2)/*   The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2
     where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
     Write C program to find the distance travelled at regular intervals of time given
     the values of 'u' and 'a'. The program should provide the flexibility to the user
     to select his own time intervals and repeat the calculations for different values of 'u' and 'a'.
*/

#include <stdio.h>
#include <math.h>

void main()
{
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========>");
printf("nnntttNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);

for(counter = 1; counter <= tim_intrval; counter++)
{
        printf("ntttAT T%d TIME(sec) : ",counter);
        scanf("%d",&time);
        printf("tttVELOCITY AT %d sec (m/sec) : ",time);
        scanf("%f",&velos);
        printf("tttACCLERATION AT %d sec (m/sec^2): ",time);
        scanf("%f",&accl);
        distance += (velos*time + (accl*pow(time,2))/2);
}

printf("nnntTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance);
getch();
}

3)/* Write a C program that uses functions to perform the following:
 i) Addition of Two Matrices
 ii) Multiplication of Two Matrices
*/

#include<stdio.h>

void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("nttMENU");
printf("n**********************************");
printf("n[1]ADDITION OF TWO MATRICES");
printf("n[2]MULTIPLICATION OF TWO MATRICES");
printf("n[0]EXIT");
printf("n**********************************");
printf("ntEnter your choice:n");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
 printf("Valid Choicen");
}

switch(ch)
{
  case 1:
  printf("Input rows and columns of A & B Matrix:");
  scanf("%d%d",&r1,&c1);
  printf("Enter elements of matrix A:n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      scanf("%d",&a[i][j]);
    }
  printf("Enter elements of matrix B:n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      scanf("%d",&b[i][j]);
    }
  printf("n =====Matrix Addition=====n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      printf("%5d",a[i][j]+b[i][j]);
      printf("n");
    }
  break;

  case 2:
  printf("Input rows and columns of A matrix:");
  scanf("%d%d",&m,&n);
  printf("Input rows and columns of B matrix:");
  scanf("%d%d",&p,&q);
  if(n==p)
  {
  printf("matrices can be multipliedn");
  printf("resultant matrix is %d*%dn",m,q);
  printf("Input A matrixn");
  read_matrix(a,m,n);
  printf("Input B matrixn");
  /*Function call to read the matrix*/
  read_matrix(b,p,q);
  /*Function for Multiplication of two matrices*/
  printf("n =====Matrix Multiplication=====n");
  for(i=0;i<m;++i)
    for(j=0;j<q;++j)
    {
      c[i][j]=0;
      for(k=0;k<n;++k)
 c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }

  printf("Resultant of two matrices:n");
     write_matrix(c,m,q);
    }
    /*end if*/
  else
  {
  printf("Matrices cannot be multiplied.");
  }
  /*end else*/
  break;

  case 0:
  printf("n Choice Terminated");
  exit();
  break;

  default:
  printf("n Invalid Choice");
}
getch();
}

/*Function read matrix*/
int read_matrix(int a[10][10],int m,int n)
  {
    int i,j;
      for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 scanf("%d",&a[i][j]);
  return 0;
  }

  /*Function to write the matrix*/
int write_matrix(int a[10][10],int m,int n)
  {
    int i,j;
      for(i=0;i<m;i++)
      {
 for(j=0;j<n;j++)
 printf("%5d",a[i][j]);
 printf("n");
      }
   return 0;
   }


week 5

1)/* Write a C program to find both the largest  and smallest number in a list of integers*/


main( )
{
  float largest(float a[ ], int n);
  float value[4] = {2.5,-4.75,1.2,3.67};
  printf("%fn", largest(value,4));
}
float largest(float a[], int n)
{
  int i;
  float max;
  max = a[0];
  for(i = 1; i < n; i++)
    if(max < a[i])
    max = a[i];
  return(max);
}

2)/* Write a C program that uses functions to perform the following:
 i) Addition of Two Matrices
 ii) Multiplication of Two Matrices
*/

#include<stdio.h>

void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("nttMENU");
printf("n**********************************");
printf("n[1]ADDITION OF TWO MATRICES");
printf("n[2]MULTIPLICATION OF TWO MATRICES");
printf("n[0]EXIT");
printf("n**********************************");
printf("ntEnter your choice:n");
scanf("%d",&ch);

if(ch<=2 & ch>0)
{
 printf("Valid Choicen");
}

switch(ch)
{
  case 1:
  printf("Input rows and columns of A & B Matrix:");
  scanf("%d%d",&r1,&c1);
  printf("Enter elements of matrix A:n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      scanf("%d",&a[i][j]);
    }
  printf("Enter elements of matrix B:n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      scanf("%d",&b[i][j]);
    }
  printf("n =====Matrix Addition=====n");
    for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
      printf("%5d",a[i][j]+b[i][j]);
      printf("n");
    }
  break;

  case 2:
  printf("Input rows and columns of A matrix:");
  scanf("%d%d",&m,&n);
  printf("Input rows and columns of B matrix:");
  scanf("%d%d",&p,&q);
  if(n==p)
  {
  printf("matrices can be multipliedn");
  printf("resultant matrix is %d*%dn",m,q);
  printf("Input A matrixn");
  read_matrix(a,m,n);
  printf("Input B matrixn");
  /*Function call to read the matrix*/
  read_matrix(b,p,q);
  /*Function for Multiplication of two matrices*/
  printf("n =====Matrix Multiplication=====n");
  for(i=0;i<m;++i)
    for(j=0;j<q;++j)
    {
      c[i][j]=0;
      for(k=0;k<n;++k)
 c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }

  printf("Resultant of two matrices:n");
     write_matrix(c,m,q);
    }
    /*end if*/
  else
  {
  printf("Matrices cannot be multiplied.");
  }
  /*end else*/
  break;

  case 0:
  printf("n Choice Terminated");
  exit();
  break;

  default:
  printf("n Invalid Choice");
}
getch();
}

/*Function read matrix*/
int read_matrix(int a[10][10],int m,int n)
  {
    int i,j;
      for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 scanf("%d",&a[i][j]);
  return 0;
  }

  /*Function to write the matrix*/
int write_matrix(int a[10][10],int m,int n)
  {
    int i,j;
      for(i=0;i<m;i++)
      {
 for(j=0;j<n;j++)
 printf("%5d",a[i][j]);
 printf("n");
      }
   return 0;
   }

week 6

1)/* Write a C program that uses functions to perform the following operations:
   To insert a sub-string in to given main string from a given position.
*/

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

void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();

puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;

// Copying the input string into another array
while(i <= r)
{
 c[i]=a[i];
 i++;
}
s = n+r;
o = p+n;

// Adding the sub-string
for(i=p;i<s;i++)
{
 x = c[i];
 if(t<n)
 {
  a[i] = b[t];
  t=t+1;
 }
 a[o]=x;
 o=o+1;
}

printf("%s", a);
getch();
}

2)/* Write a C program to determine if the given string is a palindrome or not */

#include<stdio.h>
#include<string.h>

enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
 int left,right,len=strlen(string);
 enum Boolean matched=true;
 if(len==0)
  return 0;
  left=0;
  right=len-1;

  /* Compare the first and last letter,second & second last & so on */
  while(left<right&&matched)
  {
   if(string[left]!=string[right])
   matched=false;
   else
   {
    left++;
    right--;
   }
  }
  return matched;
 }

int main()
{
  char string[40];
  clrscr();
  printf("****Program to test if the given string is a palindrome****n");
  printf("Enter a string:");
  scanf("%s",string);
  if(IsPalindrome(string))
  printf("The given string %s is a palindromen",string);
  else
  printf("The given string %s is not a palindromen",string);
  getch();
  return 0;
}

3)/* Write a C program that uses functions to perform the following operations:
   To delete n Characters from a given position in a given string.
*/

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

void delchar(char *x,int a, int b);

void main()
{
     char string[10];
     int n,pos,p;
     clrscr();

     puts("Enter the string");
     gets(string);
     printf("Enter the position from where to delete");
     scanf("%d",&pos);
     printf("Enter the number of characters to be deleted");
     scanf("%d",&n);
     delchar(string, n,pos);
     getch();
}

// Function to delete n characters
void delchar(char *x,int a, int b)
{
  if ((a+b-1) <= strlen(x))
  {
    strcpy(&x[b-1],&x[a+b-1]);
    puts(x);
    }
}

week 7

1)/* Write a C program to count the lines, words and characters in a given text*/

Program
#include <stdio.h>
main()
{
 char line[81], ctr;
 int i,c,
   end = 0,
   characters = 0,
   words = 0,
   lines = 0;
 printf("KEY IN THE TEXT.n");
 printf("GIVE ONE SPACE AFTER EACH WORD.n");
 printf("WHEN COMPLETED, PRESS 'RETURN'.nn");
 while( end == 0)
 {
  /* Reading a line of text */
  c = 0;
  while((ctr=getchar()) != 'n')
   line[c++] = ctr;
  line[c] = '

week 11

/* Write a C program that uses functions to perform the following operations:
 i) Reading a complex number
 ii) Writing a complex number
 iii) Addition of two complex numbers
 iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.) */

#include<stdio.h>
#include<math.h>

void arithmetic(int opern);

struct comp
{
     double realpart;
     double imgpart;
};

void main()
{
     int opern;
     clrscr();
     printf("nn ttt***** MAIN MENU *****");
     printf("nn Select your option: n 1 : ADDn 2 : MULTIPLYn 0 : EXIT nntt Enter your Option [  ]bb");

     scanf("%d",&opern);

     switch(opern)
     {
   case 0:
        exit(0);
   case 1:
   case 2:
        arithmetic(opern);
   default:
        main();
     }

}

void arithmetic(int opern)
{

     struct comp w1, w2, w;

     printf("n Enter two Complex Numbers  (x+iy):n Real Part of First Number:");
     scanf("%lf",&w1.realpart);
     printf("n Imaginary Part of First Number:");
     scanf("%lf",&w1.imgpart);
     printf("n Real Part of Second Number:");
     scanf("%lf",&w2.realpart);
     printf("n Imaginary Part of Second Number:");
     scanf("%lf",&w2.imgpart);
     switch(opern)
     {

     /*addition of complex number*/
     case 1:
   w.realpart = w1.realpart+w2.realpart;
   w.imgpart = w1.imgpart+w2.imgpart;
   break;

     /*multiplication of complex number*/
     case 2:
   w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
   w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart);
   break;
     }


     if (w.imgpart>0)
   printf("n Answer = %lf+%lfi",w.realpart,w.imgpart);
     else
   printf("n Answer = %lf%lfi",w.realpart,w.imgpart);
     getch();
     main();
}

week 12

1)/* Write a C program which copies one file to another.*/

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

void main(int argc, char *argv[])
{
 FILE *fs,*ft;
 char ch;
 clrscr();
 if(argc!=3)
 {
  puts("Invalid number of arguments.");
  exit(0);
  }
 fs = fopen(argv[1],"r");
 if(fs==NULL)
 {
 puts("Source file cannot be opened.");
 exit(0);
 }
 ft = fopen(argv[2],"w");
 if (ft==NULL)
 {
 puts("Target file cannot be opened.");
 fclose(fs);
 exit(0);
 }
 while(1)
 {
  ch=fgetc(fs);
  if (ch==EOF)
  break;
  else
  fputc(ch,ft);
  }
  fclose(fs);
  fclose(ft);
  getch();
}

2)/* Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)*/

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

void main(int argc, char *argv[])
{
  char a[15];
  char s[20];
  char n;
  int k;
  int j=0;
  int i;
  int len;
  FILE *fp;

  if(argc!=3)
  {
  puts("Improper number of arguments.");
  exit(0);
  }
  fp = fopen(argv[1],"r");
  if(fp == NULL)
  {
  puts("File cannot be opened.");
  exit(0);
  }

  k=*argv[2]-48;
  n = fread(a,1,k,fp);
  a[n]='

week 16

1)/* Write C programs that implement Queue (its operations) using i) Arrays */

#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#define size 10
#define true 1
#define false 0

struct q_arr
{
  int f,r;
  int num;
  int a[size];
};

void init(struct q_arr* queue);
int e_que(struct q_arr* queue);
int f_que(struct q_arr* queue);
int add_ele(struct q_arr* queue,int);
int rem_ele(struct q_arr* queue);
void display_ele(struct q_arr* queue);

/*main function*/
void main()
{
  int ele,k;
  int ch;

  struct q_arr *queue = (struct q_arr*)malloc(sizeof(struct q_arr));
  init(queue);

  while(1)
  {
    clrscr();
    printf("nn****IMPLEMENTATION OF QUEUE USING ARRAYS****n");
    printf("============================================");
    printf("nttMENUn");
    printf("============================================");
    printf("nt[1] To insert an element");
    printf("nt[2] To remove an element");
    printf("nt[3] To display all the elements");
    printf("nt[4] Exit");
    printf("nnt Enter your choice: ");
    scanf("%d",&ch);

    switch(ch)
    {
      case 1:
      {
 printf("nElement to be inserted:");
 scanf("%d",&ele);
 add_ele(queue,ele);
 break;
      }

      case 2:
      {
 if(!e_que(queue))
 {
   k=rem_ele(queue);
   printf("n%d element is removedn",k);
   getch();
 }
 else
 {
   printf("tQueue is Empty. No element can be removed.");
   getch();
 }
 break;
      }

      case 3:
      {
 display_ele(queue);
 getch();
 break;
      }

      case 4:
 exit(0);

      default:
 printf("tInvalid Choice.");
 getch();
 break;
    }
  }
}
/*end main*/

void init(struct q_arr* queue)
{
  queue->f = 0;
  queue->r = -1;
  queue->num = 0;
}

/* Function to check is the queue is empty*/
int e_que(struct q_arr* queue)
{
  if(queue->num==0)
  return true;
  return false;
}

/* Function to check if the queue is full*/
int f_que(struct q_arr* queue)
{
  if(queue->num == size)
  return true;
  return false;
}

/* Function to add an element to the queue*/
int add_ele(struct q_arr* queue,int j)
{
  if(f_que(queue))
  return false;

  if(queue->r == size - 1)
  queue->r = -1;
  queue->a[++queue->r] = j;
  queue->num++;
  return true;
}

/* Function to remove an element of the queue*/
int rem_ele(struct q_arr* queue)
{
  int j;
  if(e_que(queue))
  return -9999;
  j = queue->a[queue->f++];
  if(queue->f == size)
  queue->f = 0;
  queue->num--;
  return j;
}

/* Function to display the queue*/
void display_ele(struct q_arr* queue)
{
  int j;
  if(e_que(queue))
  {
    printf("Queue is Empty. No records to display.");
    return;
  }
  printf("nElements present in the Queue are: ");
  for(j=queue->f;j<=queue->r;j++)
    printf("%dt",queue->a[j]);
    printf("n");
}


2)/* Write C programs that implement Queue (its operations) using ii) Pointers */

#define true 1
#define false 0

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

struct q_point
{
  int ele;
  struct q_point* n;
};

struct q_point *f_ptr = NULL;

int e_que(void);
void add_ele(int);
int rem_ele(void);
void show_ele();

/*main function*/
void main()
{
  int ele,choice,j;
  while(1)
  {
    clrscr();
    printf("nn****IMPLEMENTATION OF QUEUE USING POINTERS****n");
    printf("==============================================");
    printf("ntt  MENUn");
    printf("==============================================");
    printf("nt[1] To insert an element");
    printf("nt[2] To remove an element");
    printf("nt[3] To display all the elements");
    printf("nt[4] Exit");
    printf("nntEnter your choice:");
    scanf("%d", &choice);

    switch(choice)
    {
      case 1:
      {
 printf("ntElement to be inserted:");
 scanf("%d",&ele);
 add_ele(ele);
 getch();
 break;
      }

      case 2:
      {
 if(!e_que())
 {
   j=rem_ele();
   printf("nt%d is removed from the queue",j);
   getch();
 }
 else
 {
   printf("ntQueue is Empty.");
   getch();
 }
 break;
      }

      case 3:
 show_ele();
 getch();
 break;

      case 4:
 exit(1);
 break;

      default:
 printf("ntInvalid choice.");
 getch();
 break;
    }

  }
}

/* Function to check if the queue is empty*/
int e_que(void)
{
  if(f_ptr==NULL)
  return true;
  return false;
}

/* Function to add an element to the queue*/
void add_ele(int ele)
{
  struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point));
  queue->ele = ele;
  queue->n = NULL;
  if(f_ptr==NULL)
    f_ptr = queue;
  else
  {
    struct q_point* ptr;
    ptr = f_ptr;
    for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);
      ptr->n = queue;
  }
}

/* Function to remove an element from the queue*/
int rem_ele()
{
  struct q_point* queue=NULL;
  if(e_que()==false)
  {
    int j = f_ptr->ele;
    queue=f_ptr;
    f_ptr = f_ptr->n;
    free (queue);
    return j;
  }
  else
  {
    printf("ntQueue is empty.");
    return -9999;
  }
}

/* Function to display the queue*/
void show_ele()
{
  struct q_point *ptr=NULL;
  ptr=f_ptr;
  if(e_que())
  {
    printf("ntQUEUE is Empty.");
    return;
  }
  else
  {
    printf("ntElements present in Queue are:nt");
    while(ptr!=NULL)
    {
      printf("%dt",ptr->ele);
      ptr=ptr->n;
    }
  }
}

week 17

1)/* Write a C program that uses Stack operations to perform the following:
 i) Converting infix expression into postfix expression
 ii) Evaluating the postfix expression */

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

int st[100];
int st_top=-1;

int cal(char post[]);
void in_post(char in[]);
void push_item(int it);
int pop_item();
int st_ISP(char t);
int st_ICP(char t);

/*main function*/
void main()
{
  char in[100],post[100];
  clrscr();
  printf("ntEnter the Infix Expression: ");
  gets(in);
  in_post(in);
  getch();
}
/*end main*/

void push_item(int it)
{
  if(st_top==99)
  {
    printf("nnt*STACK is Full*");
    getch();
    exit(1);
  }
  st[++st_top]=it;
}

int pop_item()
{
  int it;
  if(st_top==-1)
  {
    getch();
  }
  return(st[st_top--]);
}

/*Function for converting an infix expression to a postfix expression. */
void in_post(char in[])
{
  int x=0,y=0,z,result=0;
  char a,c, post[100];
  char t;
  push_item('

week 21

1)/* Write C program that implement the following sorting methods
to sort a given list of integers in ascending order: i) Insertion sort */

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

void inst_sort(int []);

void main()
{
  int num[5],count;
   clrscr();
   printf("nEnter the Five Elements to sort:n");

  for (count=0;count<5;count++)
    scanf("%d",&num[count]);
    inst_sort(num);

  printf("nnElements after sorting: n");
  for(count=0;count<5;count++)
    printf("%dn",num[count]);
    getch();
}

// Function for Insertion Sorting
void inst_sort(int num[])
{
 int i,j,k;
 for(j=1;j<5;j++)
  {
    k=num[j];
     for(i=j-1;i>=0 && k<num[i];i--)
       num[i+1]=num[i];
       num[i+1]=k;
   }
}

2)/* Write C program that implement the following sorting methods to sort a given list of integers in ascending order:
ii) Merge sort */

#include <stdio.h>
#include <stdlib.h>

#define MAX_ARY 10

void merge_sort(int x[], int end, int start);

int main(void) {
 int ary[MAX_ARY];
 int j = 0;

 printf("nnEnter the elements to be sorted: n");
   for(j=0;j<MAX_ARY;j++)
       scanf("%d",&ary[j]);

 /* array before mergesort */
 printf("Before    :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("n");

 merge_sort(ary, 0, MAX_ARY - 1);

 /* array after mergesort */
 printf("After Merge Sort :");
 for(j = 0; j < MAX_ARY; j++)
  printf(" %d", ary[j]);

 printf("n");
 getch();
}

/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
 int j = 0;
 const int size = start - end + 1;
 int mid  = 0;
 int mrg1 = 0;
 int mrg2 = 0;
 int executing[MAX_ARY];

 if(end == start)
  return;

 mid  = (end + start) / 2;

 merge_sort(x, end, mid);
 merge_sort(x, mid + 1, start);

 for(j = 0; j < size; j++)
  executing[j] = x[end + j];

 mrg1 = 0;
 mrg2 = mid - end + 1;

 for(j = 0; j < size; j++) {
  if(mrg2 <= start - end)
   if(mrg1 <= mid - end)
    if(executing[mrg1] > executing[mrg2])
     x[j + end] = executing[mrg2++];
    else
     x[j + end] = executing[mrg1++];
   else
    x[j + end] = executing[mrg2++];
  else
   x[j + end] = executing[mrg1++];
 }
}

week 22

1)/* Write C program to implement the Lagrange interpolation.*/

#include<stdio.h>
#include<conio.h>
#define MaxN 90

void main()
{
 float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
 int i, j, n;
 clrscr();
 printf("Enter the value of n: n");
 scanf("%d", &n);
 printf("Enter the values of x and y: n");
 for(i=0; i<=n; i++)
 scanf("%f%f", &arr_x[i], &arr_y[i]);
 printf("Enter the value of x at which value of y is to be calculated: ");
 scanf("%f", &x);
 for (i=0; i<=n; i++)
 {
  numerator=1;
  denominator=1;
  for (j=0; j<=n; j++)
  if(j!=i)
  {
   numerator *= x-arr_x[j];
   denominator *= arr_x[i]-arr_x[j];
  }
  y+=(numerator/denominator)*arr_y[i];
 }
printf("When x=%4.1f y=%7.1fn",x,y);
getch();
}

2)/* Write C program to implement the Newton- Gregory forward interpolation.*/

#include<stdio.h>
#include<conio.h>
#define MaxN 100
#define Order_of_diff 4

void main ()
{
 float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1];
 int i,j,n,k;
 clrscr();

 printf("Enter the value of n n");
 scanf("%d",&n);
 printf("Enter the values of x and y");

 for(i=0; i<=n; i++)
 scanf("%f%f", &arr_x[i], &arr_y[i]);
 printf("Enter the value of x at which value of y is to be calculated");
 scanf("%f", &x);
 h=arr_x[1]-arr_x[0];

 for(i=0; i<=n-1; i++)
 diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/
 for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/
 for(i=0; i<=n-j; i++)
 diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1];
 i=0;

 while(!(arr_x[i]>x)) /* Finding x0 */
 i++;
 i--;
 p=(x-arr_x[i])/h;
 y=arr_y[i];

 for (k=1; k<=Order_of_diff; k++)
 {
  numerator *=p-k+1;
  denominator *=k;
  y +=(numerator/denominator)*diff_table[i][k];
 }
printf("When x=%6.1f, y=%6.2fn",x, y);
getch();
}

week 23

/* Write C program to implement the linear regression algorithm. */

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

float mean(float *a, int n);
void deviation(float *a, float mean, int n, float *d, float *S);

void main()
{
   float a[20],b[20],dx[20],dy[20];
   float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0;
   float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0;
   char type_coff[7];
   int n=0,i=0;

   clrscr();

   printf("Enter the value of n: ");
   scanf("%d",&n);
   printf("Enter the values of x and y:n");
   for(i=0;i<n;i++)
      scanf("%f%f",&a[i],&b[i]);
   mean_x=mean(a,n);
   mean_y=mean(b,n);
   deviation(a,mean_x,n,dx,&sx);
   deviation(b,mean_y,n,dy,&sy);
  
   for(i=0;i<n;i++)
     sum_xy=sum_xy+dx[i]*dy[i];
   corr_coff=sum_xy/(n*sx*sy);
   printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");
   fflush(stdin);
   gets(type_coff);
  
   if(strcmp(type_coff,"x on y")==1)
   {
 reg_coff_xy=corr_coff*(sx/sy);
 printf("nThe value of linear regression coefficient is %f",reg_coff_xy);
   }
   else if(strcmp(type_coff,"y on x")==1)
   {
 reg_coff_yx=corr_coff*(sy/sx);
 printf("nThe value of linear regression coefficient is %f",reg_coff_yx);
   }
   else
      printf("nEnter the correct type of regression coefficient.");
   getch();
}
float mean(float *a, int n)
{
   float sum=0, i=0;
   for(i=0;i<n;i++)
      sum=sum+a[i];
   sum=sum/n;
   return (sum);
}

void deviation(float *a, float mean, int n, float *d, float *s)
{
   float sum=0,t=0;
   int i=0;
   for(i=0;i<n;i++)
   {
      d[i]=a[i]-mean;
      t=d[i]*d[i];
      sum=sum+t;
   }
   sum=sum/n;
   *s=sqrt(sum);
}

Think Quest :
This is an exciting opportunity for all of us. Share innovative ideas for development of technology in INDIA.
Quote of the week:

No matter what looms ahead, if you can eat today, enjoy today, mix good cheer with friends today enjoy it and bless God for it. - Henry Ward Beecher


Articles:

you are welcome to submit your articles on any useful topic to eceinace@gmail.com
 
Today, there have been 12 visitors (15 hits) on this page!
thank you for visiting this site. phani-ece batch This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free