You are on page 1of 39

SRI VENKATESWARA COLLEGE OF ENGINEERING SRIPERUMBUDUR

DEPARTMENT OF INFORMATION TECHNOLOGY LAB MANUAL GE2155 - COMPUTER PRACTICE LABORATORY - II II SEMESTER ANNA UNIVERSITY - REGULATION 2008

Prepared by
Signature Name Ms. P. Rajalakshmi Designation Assistant Professor 03.02.2012 Date

Approved by

Dr .G.Sumathi HOD-IT
03.02.2012

TABLE OF CONTENTS

Exercise No
1

Exercise Name
Study of UNIX OS, Basic commands UNIX commands- Advanced , Vi editor Shell programming Simple and Conditional Statements a. Area and perimeter of Rectangle b. Conversion from Celsius to Fahrenheit c. Swap two Numbers d. Even or Odd e.Greatest number Conditional and Looping Statements a. Fibonacci Series b.Armstrong Number c.Palindrome Number d. Prime Number Generation e.Factorial of a number f.Sum of array elements a. String Length b. String Comparison c. String Concatenation a. String Reverse b. String Copy c. Palindrome checking a. Character Count b. Character replacement C Programming on UNIX Dynamic Storage Allocation
a. Sorting n numbers b. Addition of two matrices

4 5 6

c. Generating Students Marksheet Pointers a. Swapping two values with and without using temporary variable. b. Sum of elements of an array
c. Reversing the elements of an array d. Merging of two arrays without using third array e. Counting the occurrences of a number in an array

Functions
a. Sequential and binary search.

10

b. Finding nCr value c. Factorial of a number using recursion d.Fibonacci series using recursion File Handling a. Change mode b. File Operations - Character I/O Functions 2 c. File Operations - String I/O Functions

Ex.No. 1 Unix Commands Aim: To Understand and Work with the Unix commands. Basic Commands: Directory commands: 1. 2. 3. 4. 5. mkdir ........... make a directory cd .............. change current directory ls .............. show directory, in alphabetical order pwd ............. Print the current working directory rmdir ........... remove directory

File commands: 6. cat ............ displays the contents of a file. 7. wc .............. counts words, character and lines 8. mv............... moves a file one place to another place 9. cp ...............Copies a file from one place to another 10. sort ............ sort the file 11. whereis ........ searchs for a given specified file 12. rm .............. remove files General Commands: 13. hostname ........ displays the name of the Server 14. which .......... gives the path of the command 15. whatis ......... tells about the file or command issued. 16. history ......... displays the command history. 17. who ............. shows who is logged into the local system 18. w ............... shows login logged in details 19. cal ............. displays the calendar of the year. 20. passwd .......... change your password 21. date............. display the system date with time 22. ps............... displays the current process of the user. 23. logout .......... logs off system 24. exit ............ close the connection with the server. Advanced commands: 1. 2. 3. 4. 5. 6. 7. nl ................. puts the line number to the file. uniq ............... removes the repeated lines in a file. Diff ............... Displays the different between two files. compare ............compare two files. file ............... Displays the information about the file man (command) ...... shows help on a specific command more (file) ........ views a file, pausing every screen full

8. head ............... display the first few lines in a file. 9. tail ............... show the last few lines of a file 10. chmod .............. changes permissions on a file 11. chown .............. changes the owner of a file. 12. chsh ............... changes the default shell of a user. 13. bc ................. a simple calculator 14. Kill ............... kills the given process. 15. grep ............... search for a string in a file 16. ln ................. links a file with another name. 17. spell...............used to find the spelling errors in a file Vi Editor Commands 1. (Esc): End a command; especially used with insert `i', append `a' or replace 'R'. 2. u : Undoes last command; 3. :set all : Display all vi options. 4. :w [file]Save into a new file [file], but do not end. 5. :q Quit vi without saving 6. :q! Quit vi without saving, living the file as it was in the last save. 7. :wq Save the default file being edited, and quit. 8. h or j or k or l : The arrow keys, such that 9. 0 : Go to beginning of the line (BOL). 10. $ : Go to end of the line (EOL). 11. yy:copies the content of a line 12. p:paste the contents 13. /[string] : Find the next occurrence of `[string]' forwards. Use `n' to repeat. 14. ?[string] (CR) : Find the next occurrence of` [string]' backwards. 15. n Repeat last `/[string] 16. i :Insert a string 17. o : Opens a new line below the current line for insertion 18. I : Insert a string at the beginning of the current line

19. J : Joins next line to current line. 20. a :Appends a string 21. A : Appends a string at the end of a line 22. r : Replace a single character over the cursor by the single character 23. R : Replace a string of characters by string. 24. x : Delete the current character at the cursor. 25. d(SPACE) : Deletes a single character. 26. dd : Deletes the current line. `[N]dd' deletes `[N]' lines. 27. D : Deletes from the cursor to the end of line (EOL). 28. dw : Deletes the current word; `[N]dw' deletes `[N]' words.

SHELL PROGRAMMING Ex No: 2a AREA AND PERIMETER OF RECTANGLE Aim: To find the area and perimeter of a rectangle using shell program. Algorithm: 1) 2) 3) 4) 5) 6) Start the execution. Read the length and breadth of the rectangle. Calculate the area of the rectangle using the formulae (length * breadth). Calculate the perimeter of the rectangle using the formulae 2*(length +breadth). Print the value of area and perimeter. Stop the execution.

Sample Output : Enter the length : 3 Enter the bredth : 4 Area of the rectangle : 12 Perimeter of the rectangle : 14

Ex No: 2b CONVERSION FROM CELCIUS TO FAHRENHEIT Aim: To convert the value in Celsius degrees to Fahrenheit using shell program. Algorithm: 1) 2) 3) 4) 5) Start the program. Enter the Celsius value. Calculate the Fahrenheit value by using the formula given below: Fahrenheit =(1.8* Celsius)+32. Print the Fahrenheit value. Stop.

Sample Output : Enter the Celsius value: 20 Fahrenheit: 68

Ex No: 2c SWAP TWO VARIABLES Aim: To write a program in Unix to perform the swapping two numbers. Algorithm: 1. Start. 2. Accept the value of two variables a and b. 3. Store the first value in a temporary variable. 4. Copy the second value in a first variable. 5. Copy the temporary variable value in second variable. 6. Display the values after swapping. 7. Stop. Sample Output : Enter the a value: 10 Enter the b value: 20 Swapped values: a=20 and b=10

Ex.No: 2d EVEN OR ODD

Aim: To write a shell program to check even or odd of a given number.

Algorithm: 1. Read a number 2. To find number is even or odd. 3. If the number is divided by modulo 2 and the remainder is zero then print as even number otherwise print as odd number. 4. Stop the execution. Sample Output : Enter the Number: 5 The entered number is Odd number.

Ex.No: 2e GREATEST NUMBER Aim: To write a shell program to find the largest of three numbers A,B, and C . Algorithm: 1) 2) 3) 4) 5) 6) Start the execution. Read the three numbers from the user. If the first number is larger than the second and third number, display that the first number is largest number. If the second number is larger than the first and third number, display that the second number is largest number. If the third number is larger than the first and second number, display that the third number is largest number. Stop the execution.

Sample Output : Enter the values for 3 variables: a=10, b=20, c=30 The greatest value is c=30

10

Ex.No: 3a FIBONACCI SERIES Aim: To write a shell program to generate Fibonacci series Algorithm: 1. 2. 3. 4. Read the number of fibonacci numbers to be generated. Assign first two Fibonacci numbers a and b. Initialize count of number generated. While less than n Fibonacci numbers have been generated do a) Write out next two Fibonacci numbers; b) Generate next Fibonacci number keeping a relevant. c) Generate next Fibonacci number from most recent pair keeping b relevant for next computation. d) Update count of number of fibonacci numbers generated. If n even then write out last two fibonacci numbers else write out second last Fibonacci number.

5.

Sample Output : Enter the limit: 5 Fibonacci series 0 1 1 2 3

11

Ex no: 3b

ARMSTRONG NUMBER Aim To write a shell program to check whether the given number is Armstrong or not. Algorithm 1. 2. 3. 4. 5. Start. Accept the number, N Find the sum of the cube of the individual digits of the number N and store in SUM1. If SUM1 equals to N then N is Armstrong Else N is not Armstrong. Stop

Sample Output : Enter the Number: 153 The Number is Armstrong Number.

12

Ex.No: 3c PALINDROME NUMBER Aim: To write a shell program to check whether the given number is palindrome or not. Algorithm: 1) 2) 3) 4) 5) 6) 7) 8) Start the execution. Read the number from the user Declare a loop, which runs only when the number is greater than zero. Extract each digit by finding the remainder using mod function Multiply the remainder by 10 and add with sum value Repeat the procedure till the condition is satisfied. Check sum value with the input number. If it equals then number is palindrome.

Sample Output : Enter the Number: 121 The Number is Palindrome Number.

13

Ex no: 3d PRIME NUMBER GENERATION Aim To write a shell program to generate prime number series up to the given limit. Algorithm: 1. Start 2. Read the value of n. 3. To print the value 2 on screen. 4. Make a for loop 5. Declare i=3 6. Increment i by 1 upto n 7. Using another for loop 8. Declare j=2 9. From j=2 to j<i; divide i by j 10. If i % j = = 0 then print the number.

Sample Output : Enter the limit: 10 Prime Numbers 2 3 5 7 Result: Thus the prime number series is generated using shell program.

14

Ex no: 3e FACTORIAL OF THE GIVEN NUMBER Aim To write a shell program to find the factorial of a given number. Algorithm: 1) 2) 3) 4) 5) 6) Start. Read the number Set fact=1 Set a loop up to the entered number. Calculate factorial fact=fact*I Print the factorial value.

Sample Output : Enter the Number: 5 The Factorial is: 120

15

Ex.No: 3f SUM OF ARRAY ELEMENTS Aim: To write a Shell program to sum the elements of an array. Algorithm: 1. 2. 3. 4. 5. 6. 7. Start the program Enter the size of the array. Enter the elements of the array. Set a loop up to the size of the array. Find the sum of the array. After the execution of the loop. Print the sum Stop Sample Output: Enter the size of the array: 5 Enter the elements of the array: 6 Sum 110 20 30 4 50

16

STRING OPERATIONS Ex No: 4a LENGTH OF A STRING Aim: To write a shell program to find the length of the string. Algorithm: 1) 2) 3) 4) Get the string from the user. Find out the number of characters using wc command. Decrement 1 from the length as it includes the new line character also. Print the output.

Sample Output : Enter the String: Computer String Length:8

17

Ex No: 4b STRING COMPARISION Aim: To write a shell program to compare two strings. Algorithm: 1) 2) 3) Get the two strings from the user. Check for the equality condition between two strings using equality operator. Print the output.

Sample Output : Enter the first string: SVCE Enter the second string: SVCE Both the strings are equal.

18

Ex No: 4c STRING CONCATENATION Aim: To write a shell program to concatenate two strings. Algorithm: 1. 2. 3. 4. 5. Start the program. Enter the two strings from the user. Then concatenate the two strings using the function strcat() Print the concatenated string. Stop.

Sample Output : Enter the string: com Enter the string: puter The concatenated string: computer

19

Ex.No: 5a

STRING REVERSAL

Aim: To write a shell program to reverse the string. Algorithm: 1) 2) 3) 4) 5) Get the string. Calculate the length of the string. Each character is got from the string in the reverse order using cut command and stored in the temp variable. The other characters are also got in the same way and concatenated with the previous temp variable. Print the output.

Sample Output : Enter the string: Practice The reversed string: ecitcarp

20

Ex No: 5b

STRING COPY Aim: To write a shell program to copy the string. Algorithm: 1) 2) 3) 4) Sample Output : Enter the string: Laboratory Copied string: Laboratory Get the string from user Get each character using cut command and store it in a temp variable. Get the next character and concatenate it to the previous temp variable. Print the output.

21

Ex No: 5c

PALINDROME CHECKING
Aim: To write a shell program to check whether the given string is palindrome or not. Algorithm: 1. 2. 3. 4. 5. 6. 7. Start the program Enter the string Reverse the string using strrev() function. Compare two strings using strcmp() function. If two strings are equal then print palindrome. If two strings are unequal then print not a palindrome. Stop.

Sample Output : Enter the string: madam Reversed string: madam The given string is palindrome.

22

Ex.No. 6a. CHARACTER COUNT

Aim: To write a shell program to count the number of occurrence of a character in the string. Algorithm: 1) 2) 3) 4) Sample Output : Enter the String: Liril Enter the character: i Number of occurrences: 2 Get the string, character to be counted from the user. Get each character using cut command and compare it with the character obtained from the user. If the character matches with the character, increment a counter variable by 1. Print the counter variable.

23

Ex No: 6b CHARACTER REPLACEMENT Aim: To write a shell program to replace a character in the string. Algorithm: 1) 2) 3) 4) Get the string and replacing character and the character to replace from the user. Get each character using cut command and check it with the character to be replaced. If the character matches with the character to be replaced, replace it. Print the output.

Sample Output : Enter the String: Liril Enter the character to be replaced: i Enter the new character to replace: a Replaced string: Laral

24

C PROGRAMMING DYNAMIC STORAGE ALLOCATION Ex.No. 7a

Sorting n numbers
. Aim To write a C program to sort the elements of an array using Dynamic memory allocation. Algorithm Description 1. Start 2. Declare a pointer variable p and read the number of elements, n. 3. Allocate the size for array using malloc() function. P=(int *)malloc(n*sizeof (int)) 4 Read the array values (p+i) 5. Repeat steps 6 to 10 from 0 to n-1 times 6. Repeat steps 7 to 10 i+1 to n-1 times 7. Check *(p+i) > *(p+j) ,if true do steps 8 to 10 8. Assign temp = *(p+i) 9. Assign *(p+i) = p(j) 10 Assign p(j) = temp 11 Print sorted array elements *(p+i) 12 Stop Sample Output Enter No of Array Elements: 4 Enter Array Elements: 4,2,10,6 After Sorting: 2 4 6 10

25

Ex.No. 7b Matrix Addition Aim To write a C program to perform matrix addition using Dynamic memory allocation. Algorithm Description 1. Start. 2. Declare pointers for two arrays and accept the number of rows and columns (r1,c1 , r2,c2) for the two matrices a & b. 3. If r1==r2 && c1==c2 then print Matrix addition is not possible. and goto Step 10 4. Allocate the size for array a & b using malloc() function. i from0 to r1 a[i]=(int *) malloc (c1*sizeof(int)) 5. Read the elements of two matrices. 6. Repeat Steps 7 & 8 for i = 0 r1-1 7. Repeat Step 8 from j = 0 to c1-1 8. Calculate *(*(c+i)+j)= *(*(a+i)+j)+ *(*(b+i)+j) 9. Display the Resultant matrix c 10 Stop. Sample Output Enter number of rows& columns in 1st matrix: 3 3 Enter number of rows& columns in 2nd matrix: 3 3 Enter the elements of the 1st matrix: 123 456 789 Enter the elements in the 2nd matrix: 100 010 001 The matrix after addition is 223 466 7 8 10

26

Ex.No. 7c Students Mark sheet Aim To write a program in C to maintain the students mark list. Algorithm 1. Start 2. Define a structure stu containing members like name, roll number, mark1, mark2, mark3, total, average & grade with structure pointer *s. 3. Read the number of students, n and allocate the size structure pointer using malloc() function. 4. Read the details of the students by accessing the members of the structure by giving s-><member name> where is the loop counter from 1 to n. 5. Repeat steps 6 to 8 for i=1 to n. 6. Compute total and average for the ith student. 7. Depending on the value of average, compute the grade for the ith student. 8. Display the details of the students in tabular format. 9. Stop. Sample Output Enter the number of students: 2 Enter details. 1. ROLL NO.: 22 NAME: san MATHS MARKS: 100 PHY MARKS: 99 CHEM MARKS: 88 2. ROLL NO.: 12 NAME: wer MATHS MARKS: 88 PHY MARKS: 22 CHEM MARKS: 33 Student Details ROLL NO NAME 22 san 12 wer TOTAL 287 143 AVG. 95 47 GRADE A C

27

Pointers
Ex.No. 8a SWAPPING TWO VALUES

Aim:
To write a C program to swap the two values with and without using the temporary variables.

Algorithm: (with temporary variable)


1. 2. 3. 4. 5. 6. 7. 8. Start the program. Read the two numbers. Call the display function. Pass the address of two numbers to the calling function. Get the address in the calling function in the pointer Swap the number using temporary variable. Print the swapped value in the main function Stop the program.

Algorithm: (without temporary variable)


9. Start the program. 10. Read the two numbers. 11. Call the display function. 12. Pass the address of two numbers to the calling function. 13. Get the address in the calling function in the pointer 14. Use the following statement to swap the values. *a=*a+*b *b=*a-*b *a=*a-*b 15. Print the swapped value in the main function 16. Stop the program. Sample Output Enter the two numbers to swap:3 5 The swapped numbers:5 3

28

Ex.No: 8b

SUM OF THE ELEMENTS OF AN ARRAY

Aim: To write a C program to sum the elements of an array. Algorithm: 8. Start the program 9. Enter the size of the array. 10. Enter the elements of the array. 11. Set a loop up to the size of the array. 12. Find the sum of the array. 13. After the execution of the loop. Print the sum 14. Stop Sample Output: Enter the size of the array: 5 Enter the elements of the array: 10 Sum 150 20 30 40 50

29

Ex.No: 8c

REVERSING THE ELEMENTS OF AN ARRAY

Aim: To write a C program to reverse the elements of an array. Algorithm: 1. Start the program. 2. Enter the elements of an array. 3. Reverse the elements and store it in another array using pointers. 4. Print the reversed array. 5. Stop. Sample Output: Enter the elements of the array: 1 2 3 4 5 Output: 5 4 3 2 1

30

Ex.No: 8d
MERGING OF TWO ARRAYS WITHOUT USING THIRD ARRAY

Aim: To write a C program to merge two arrays without using third array Algorithm: 1. Start 2. Read the number of elements of two arrays. 3. Read the elements of two arrays. 4. Let first array size is n and second array size is m. 5. Start the loop from n and execute the loop till n+m 6. Copy the second array elements to first array. 7. Print the first array. 8. Stop. Sample Output: Array 1: 10 Array 2: 11 20 22 30 33 20 44 30 55 11 22 33 44 55

Merged Array: 10

31

Ex.No. 8e COUNT THE NUMBER OF OCCURRENCES OF THE GIVEN NUMBER IN AN ARRAY USING POINTERS Aim To write a program to count the number of occurrences of the given number in an array using pointers. Algorithm 1. 2. 3. 4. 5. 6. array 7. Assign str to a variable ptr. 8. while (*ptr!=\0) repeat steps 9. if(a!=*ptr) 10. Increment pointer variable. 11.else repeat steps 10 and 11. 12.Increment the count 13.Increment the pointer variable. 14.Display the counter variable. 15. Stop Sample Output Enter the number of elements 5 Enter the elements 1 2 3 2 5 Enter the number to be counted 2 Number of occurrences of an element 2 Start Accept the number of elements, n, in the array. For i = 0 to n do step 4 Read str(i) Declare a variable count. Accept the variable to find the number of occurrences of an element in a

32

FUNCTIONS Ex.No. 9a SEQUENTIAL AND BINARY SEARCH USING FUNCTIONS Aim To write a program in C to perform sequential and binary search using functions Algorithm 1. 2. 3. 4. 5. 6. 7. Start. Accept the limit ,n Read all the n elements in the array a[n] Accept the element, s, to search. call the function, seq(a,s,n) for sequential search call the function, bin(a,s,n) for binary search Stop

Function seq(a,s,n) 1. Start 2. Initialize variables big and small to a[0] and posn to 1 respectively. 3. For i = 0 to n-1 do step 4 4. if (s==a(i)) then posn = i 5. if (posn>-1) then print The given element is found at position : , posn 6. if (posn==-1) then print The given element is not found in the array 7. Stop Function bin(a,s,n) 1. Start 2. Initialize left=0 right =n 3. while left<=right do step 4 4. mid=floor((right-left)/2)+left 5. if value > a[mid] ,left=mid +1 6. else if value < a[mid], right=mid-1 7. else return mid 8. else return not found 9. Stop Sample output: Enter No of elements: 3 Enter Array elements: 2,6,9 Enter Searching element: 6 The given element is found at position :2.

33

Ex.No. 9b Finding nCr value Aim To write a program in C to find the nCr value of the given numbers Algorithm 1. 2. 3. 4. 5. 6. Start. Accept the numbers, n and r. Find the factorial of n, r and n-r value using the function fact(). Find nCr using the formula nCr=n!/ (n-r)!r! Print the nCr value Stop

Function fact(n) 1. Start 2. Initialise f = 1 3. For i = 1 to n do step 4 4. Multiply f and i and store it in f. 5. Return f Sample output: Enter the value of n: 3 Enter the value of r: 2 nCr value is :3.

34

Ex.No. 9c

FACTORIAL USING RECURSION Aim To write a program in C to calculate factorial of the given number using functions Algorithm 1. 2. 3. 4. 5. Start Enter the number,n for which the factorial has to be calculated Call the function, fact(n) Print the factorial of the number Stop

Function fact (n) 1. if n==1 return 1 2. else f=n*fact(n-1) 3. return f Sample Output Enter the number: Factorial : 4 24

35

Ex.No. 9d

FIBONACCI SERIES USING RECURSION Aim To write a program in C to generate Fibonacci series using recursion functions. Algorithm 1. 2. 3. 4. 5. 6. 7. 8. Start the program. Enter the number. Check whether the number is 0 or not If 0 print 0 value and not 0 go further. set loop up to the given number. Fib=Fib + a; a=b;b=c; Every increment in the loop prints the value of fib. After the execution of the loop stop the program

Sample Output: Input: 6 Output: 0 1 1 2 3 5

36

FILE HANDLING
Ex.No. 10a CHANGE MODE Aim: To write a C program to illustrate the use of CHMOD system call. Algorithm: 1. 2. 3. 4. 5. Initialize and assign all variables. Get the source file name and the mode of file. Use CHMOD to change properties and mode of file. If the value returned is not equal to 1 then the mode has been changed. Else the mode is not changed.

Sample Output: Enter the filename : aa.txt Default mode has been modified Use ls l to view the modification.

37

FILE OPERATIONS Ex.No. 10b CHARACTER I/O FUNCTIONS OF A FILE Aim: To write a C program to implement character functions in a file Algorithm: 1) 2) 3) 4) 5) 6) 7) 8) Initialize the variables. Declare a pointer to the file. Use fopen to open the destination file in w mode. The function fputc writes a character to a file, which is opened for writing. The file pointer is closed. Again the destination file is opened in r mode. The function fgetc is used to read a character from the current position of the file associated with the pointer. The contents of the file are displayed.

Sample Output: Enter the filename : aa.txt Enter the contents of the file: Welcome The file contents are. Welcome

38

Ex.No. 10c

STRING I/O FUNCTIONS OF A FILE

Aim: To write a C program to implement the string functions in a file. Algorithm: 1) 2) 3) 4) Initialize the variables. Declare a pointer to the file. Use fopen( ) to open the destination file in w mode. Use fputs function to write a string to a file, which is opened for writing.

Sample Output: Enter the source filename : aa.txt Enter the Destination filename: bb.txt Copying from source file to Destination file File copied. >>cat bb.txt This is Computer Practice Laboratory-II

39

You might also like