Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Sem: II Programming in C- II (SPH8S12) credit: 2

UNIT-I Defining an array- Processing in array- one dimensional arrays, two dimensional arrays- multidimensional array- passing arrays to functionsprogrammers using arrays and strings. UNIT-II To multiply 2 matrices of order( lxm) and (mxn) to add and subtract 2 matrices- to arrange the given set of numbers in ascending order- to arrange the given set of numbers in descending order. UNIT-II To find the arithmetic mean, geometric mean and harmonic mean of a given set of numbers. UNIT-IV Defining a structure processing a structure- array of structure- unions- bit fields- programmer using structure- to print current date and time using functions. UNIT-V To purpose the salary bill for employees of a company pointers fundamentals pointer -declaration pointers- simple variables.
BOOKS FOR STUDY: 1. Programming In ANSI C- C. Balagurusamy

UNIT-I

ARRAY: An array is a collection of data item that all have the same name. The data items must be of the same type. The individual data items are represented by a corresponding array element. The individual array elements are distinguished from one another by the value assign to the subscript. DEFINING AN ARRAY: Arrays are defined in such a manner as ordinary variables except that array name must be accompanied a size specification i.e. number of elements. The general format for an array is: Syntax: data-type variable name [size];

Example: suppose in a classroom if we want to get 10 values for the subject science. int sci [10]; Then science is treated as variables. Since marks of 10 students are different but they all coming under variable name science. Array values are start with zero. First element sci [0] Second element sci [1] : : Tenth element sci [9]

PROCESSING IN ARRAY: Single operations which involve entire array are not permitted in C. Thus if a & b are singular arrays. i.e. same data type, same dimension, same size. To perform assignment operations, comparison operation, etc these must be carried in an element by element basis. And this is usually done by within a loop where each of them pass through the loop is used to process one array element. The number of process through the loop will therefore equal to the number of array element to be processed. Numerical array are processed also in the same manner. In a numerical array each array element represents a single numerical quantity.

TYPES OF ARRAY: Array can be classified in many types according to their dimensions. They are: 1. one dimensional array 2. two dimensional array 3. multi dimensional array ONE DIMENSIONAL ARRAY: One dimensional array is also called as list. This means that by giving its subscription we can access an item in an array manner. An one dimensional array requires one pair of square brackets [ ]. The general format of one dimensional array is: Syntax: data-type variable name [array size];

Ex: int a[5]; Here we declared an array element 5 with integer data type. This can be referred as a [0], a [1], a [3], a [4].

DECLARATION OF ONE DIMENSIONAL ARRAY: Like any other values of variables arrays must be declared before they are used. So that, the compiler can allocates space for them in memory. Ex: int a [6]; In this, the data type refers the type of element in a array such as integer, float, character, etc. Then the size indicates the maximum number of elements that can be stored inside the array. While declaring array, we should the following: 1. Any reference to the array outside the declared limits can not necessarily cause an error. Rather it might result in unpredicted program research. 2. The size should be either a numeric constant or a symbolic constant (character).

TWO DIMENSIONAL ARRAYS: Two dimensional arrays is an array with two one dimensional array elements. A single dimensional array can store a list of values whereas two dimensional arrays can store a table of values. Suppose a situation if we have to store a table of values, we use two dimensional array in C.

Syntax:

data-type variable name [size of row][size of column];

Ex: int sales [3][3];

Book 1 Sales 1 Sales 2 Sales 3 310 210 405

Book 2 275 190 235

Book 3 365 325 240

In this table it contains a total of 9 values in each line. So for this case, we have to think it in the form of matrix consisting 3 rows and 3 columns. In mathematics, we represent a particular value in a matrix form by using to subscript as A ij . Here, A denotes the entire matrix, and I denotes the row and j denotes the column. To manipulate these kinds of data, C provides us two dimensional arrays. MULTI DIMENSIONAL ARRAY: An array can be one-dimensional, two dimensional and multi

dimensional depending on the declaration of the dimension of an array element. For a multi dimensional array, it requires more than three pairs of square brackets. The general format of the multi dimensional array is: Syntax: data-type array name [expn 1][expn 2][expn3].[expn n]; Ex: float mul [2][2][3];

PROGRAMS: 1. Program to accept and store 12 numbers in the form of 3x4 matrices. # include<stdio.h> Main( ) { int i, j, matrix [3][4]; for(i=0;i<3;i++) { printf(enter the number for row %d \n\t, i+1); for(j=0;j<4;j++) { scanf(%d, &matrix [i][j]); printf(\t); } } printf(\n you entered); for(i=0;i<3;i++) { printf(\n row %d\t,i+1); for(j=0;j<4;j++) { printf(%d \t,matrix [i][j]); } } getch(); }

2. Using scanf( ) with arrays: #include<stdio.h> main( ) { char name[10]; printf(enter your first name); scanf(%s, name); printf(Hello %s, name); } When scanf( ) is used as a input function it accepts only a string terminated by a white space character.

3. Using gets( ) with arrays: The easiest way to input a string from the keyboard is with a gets( ) library function. The gets( ) will continue to read characters until you enter a carriage return. EX: #include<stdio.h> main( ) { char name[10]; printf(enter your first name); gets( name); printf(Hello %s, name); }

UNIT-II 1. Program to multiply two matrices: #include<stdio.h> #include<conio.h> #include<math.h> main( ) { int m1[5][5], m2[5][5], mul[5][5], i,j,k,r1,c1,r2,c2; clrscr( ); printf(Enter row1, column1 value:); scanf(%d%d, &r1,&c1); printf(Enter row2, column2 value:); scanf(%d%d, &r2,&c2); printf(Enter elements of first matrix\n); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { scanf(%d, &m1[i][j]); } } printf(Enter elements of second matrix\n); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf(%d, &m2[i][j]); } }

clrscr( ); print(\n first matrix\n); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf(%d, m1[i][j]); } printf(\n); } print(\n second matrix\n); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf(%d, m2[i][j]); } printf(\n); } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { mul[i][j]=0; for(k=0;k<r2;k++) { mul[i][j]=mul[i][j]+(m1[i][k]*m2[k][j]);; } } } printf(\n resultant matrix\n);

printf(\n-------------------\n); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf(%d, mul[i][j]); } printf(\n); } getch( ); } Output: Enter row1,column1 value: 2,2 Enter row2,colunnm2 value: 2,2 Enter values for first matrix 3563 Enter values for second matrix 6718 First matrix 35 63 Second matrix 67 18 Resultant matrix 23 23 66 66

*************************************

2. Program for the addition and subtraction of two matrices: #include<stdio.h> #include<conio.h> #include<math.h> main( ) { int a[5][5], b[5][5], c[5][5], i,j; clrscr( ); printf(Enter elements of first matrix\n); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf(%d, &a[i][j]); } } printf(Enter elements of second matri x\n); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf(%d, &b[i][j]); } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { c[i][j]= a[i][j]+b[i][j];

} } printf(\Addition of matrices is %d\n, c[i][j]); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf(%d, c[i][j]); } printf(\n); } for(i=0;i<r;i++) { for(j=0;j<c;j++) { c[i][j]= a[i][j]-b[i][j]; } } printf(Subtraction of matrices\n); for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf(%d, c[i][j]); } printf(\n); } getch( ); }

Output: Enter elements of first matrix: 2 3 5 7 Enter elements of second matrix: 1 0 4 8 Addition of matrices: 14 16 33 40 Subtraction of matrices: -10 16 -23 40 *************************************

3. Program to arrange the given set of numbers in ascending order: #include<stdio.h> #include<conio.h> main( ) { int a[5], i,j,n,temp; clrscr( );

printf(Enter n value:); scanf(%d, &n); printf(Enter numbers one by one:\n); for(i=1;i<=n;i++) scanf(%d, &a[i]); for(i=0;i<=n;i++) { for(j=i+1;j<=n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf( Ascending order); for(i=0;i<=n;i++) printf(%d\n, a[i]); getch( ); } Output: Enter n value: 4

Enter numbers one by one: 5 7 2 3 Ascending order: 2 3 5 7 ************************************

4. Program to arrange the given set of numbers in descending order: #include<stdio.h> #include<conio.h> main( ) { int a[5], i,j,n,temp; clrscr( ); printf(Enter n value:); scanf(%d, &n); printf(Enter numbers one by one:\n); for(i=1;i<=n;i++) scanf(%d, &a[i]); for(i=0;i<=n;i++) { for(j=i+1;j<=n;j++)

{ if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf( Descending order); for(i=0;i<=n;i++) printf(%d\n, a[i]); getch( ); } Output: Enter n value: 5 Enter numbers one by one: 1 4 6 7 2 Descending order: 7 6 4 2 1 ************************************

UNIT-III Program for calculating the Arithmetic mean, Geometric mean, Harmonic mean:
#include<stdio.h> #include<conio.h> #include<math.h> main() { int no,i; float a[100], sumam, sumgm, sumhm, am,gm,hm; clrscr(); printf(enter how many numbers:); scanf(%d, &no); printf(enter elements for array A:); for(i=0;i<no;i++) { scanf(%f, &a[i]);m } sumam=sumgm=sumhm=0; for(i=0;i<no;i++) { sumam += a[i]; sumgm +=log[i]; sumh +=10/a[i]; } am= sumam/no; gm=sumgm/no; hm=no/sumhm; printf(Arithmetic mean=%8.4f, am); printf(Geometric mean =%8.4f, gm); printf(Harmonic mean=%8.4f, hm); getch( ); }

UNIT-IV
Defining a structure: A structure is compound data type. It is a collection of different types of variables. It uses functions. The data items in structure are called as members. In this, separate memory location is allocated for each member. It is also used to keep the functions into the structure. Not like arrays, it is defined first for their format that may be used later to declare structure variables. The general format of a structure is: struct { data_type member1; data_type member2; ---------------------------------------------} PROCESSING A STRUCTURES: Since we can process the structure members in a number of ways. The members themselves are not variables. They should be linked to the structure variables in order to make them meaningful members. For example, the word title has no meaning whereas the phrase title of book3 has a meaning. The link between a member and a variable is established using the member operator. That is also known as dot operator or period operator. tag_name

ARRAYS OF STRUCTURES: Since in structures, arrays are used or declared to describe the format of a number of related variables. For example, in analyzing marks obtained by the students of a class. By use of this, we can describe the data items in a program manner. In cases, we may declare an array of structures and each element of array representing a structure variable. Ex: struct class student[100]; In this, array is student consists of 100 members. And each element is defined to be the type struct class. ARRAYS WITHIN STRUCTURE: Arrays of characters can be used inside a structure and the use of arrays as structure element is also permitted in C. we can use single, two or multidimensional arrays of data type integer or float inside a structure. Consider the following structure declaration as Struct stdrec { Int number; Float marks[3]; }marksec[5]; Here the member marks contains three elements. i.e. mark[0] mark[1] mark[2]

Thus it shows that marks obtained in three different subjects. The statement markrec[1].mark[2] will refer the second mark record and marks obtained by the third subject. UNION: A union is similar in structure. Since in structure, separate memory location is allocated for each member. But in union, some memory is only allocated for all members. All members store the memory. Since in structure, uses functions. But in union, it is not possible. Although a union may contain many members of different types but it can handle only one member at a time. Like array, it is declared using keyword as: union item { int m; float x; char c; code; } This declares variable code of the type union item. It contains 3 member but each with a different type. It uses only one item at a time since one location is allocated for a union variable. Ex:

BIT FIELDS: A bit field is a set o adjacent bits whose size can be varied from 1 to 16 bits in length. It allows a direct manipulation of string o preselected bits as if

it represented as integral quantity. Thus word can be divided into a number of bit fields. The name and size of the bit fields are defined using a structure. The general form of a bit field is: struct tag_name { data-type name1: bit-length; data-type name2: bit-length; ------------------------------------------------------------------------------------data-type name N: bit-length; } Since the data type is either int or signed int or unsigned int. the bitlength is the no of bits used and note that bit field should have at least 2 bits to precede the process.

Program using structures:


Program to illustrate the comparison: struct class { int number; char name[12]; float mark; }; main( ) { int x; struct class student 1= {121, geetha, 87.5}; struct class student 2= {121, uma, 67};

struct class student 3; student3=student2; x= ((student3.number == student2.number)&& (student3.marks == student.marks))?1:0; if (x==1) printf(\n student2 and student3 are same \n\n); printf(%d%s%f\n, student3.number,student3.name,student3.marks); } else printf(\n student2 and student3 are different \n\n); } Program to print current date and time using function: #include<dos.h> #include <stdio.h> main() { struct date curday; struct time curtime; get date(& curday); get time(& curtime); printf(\n Todays date is in dd/mm/yyyy format is :); printf(%d-%d-%d, curday.da-day,curday.da-mon,curday.da-year); printf(\n Todays time is in hh/mm/ss format is :); printf(%d-%d-%d, curtime.ti-hour,curtime.ti-min,curtime.ti-sec); } Output: Todays date is in dd/mm/yyyy format is : 4-6-2010 Current time is hh/mm/ss format is: 7:4:45

UNIT-V
2. Program for salary bill of employee: #include <stdio.h> #include <conio.h> { struct employ { int id no; int type; char name[20]; char address [50]; long balance; long last_payment; }emp; main( ) { int id no,account no; printf(enter the id no:); scanf(%d, & id no); printf(enter the acct-no:); scanf(%d, & acct_no); printf(enter the type:); scanf(%d, & emp.type); printf(enter the name:); scanf(%d, & emp.name[ ]); printf(enter the address:);

if(acct_no=emp.id no) { printf(previous balance:%d,emp.balance); printf(enter the last payment:); scanf(%d,&emp.last payment); emp.balance += emp.last pay; printf(the final balance is %d, emp.balance); } else printf(invalid account number); getch( ); }

POINTERS FUNDAMENTALS: A pointer holds the address of the variables. That is they are used to store the address of the variable. It is one of the most dangerous features in the C-language. It provides a way of accessing a variable without referring to the variable directly. A pointer contains a memory address of a variable. For example: main( ) {

int *ptr,x; x=56; printf( the value of x is %d and its address is %d, x,ptr); getch( ); }

In this program, the variable ptr is holding the memory address of the variable x. POINTER DECLARATION: Pointer variables must contain addresses that belong to separate data type. Pointer variables are declared similarly as normal variables except for the addition of the unary operator. This symbol can appear anywhere between the type name and the printer variable name. The declaration of a pointer takes the forms as: data_type *pt_name;

It should note the 3 things as: 1. The * tells the variable pt_name is a pointer variable 2. Pt_name points a memory location 3. Pt_name points to a variable of type data_type For ex: Int *p; Float *x; ____________ integer pointer ____________ float pointer

SIMPLE VARIABLES: A variable is a data name that may use to store a data value. Unlike constants which remain unchanged during the execution. But a variable can

take different values at different times of execution. A variable name can be chosen by the programmer in a meaningful way so as to reflect a function and nature of the program. It is used to store values for the calculations. A variable consists of 3 parts: 1. Name of the variable-identifier 2. Data type of the variable 3. Value of the variable Syntax: datatype name; Ex: int x; char name[20];

You might also like