Vous êtes sur la page 1sur 10

Winter 2011 Master of Computer Application (MCA) Semester 1 MC0061 Computer Programming C Language 4 Credits (Book ID: B0678

678 & B0679) Assignment Set 1 (40 Marks)


Answer all questions Each question carries TEN marks

Book ID: B0678 1. Explain the following operators with an example for each: a. Conditional Operators b. Bitwise Operators c. gets() and puts() function with a programming example for each.

Ans:

a. Conditional Operators
The Conditional operator is also called as Ternary Operator. The conditional operator consists of 2 symbols the question mark (?) and the colon (:). The syntax for a ternary operator is as follows exp1 ? exp2 : exp3 The conditional operator works as follows exp1 is evaluated first. If the expression is true then exp2 is evaluated & its value becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expression is evaluated. For example a = 10; b = 15; x = (a > b) ? a : b Here x will be assigned to the value of b. The condition follows that the expression is false therefore b is assigned to x.
MC0061 Computer Programming C Language Roll No. XXXXXXXXX

b. Bitwise Operators
C has a distinction of supporting special operators known as bitwise operators for manipulation DATA at bit level. A bitwise operator operates on each bit of DATA. Those operators are used for testing, complementing or shifting bits to the right on left. Bitwise operators may not be applied to a float or double. Operator Meaning

&

Bitwise AND

Bitwise OR

Bitwise Exclusive

<<

Shift left

>>

Shift right

For example, The & operator performs a bitwise AND on two integers. Each bit in the result is 1 only if both corresponding bits in the two input operands are 1. x= y & z; assigns x the result of "y AND z". This is different from logical "and" operator, "&&", which takes two logical operands as input and produces a result of "true" or "false". If, y = 0x56
MC0061 Computer Programming C Language Roll No. XXXXXXXXX

z = 0x32 Then, x will be 0x12, because (in binary) 01010110 & 00110010 -------------------00010010

c. gets() and puts() function


gets() reads a complete line of text into a string until a end-of-file (EOF) is encountered. It is the responsibility of the programmer to ensure that the string which receives the input text read by gets is large enough. puts() displays a string onto the standard output or terminal and follows it with a newlinecharacter. #include <stdio.h> main () { char answer[256]; puts("Enter your name"); while((gets(answer))!= NULL) printf("Hello " %s, answer); }

2. Explain the following with a programming example for each: a. Arrays of Structures b. Unions

MC0061 Computer Programming C Language

Roll No. XXXXXXXXX

Ans: a. Arrays of Structures

We can use structures to describe the format of a number of related variables. For example, in analyzing the marks obtained by a class of students, we may use a template to describe student name and marks obtained in various subjects and then declare all the students as structure variables. In such cases, we may declare an array of structures, each element of the array representing a structure variable. e.g, struct stclass student[100]; defines an array called student, that consists of 100 elements. Each element is defined to be of the type struct stclass. Consider the following declaration : struct marks { int subject1; int subject2; int subject3; }; main( ) { static struct marks student[3]={{45,68,81},{75,53,69},{57,36,71}}; } This declares the student as an array of three elements student[0], student[1] and student[2] and initializes their members as follows: student[0].subject1 = 45; student[0].subject2 = 68; .. student[2].subject3 = 71;

MC0061 Computer Programming C Language

Roll No. XXXXXXXXX

Example: Program to process employee details using structures #include<conio.h> #include<stdio.h> struct employee { int empno; char name[30]; int basic; int hra; }; void main() { int i,j,n,net[50]; float avg; employee e[50]; printf(\nEnter the number of employees:); scanf(%d, &n); printf(\nEnter Empno.\tName\tBasic\tHra of each employee:\n); for(i=0;i<n;i++) { scanf(%d,&e[i].empno); gets(e[i].name); scanf(%d,&e[i].basic);
MC0061 Computer Programming C Language Roll No. XXXXXXXXX

scanf(%d,&e[i].hra); net[i]= e[i].basic+e[i].hra; avg=avg+net[i]; } avg=avg/n; printf(\nEmpno.\tName\tNetpay\n); for(i=0;i<n;i++) { if(net[i]>avg) { printf(e[i].empno\t); printf(e[i].name\t); printf(net[i]\n); } } getch(); }

b. Unions

Unions look similar to structures. They have identical declaration syntax and member access, but they serve a very different purpose. union Utype { int ival;
MC0061 Computer Programming C Language Roll No. XXXXXXXXX

float fval; char *sval; };

union Utype x, y, z; Accessing members of a union is via . member operator or, for pointers to unions, the -> operator. A union holds the value of one-variable at a time. The compiler allocates storage for the biggest member of the union. The type retrieved from the union must be the type most recently stored. Otherwise, the result is implementation dependent. union Utype x; x.fval = 56.4; /* x holds type float. */

printf(%f\n, x.fval); /* OK. */ printf(%d\n, x.ival); /* Implementation dependent. */ Unions are used to store one of a set of different types. These are commonly used to implement a variant array. (This is a form of generic programming.) There are other uses also, but they are quite advanced (e.g., concern the alignment properties of unions).

Book ID: B0679 3. Write a program demonstrating the usage of pointers with one dimensional and two dimensional arrays. Ans:

Arrays and pointers are very intimately connected. Most important point to remember is that array name without the brackets is the base address of that array or address of its first (0th) element. This address can be stored in the pointer. More fading this subtle difference is that pointer can also be used with brackets to index array as if it was an array name.
MC0061 Computer Programming C Language Roll No. XXXXXXXXX

In general access provided by the pointer is faster if pointer arithmetic(which by the way is almost completely different from normal arithmetic)is used for acess. Formula for accessing Ith element in a 1-D array is *(pointer name + I) Formula for accessing Ith row, Jth Column element in a 2-D array is *(pointer name + I * Number_Of_Elements_Per_Row + J) And so on, formula become more complicated with increase in dimension of the array.

Code below shows how to access a 1-D array using pointers. #include <stdio.h> int main () { int value [1000],i; // Declaring an Array int *pointer; pointer = value; for (i = 0; i<1000; i++) { *(pointer +i) = i; // Loading the Array } for (i = 0; i<1000; i++) { printf ("\n%d",*(pointer + i); // Displaying the Array } return 0; }

An array is actually very much like pointer. We can declare the arrays first element as a[0] or asint *a because a[0] is an address and *a is also an address the form of declaration is equivalent. The difference is pointer is a avalchanda and can appear on the left of the assignment operator that is lvalue. The array name is constant and cannot appear as the left side of assignment operator.

MC0061 Computer Programming C Language

Roll No. XXXXXXXXX

An example program which shows how to access 2-D array using pointer : #include <stdio.h> #include <stdlib.h> main() { l o n g m a t [ 5 ] [ 5 ] , * * p t r ; mat[0][0] = 3; ptr = (long **)mat; printf(" mat %p \n", mat); printf(" ptr %p \n", ptr); printf(" mat[0][0] %d \n", mat[0][0]); printf(" &mat[0][0] %p \n", &mat[0][0]); printf(" &ptr[0][0] %p \n", &ptr[0][0]); return; } The output is: mat 7FDF6310ptr 7FDF6310mat[0][0] 3&mat[0][0] 7FDF6310&ptr[0][0] 3

4. Describe the following with suitable programming examples: a. Input/Output operations on files b. Predefined Streams c. Error handling during I/O operations d. Random access to files Ans:

MC0061 Computer Programming C Language

Roll No. XXXXXXXXX

Remaining answers are available in the full assignments (in MS-WORD format).

For full assignments Contact us:

Prakash: 9686515230
Email: info@assignmentsclub.com / assignments.prakash@gmail.com Website: www.assignmentsclub.com

Note: Sample papers are available in Portable Document Format (.pdf) with a watermark of our Website. Full assignments will be in MS-WORD (.doc) format without any watermark... Contact us for Full assignments...

MC0061 Computer Programming C Language

Roll No. XXXXXXXXX

Vous aimerez peut-être aussi