ge2155_-lab_manual_2011-2012_(modi)
DESCRIPTION
fghjkl;TRANSCRIPT
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 Approved bySignature
Name Ms. P. Rajalakshmi Dr .G.SumathiDesignation Assistant Professor HOD-ITDate 03.02.2012 03.02.2012
1
TABLE OF CONTENTS
Exercise No Exercise Name
Study of UNIX OS, Basic commands1 UNIX commands- Advanced , Vi – editor
Shell programming
2
Simple and Conditional Statements a. Area and perimeter of Rectangle b. Conversion from Celsius to Fahrenheitc. Swap two Numbers
d. Even or Odde.Greatest number
3
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
4a. String Lengthb. String Comparisonc. String Concatenation
5a. String Reverse b. String Copyc. Palindrome checking
6 a. Character Count b. Character replacement C Programming on UNIX
7
Dynamic Storage Allocationa. Sorting n numbers b. Addition of two matricesc. Generating Students Marksheet
8
Pointersa. Swapping two values with and without
using temporary variable.b. Sum of elements of an array c. Reversing the elements of an arrayd. Merging of two arrays without using third
arraye. Counting the occurrences of a number in an
array
9
Functionsa. Sequential and binary search. b. Finding nCr value c. Factorial of a number using recursiond.Fibonacci series using recursion
10
File Handlinga. Change mode
b. File Operations - Character I/O Functions c. File Operations - String I/O Functions
2
Ex.No. 1Unix Commands
Aim:To Understand and Work with the Unix commands.
Basic Commands:
Directory commands:
1. mkdir …........... make a directory2. cd ….............. change current directory3. ls ….............. show directory, in alphabetical order4. pwd …............. Print the current working directory5. rmdir …........... remove directory
File commands:
6. cat …............ displays the contents of a file.7. wc ….............. counts words, character and lines8. mv…............... moves a file one place to another place9. cp …...............Copies a file from one place to another10. sort …............ sort the file11. whereis …........ search’s for a given specified file12. rm ….............. remove files
General Commands:
13. hostname …........ displays the name of the Server14. which ….......... gives the path of the command15. whatis …......... tells about the file or command issued.16. history …......... displays the command history.17. who …............. shows who is logged into the local system18. w …............... shows login logged in details19. cal …............. displays the calendar of the year.20. passwd ….......... change your password21. date…............. display the system date with time22. ps…............... displays the current process of the user.23. logout ….......... logs off system24. exit …............ close the connection with the server.
Advanced commands:
1. nl ................. puts the line number to the file.2. uniq ............... removes the repeated lines in a file.3. Diff ............... Displays the different between two files.4. compare ............compare two files.5. file ............... Displays the information about the file6. man (command) ...... shows help on a specific command7. more (file) ........ views a file, pausing every screen full
3
8. head ............... display the first few lines in a file.9. tail ............... show the last few lines of a file10. chmod .............. changes permissions on a file11. chown .............. changes the owner of a file.12. chsh ............... changes the default shell of a user.13. bc ................. a simple calculator14. Kill ............... kills the given process.15. grep ............... search for a string in a file16. 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
4
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 RECTANGLEAim:
To find the area and perimeter of a rectangle using shell program.
Algorithm: 1) Start the execution.2) Read the length and breadth of the rectangle. 3) Calculate the area of the rectangle using the formulae
(length * breadth).4) Calculate the perimeter of the rectangle using the formulae
2*(length +breadth).5) Print the value of area and perimeter.6) Stop the execution.
Sample Output :
Enter the length : 3Enter the bredth : 4Area of the rectangle : 12Perimeter of the rectangle : 14
5
Ex No: 2b
CONVERSION FROM CELCIUS TO FAHRENHEIT
Aim:
To convert the value in Celsius degrees to Fahrenheit using shell program.
Algorithm: 1) Start the program.2) Enter the Celsius value. 3) Calculate the Fahrenheit value by using the formula given below: Fahrenheit =(1.8* Celsius)+32.4) Print the Fahrenheit value.5) Stop.
Sample Output :
Enter the Celsius value: 20Fahrenheit: 68
6
7
Ex No: 2cSWAP 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: 10Enter the b value: 20Swapped values: a=20 and b=10
8
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 number2. 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: 5The entered number is Odd number.
9
Ex.No: 2eGREATEST NUMBER
Aim:
To write a shell program to find the largest of three numbers A,B, and C .
Algorithm: 1) Start the execution.2) Read the three numbers from the user. 3) If the first number is larger than the second and third number, display that the
first number is largest number.4) If the second number is larger than the first and third number, display that the
second number is largest number.5) If the third number is larger than the first and second number, display that the
third number is largest number.6) Stop the execution.
Sample Output :
Enter the values for 3 variables: a=10, b=20, c=30The greatest value is c=30
10
Ex.No: 3a
FIBONACCI SERIES
Aim:
To write a shell program to generate Fibonacci series
Algorithm:
1. Read the number of fibonacci numbers to be generated.2. Assign first two Fibonacci numbers a and b.3. Initialize count of number generated.4. 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.
5. If n even then write out last two fibonacci numbers else write out second last Fibonacci number.
Sample Output :
Enter the limit: 5Fibonacci series01123
11
Ex no: 3b
ARMSTRONG NUMBER
Aim
To write a shell program to check whether the given number is Armstrong or not.
Algorithm
1. Start. 2. Accept the number, N
3. Find the sum of the cube of the individual digits of the number N and store in SUM1.
4. If SUM1 equals to N then N is Armstrong Else N is not Armstrong. 5. Stop
Sample Output :
Enter the Number: 153The 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) Start the execution.2) Read the number from the user3) Declare a loop, which runs only when the number is greater than zero.4) Extract each digit by finding the remainder using mod function5) Multiply the remainder by 10 and add with sum value6) Repeat the procedure till the condition is satisfied.7) Check sum value with the input number. 8) If it equals then number is palindrome.
Sample Output :
Enter the Number: 121The 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. Start2. Read the value of n.3. To print the value 2 on screen.4. Make a for loop 5. Declare i=36. Increment i by 1 upto n7. Using another for loop8. Declare j=29. From j=2 to j<i; divide i by j 10. If i % j = = 0 then print the number.
Sample Output :
Enter the limit: 10Prime Numbers2357
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) Start.2) Read the number3) Set fact=14) Set a loop up to the entered number.5) Calculate factorial fact=fact*I 6) Print the factorial value.
Sample Output :
Enter the Number: 5The 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. Start the program2. Enter the size of the array.3. Enter the elements of the array.4. Set a loop up to the size of the array.5. Find the sum of the array.6. After the execution of the loop. Print the sum 7. Stop
Sample Output:
Enter the size of the array: 5Enter the elements of the array: 6 20 30 4 50Sum 110
16
STRING OPERATIONSEx No: 4a
LENGTH OF A STRING
Aim:
To write a shell program to find the length of the string.
Algorithm:
1) Get the string from the user.2) Find out the number of characters using wc command.3) Decrement 1 from the length as it includes the new line character
also.4) Print the output.
Sample Output :
Enter the String: ComputerString Length:8
17
Ex No: 4bSTRING COMPARISION
Aim:
To write a shell program to compare two strings.
Algorithm:
1) Get the two strings from the user.2) Check for the equality condition between two strings using equality
operator.3) Print the output.
Sample Output :
Enter the first string: SVCEEnter the second string: SVCEBoth the strings are equal.
18
Ex No: 4c
STRING CONCATENATION
Aim:
To write a shell program to concatenate two strings.
Algorithm:
1. Start the program.2. Enter the two strings from the user.3. Then concatenate the two strings using the function strcat()4. Print the concatenated string.5. Stop.
Sample Output :
Enter the string: comEnter the string: puterThe concatenated string: computer
19
Ex.No: 5a
STRING REVERSAL
Aim:
To write a shell program to reverse the string.
Algorithm:
1) Get the string.2) Calculate the length of the string.3) Each character is got from the string in the reverse order using
cut command and stored in the temp variable.4) The other characters are also got in the same way and
concatenated with the previous temp variable.5) Print the output.
Sample Output :
Enter the string: PracticeThe reversed string: ecitcarp
20
Ex No: 5b
STRING COPY
Aim:To write a shell program to copy the string.
Algorithm:
1) Get the string from user2) Get each character using cut command and store it in a temp
variable.3) Get the next character and concatenate it to the previous temp
variable.4) Print the output.
Sample Output :
Enter the string: LaboratoryCopied string: Laboratory
21
Ex No: 5c
PALINDROME CHECKING
Aim:To write a shell program to check whether the given string is
palindrome or not.
Algorithm:1. Start the program2. Enter the string3. Reverse the string using strrev() function.4. Compare two strings using strcmp() function.5. If two strings are equal then print palindrome. 6. If two strings are unequal then print not a palindrome.7. Stop.
Sample Output :
Enter the string: madamReversed string: madamThe 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) Get the string, character to be counted from the user.2) Get each character using cut command and compare it with the
character obtained from the user.3) If the character matches with the character, increment a
counter variable by 1.4) Print the counter variable.
Sample Output :
Enter the String: LirilEnter the character: iNumber of occurrences: 2
23
Ex No: 6b
CHARACTER REPLACEMENT
Aim:
To write a shell program to replace a character in the string.
Algorithm:
1) Get the string and replacing character and the character to replace from the user.
2) Get each character using cut command and check it with the character to be replaced.
3) If the character matches with the character to be replaced, replace it.
4) Print the output.
Sample Output :
Enter the String: LirilEnter the character to be replaced: iEnter the new character to replace: aReplaced 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 times6. Repeat steps 7 to 10 i+1 to n-1 times7. Check *(p+i) > *(p+j) ,if true do steps 8 to 108. Assign temp = *(p+i)9. Assign *(p+i) = p(j)10 Assign p(j) = temp11 Print sorted array elements *(p+i) 12 Stop
Sample Output
Enter No of Array Elements: 4Enter Array Elements: 4,2,10,6
After Sorting: 24610
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-17. 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 c10 Stop.
Sample Output
Enter number of rows& columns in 1st matrix: 3 3Enter number of rows& columns in 2nd matrix: 3 3Enter the elements of the 1st matrix:1 2 34 5 67 8 9Enter the elements in the 2nd matrix:1 0 00 1 00 0 1The matrix after addition is2 2 34 6 67 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: 2Enter details….1.ROLL NO.: 22NAME: sanMATHS MARKS: 100PHY MARKS: 99CHEM MARKS: 882.ROLL NO.: 12NAME: werMATHS MARKS: 88PHY MARKS: 22CHEM MARKS: 33
Student DetailsROLL NO NAME TOTAL AVG. GRADE22 san 287 95 A12 wer 143 47 C
27
Pointers
Ex.No. 8a
SWAPPING TWO VALUESAim: To write a C program to swap the two values with and without using the temporary variables.
Algorithm: (with temporary variable)1. Start the program.2. Read the two numbers.3. Call the display function.4. Pass the address of two numbers to the calling function.5. Get the address in the calling function in the pointer6. Swap the number using temporary variable.7. Print the swapped value in the main function8. 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 pointer14. 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 function16. Stop the program.
Sample Output
Enter the two numbers to swap:3 5The 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 program9. 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: 5Enter the elements of the array: 10 20 30 40 50Sum 150
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 5Output: 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. Start2. 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+m6. Copy the second array elements to first array.7. Print the first array.8. Stop.
Sample Output:
Array 1: 10 20 30Array 2: 11 22 33 44 55
Merged Array: 10 20 30 11 22 33 44 55
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. Start 2. Accept the number of elements, n, in the array.3. For i = 0 to n do step 4 4. Read str(i)5. Declare a variable count.6. Accept the variable to find the number of occurrences of an element in a
array7. 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 count13.Increment the pointer variable.14.Display the counter variable.15. Stop
Sample Output
Enter the number of elements 5Enter the elements12325Enter the number to be counted2Number of occurrences of an element2
32
FUNCTIONS
Ex.No. 9a
SEQUENTIAL AND BINARY SEARCH USING FUNCTIONSAim
To write a program in C to perform sequential and binary search using functions
Algorithm
1. Start.2. Accept the limit ,n3. Read all the n elements in the array a[n]4. Accept the element, s, to search.5. call the function, seq(a,s,n) for sequential search6. call the function, bin(a,s,n) for binary search7. Stop
Function seq(a,s,n)1. Start2. Initialize variables big and small to a[0] and posn to –1 respectively.3. For i = 0 to n-1 do step 44. if (s==a(i)) then posn = i 5. if (posn>-1) then print “The given element is found at position :” , posn6. if (posn==-1) then print “The given element is not found in the array”7. Stop
Function bin(a,s,n)1. Start2. Initialize left=0 right =n3. while left<=right do step 44. mid=floor((right-left)/2)+left5. if value > a[mid] ,left=mid +16. else if value < a[mid], right=mid-17. else return mid8. else return not found9. Stop
Sample output:
Enter No of elements: 3Enter Array elements: 2,6,9Enter Searching element: 6The 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. Start.2. Accept the numbers, n and r.3. Find the factorial of n, r and n-r value using the function fact().4. Find nCr using the formula nCr=n!/ (n-r)!r!5. Print the nCr value6. Stop
Function fact(n)1. Start2. Initialise f = 13. For i = 1 to n do step 44. Multiply f and i and store it in f.5. Return f
Sample output:
Enter the value of n: 3Enter 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. Start2. Enter the number,n for which the factorial has to be calculated3. Call the function, fact(n)4. Print the factorial of the number5. Stop
Function fact (n)
1. if n==1 return 12. else f=n*fact(n-1)3. return f
Sample Output
Enter the number: 4Factorial : 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. Start the program.2. Enter the number.3. Check whether the number is 0 or not4. If 0 print 0 value and not 0 go further.5. set loop up to the given number.6. Fib=Fib + a; a=b;b=c;7. Every increment in the loop prints the value of fib.8. After the execution of the loop stop the program
Sample Output:
Input: 6
Output: 0 1 1 2 3 5
36
FILE HANDLINGEx.No. 10a
CHANGE MODE
Aim:
To write a C program to illustrate the use of CHMOD system call.
Algorithm:
1. Initialize and assign all variables.2. Get the source file name and the mode of file.3. Use CHMOD to change properties and mode of file.4. If the value returned is not equal to –1 then the mode has been
changed.5. Else the mode is not changed.
Sample Output:Enter the filename : aa.txtDefault 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) Initialize the variables.2) Declare a pointer to the file.3) Use fopen to open the destination file in “w “ mode.4) The function fputc writes a character to a file, which is opened for
writing.5) The file pointer is closed.6) Again the destination file is opened in “r” mode.7) The function fgetc is used to read a character from the current position
of the file associated with the pointer.8) The contents of the file are displayed.
Sample Output:Enter the filename : aa.txtEnter 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) Initialize the variables.2) Declare a pointer to the file.3) Use fopen( ) to open the destination file in “w” mode.4) Use fputs function to write a string to a file, which is opened
for writing.
Sample Output:
Enter the source filename : aa.txtEnter the Destination filename: bb.txtCopying from source file to Destination file……
File copied.
>>cat bb.txtThis is Computer Practice Laboratory-II
39