GE 2155 Computer practice laboratory II - C PROGRAM IN UNIX OS

                      III    C   PROGRAM   IN   UNIX  OS
                  
                   DYNAMIC MEMORY ALLOCATION

Algorithm:
 Step 1 :  Start
Step 2 :  allocate 5 bytes to buffer using malloc( )
Step 3:   malloc( ) function allocated to character pointer p.
Step 4:   copy data to allocated buffer.
Step 5:   print the data
Step 6:   reallocate buffer to increase the size from 5 bytes to 25 bytes.
Step 7:   move  data to the variable.
Step 8:   Now print new data  that memory contains.
Step 9:   Stop

Program:

#include<stdio.h>
#include<ctype.h>
int main()
{
char *p;
p=(char *)malloc(5);
strcpy(p,"CSIIT");
printf("memory contains %s\n",p);
p=(char*)realloc(p,25);
strcpy(p,"CSIINSTITUTE OF TECHNOLOGY");
printf("memory now contains%s\n",p);
free(p);
return(0);
}


Output:

Memory contains       :   CSIIT
Memory now contains:  CSI INSTITUTE OF TECHNOLOGY

        INTERCHANGE TWO NUMBERS USING POINTERS

Algorithm:

Step 1:  Start
Step 2:  Read the values of a and b in main fumction
Step 3:  call the function interchange
Step 4:  print the  exchange  values  of a and b
Stop 5:  Stop

Program:

#include<stdio.h>
#include<ctype.h>
#include<fcntl.h>
void interchange(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
main()
{
int a,b;
printf("enter the values of a and b");
scanf("%d%d",&a,&b);
printf("a=%d b=%d\n",a,b);
interchange(&a,&b);
printf("after interchange\n");
printf("a=%d  b=%d\n",a,b);
}

Input:
Enter the values of a and b
8

Output:
a=5       b=8
after interchange
a=8      b=5

        ADDITION   OF    TWO   NUMBERS   USING   FUNCTION

Algorithm:

Main program
Step 1:  Start
Step 2:  assign function name add( )  to c.
Step 3:  Print the value of c
Step 4:  Stop

Subprogram

Step 1:   Read input values of  a and b  in subprogram
Step 2:   calculate c = a+b
Step 3:   Return the value  to main function.

Program:

#include<stdio.h>
main()
{
int c;
c=add();
printf("Result is=  %d\n",c);
}
int add()
{
int a,b,c;
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
}

Input:
Enter 2 numbers
10

Output:

Result is  30


                              FACTORIAL USING RECURSION
Algorithm:

Main program
Step 1:  Start
Step 2:  read input value n
Step 3:  call and print function fact(n)
Step 4:  stop

Sub program
Step 1:  function fact start
Step 2:  Is n=0 return 1 else goto step3
Step 3:  Return  (n*fact(n-1))

Program:

#include<stdio.h>
#include<ctype.h>
long fact(long num);
main()
{
long num,fac;
printf("enter the number\n");
scanf("%d",&num);
fac=fact(num);
printf("the factorial value is %d\n",fac);
}
long fact(long x)
{
if(x<=1)
return(1);
else
return(x*fact(x-1));
}

 Input:
Enter the number    5

Output:
The factorial value is  120

         BIGGEST AND  SMALLEST  NUMBER   IN  AN  ARRAY

Algorithm:-

Step 1:  Start
Step 2:  Read Input value to n
Step 3:  Repeat for i=0 to n
Step 4:  read input values to array a[i]
Step 5:  Increment I  by 1
Step 6:  End of step 3
Step 7:  Assign large=a[0], small=a[0]
Step 8: Repeat for i=1 to n
Step 9: Check  whether the value of a[i] is greater than large
Step 10: If step 9 is true, assign large=a[i].
Step 11: If step 9 is false , check the value of a[i] is less than small
Step 12: If step 11 is true , assign small =a[i]
Step 13: Increment I by 1
Step 14: End of step 8
Step 15: Print the values of large, small
Step 16: Stop.

Step 2:  Read the elements into the array.
Step 3:  Check a[i], if it is greater than the variable large,
Then assign the  value to a[i]. That is the biggest value.
If the array a[i] smaller than the variable small, then
Assign the value to a[i]. That is the smallest value.

 Program:-

#include<stdio.h>
main()
{
int a[100],i,small,large,no;
printf("Enter the number of elements in the array");
scanf("%d",&no);
printf("\nEnter the elements of the array");
for(i=0;i<no;i++)
scanf("%d",&a[i]);
printf("\n\nThe elements of the array are..");
for(i=0;i<no;i++)
printf("\t%d",a[i]);
small=a[0];
large=a[0];
for(i=1;i<no;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
printf("\n\nThe largest of the given array is %d",large);
printf("\n\nThe smallest of the given array is %d",small);
}
Input:-
Enter the number of elements in the array    5
Enter the elements of the array     45    78     34     89      90

The largest of the given array is    90
The smallest of the given array is   34

      ARRANGE NUMBERS  IN  ASCENDING   AND   DESCENDING  ORDER

Algorithm:-

Step 1: Start
Step 2: Read input value n.
Step 3: Setup loop i from 0 to n, Read a[i]
Step 4: Setup loop i from 0 to n-1, setup loop j from i+1 to n
Step 5: Check up the value of a[i] is greater than a[j]
Step 6: If step 5 is true , swap a[i] and a[j]
Step 7: Setup loop I from 0 to n
Step 8: Print a[i]
Step 9: Stop

Program:-
#include<stdio.h>
main()
{
int num[100],no,i,j,a;
printf("Enter the upper limit");
scanf("%d",&no);
printf("\n\nEnter the numbers");
for(i=0;i<no-1;i++)
scanf("%d",&num[i]);
for(i=0;i<no-1;i++)
{
for(j=i+1;j<no;j++)
{
if(num[i]>num[j])
{
a=num[i];
num[i]=num[j];
num[j]=a;
}
}
}
printf("\n\nTHE ASCENDING ORDER OF THE GIVEN NUMBERS IS");
for(i=0;i<no;i++)
printf("\t%d",num[i]);
printf("\n\nTHE DESCENDING ORDER OF THE GIVEN NUMBERS IS");
for(j=no-1;j>=0;j--)
printf("\t%d",num[j]);
}

Input:-
Enter the upper limit   5
Enter the numbers   5   2   7   1   9

Output:-

 THE ASCENDING ORDER OF THE GIVEN NUMBERS IS
1 2 5 7 9

THE DESCENDING ORDER OF THE GIVEN NUMBERS IS

9 7 5 2 1

                                     FILE DISPLAY

Algorithm:

Step 1:  Start
Step 2:  Open the file in read mode.
Step 3:  Read the character using getc from the file.
Step 4:  Assign  the characters to c.
Step 5:  Display  the character using putchar.
Step 6:  Close the file.
Step 7:  Stop
Program:

#include<stdio.h>
main()
{
FILE *fopen(), *fp;
int c;
fp=fopen("fib","r");
c=getc(fp);
while(c!=EOF)
{
putchar(c);
c=getc(fp);
}
fclose(fp);
}
Output:
echo enter the number
read n
a=0
b=1
echo "the fibonacci series "
echo "$a"
echo "$b"
while [ $n -gt 2 ]
do
c=`expr $a + $b`
a=$b
b=$c
echo "$c"
n=`expr $n - 1`
done

                                               FILE COPY
Algorithm:

Step 1:  Start
Step 2:  create two separate text files (ben.txt  and  ken.txt)
Step 3:  Type information in first file and second file is empty.
Step 4:  Open both the files in read write mode.
Step 5:  Set the file pointer at starting position.
Step 6:  read the content from buffer and assign to the  variable n
Step 7:  write content present in n.
Step 8: Now the information present in first file copied into second file.
Step 9: Stop

Program:
#include<stdio.h>
#include<fcntl.h>
main()
{
int fd,fd1,fd2,n;
char buff[80];
fd=open("ben.txt",O_RDWR);
fd2=open("ken.txt",O_RDWR);
lseek(fd,0,0);
while((n=read(fd,buff,1))>0)
{
write(fd2,buff,n);
}
printf("file copied");
}

Input:
(Type information in Ben.txt)
C.S.I.I.T

Output:
(open the file Ken.txt)
 C.S.I.I.T


No comments:

Post a Comment

Add your valuable comments Here . . .