pc202 lab manual

Upload: patelvivekj7930

Post on 10-Apr-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 PC202 Lab Manual

    1/5

    PC202 - Introduction to Computer Technology-II

    Credit:5 (L=4, P=2)Lab Practical

    CC: Nitesh SurejaObjective:

    This is an introductory course for programming language C.Students are introduced to the basic concepts of solving a problem using Cprogramming language. Data types available in C, various constructs,loops, arrays, structures, files, pointers and other important features of Care covered in depth during the course work. Students are alsoencouraged to build their logic for solving related problems using C. Thiscourse provides a platform to learn the higher level languages too.

    Lab 1-2:

    LOOPING

    The C language provides for three loop constructs for performingloop iterations. They are:1. The while statement. 2. The do statement. 3. The for statement.Control structure may be classified as the entry-controlled loopor as exit-controlled loop.

    -The while and for statements are entry-controlled loopstatements.-The do statement is exit-controlled loop statement.

    Exercise:1 I Write a program to check whether the given

    number is prime or notII Write a program to compute the sum of the digits

    of a given integer number.III Write a program to find the factorial value of agiven number.

    IV Write a program to find out the number of digitsof a given number.

    2 I Given a number, write a program to reverse thedigits of the number. (For example, the number12345 should be written as 54321)

    II Write a program to print Fibonacci series. The firstseven Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8.

    III Write programs to print the following patterns.

    a) 1 b) 1 c) 1d) 12 2 2 3 2 20 13 3 3 4 5 6 3 3 31 0 14 4 4 4 7 8 9 10 4 4 4 4

  • 8/8/2019 PC202 Lab Manual

    2/5

    0 1 0 1

    Lab3-4:

    ARRAY:An array is a group of related data items that share a

    common name. For instance, we can define an array name

    salary to represent a set of salaries of a group of employees.The ability to use a single name to represent a collection of

    items and to refer to an item by specifying the item numberenables us to develop concise and efficient programs.

    A list of items can be given one variable name using onlyone subscript and such a variable is called a single-subscriptedvariable or a one-dimensional array.Exercise:

    3: I Write a program to find out maximum andminimum from 1-D array.

    II Write a program to sort an integer array in

    ascending order.III Write a program to sort an integer array in

    descending order.IV Write a programme to print pascal triangleV Write a program to search an element in an array, if it is not

    there then insert it at the end of the array

    4: I Write a program to add two 3 by 3 matrices.

    II Write a program to multiply two matrices

    III Exercise 7.5 (E Balagurusamy Book, ThirdEdition)

    Lab 5: STRING.A string is an array of characters.Any group of characters defined between double quotationmarks is a constant string. Character strings are often used tobuild meaningful and readable programs. A string variable is anyvalid C variable name. The general form of declaration of a stringvariable is

    Char string_name[ size ];The size determines the number of characters in the string-name.When we initialize a character array by listing its elements, we

    must supply explicitly the null terminator.Exercise:I Write a separate program that will demonstrate

    the use of strcat( ), strcmp( ), strcpy( ), strlen( )functions.

    II Write a program to count the number of words,number of characters, numbers of blanks andnumbers of lines in a multiline string.

  • 8/8/2019 PC202 Lab Manual

    3/5

    III Write a program to reverse the string.IV Write a program to count frequency of the last character of a

    given string in that string. String will be taken from the user.

    Lab 6:

    USER-DEFINED FUNCTION:

    C functions can be classified into two categories:

    1 library functions : example printf, scanf, sqrt etc2 user-defined functions: main, developed by the user at thetime of writing a program.A function is a self-contained block of code that performs aparticular task.In order to make use of a user-defined function, we need toestablish three elements that are related to functions:

    1. Function definition2. Function call3. Function declaration

    Exercise:I Write a program for addition of two integer

    number that will satisfied the followingcriteria:1. Function with no arguments and no returnvalues.2. Function with arguments and no returnvalues.3. Function with arguments and one returnvalue.4. Function with no arguments but return a

    value.II Write a program to find out the factorial of a

    given number with the use of recursion.III Write a program that uses a function to sort

    an array of integers.IV Write a separate program that will demonstrate the use of

    static variables and global variables.

    Lab7-8:

    STRUCTURES AND UNIONS:An array is a collection of related data elements of same type.Structure can have elements of different types.Unions are a concept borrowed from structures and therefore

    follow the same syntax as structures. However, there is majordistinction between them in terms of storage. In structures, eachmember has its own storage location, whereas all the membersof a union use the same location. This implies that, although aunion may contain many members of different types, It canhandle only one member at a time.Exercise:

  • 8/8/2019 PC202 Lab Manual

    4/5

    I Define a structure data type called time_structcontaining three members integer hour,integer minute, and integer second. Developa program that would assign values to theindividual members and display the time in

    this form: 16:40:51II Define a structure called cricket that willdescribe the followingInformationPlayer nameTeam nameBatting averageUsing cricket, declare an array player with 3elements and write a program to read theinformation about all the 3 players and print ateam-wise list containing names of players

    with their average.III Design a structure student_record to contain

    name, roll_no,and total marks obtained. Develop a programto read data for 10 students in a class and listthem rank-wise.

    IV Write a program to demonstrate the use ofunion.

    Lab 9:

    POINTERS:A pointer is a derived data type in C. Pointers contains memoryaddresses as their values. Since these memory addresses are

    the locations in the computer memory where programinstructions and data are stored, pointers can be used to accessand manipulate data stored in the memory.Exercise:I Write a program using pointers to compute

    the sum of all elements stored in an array.II Write a function using pointers to exchange

    the values stored in two locations in thememory.

    III Write a function using pointers for solution ofquadratic equation (ax2+bx+c=0) using

    pointers.Lab10:

    FILE MANAGEMENT:The console oriented I/O functions, such as printf, scanf worksfine as long as the data is small. However, many real-lifeproblem involve large volumes of data and in such situations,the console oriented I/O operations pose problems. One of theproblem is the entire data is lost when either the program isterminated or the computer is turn off.

  • 8/8/2019 PC202 Lab Manual

    5/5

    It is therefore necessary to have a more flexible approach wheredata can be stored on the disks and read whenever necessary,without destroying the data. This method employs the concept offiles to store data. A file is a place on the disk where a group ofrelated data is stored.

    Exercise:I Write a program to read data from thekeyboard, write it to a file called INPUT, againread the same data from the INPUT file, anddisplay it on the screen.

    II Write a program to read n integer numberfrom keyboard and store them into a fileALL.txt. From the file ALL.txt, separate evenand odd numbers and store them into filesEVEN.txt and ODD.txt respectively.Display content of all the three files.

    III Write a program that will read n number fromkeyboard, store them in file UNSORT.txt. Sortall the contents of UNSORT.txt and store intoSORT.txt.Display contents of both these files.