Breaking News
You are here: Home / Paper Solutions / SEM-2 / C Programming / C Programming Paper solution MARCH/APRIL 2011

C Programming Paper solution MARCH/APRIL 2011

C Programming Paper solution MARCH/APRIL 2011

Q 1, Answer the Following in Brief (Any Seven)

(i)   Give the rules for waiting one Dimensional Array Definition.

Three Rules to pass an Array to a Function.

1)  The Function must be called by passing only the name of the array.

2)  In the function definition, the formal parameter must be an array type; The size of the array dose not needs to be specified.

3)  The function prototype must show that the argument is an array.

 

(ii) What is a purpose of external variable? What is its scope?

 

  1. A global variables is one whose value can be accessed any of the function in which it is defined.
  2. Local variables are recognized only to the function in which they are defended.
  3. Thus, scope of the availability is limited to the function in which they are defended.

 

(iii)       What is structure member? What is a relationship between structure member and structure?

 

Structure members are used for storing information in members, members of structure can be of different type.

 

The main relation between structure and structure members is that to to access the structure member we must have to use structure.

 

(iv)        What do you mean by overflow and underflow of data?

 

Problem of data overflow occurs when the value of a variable is either too big or too small for the data type to hold. The largest value that a variable can hold also depends on the machine. Since floating-point values are rounded off to the number of significant digit allowed (or specified), an overflow normally result in the largest possible real value, whereas an underflow results in zero.

 

(v) Differentiate between if and goto statement.

If goto
It is a condition statement. Goto is a jumping statement
Syntax: Syntax :
If(condition) Goto( label)
{
(<body of if>)
}
Example : Example :
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int a; int a;
printf(“enter number :”); printf(“enter number :”);
scanf(“%d”,a); scanf(“%d”,a);
if (a%2 == 0) a1: a++
{ while (a%2==0)
printf(“it is even”); {
} printf(“it is even”);
getch(); goto a1;
} }

 

(vi)        Differentiate between break and continue.

Break continue
Break is use to come out of condition or The continue statement is used to skip
looping statement. some part of the loop.
Break terminates the loop. Continue cases the loop to continue with
the  next  iteration  after  skipping  any
statement in between.
When we want to stop the loop after any When we want to continue the loop and
condition satisfied at that time we can use omit some statement at that time continue
break statement. is useful.
Syntax : break; Syntax : continue;
Example : Example :
For (i=0;i<=5;i++) For (i=0;i<=5;i++)
{ {
if(i==3) if(i==3)
break; continue;
printf(“%d\n”,i); printf(“%d\n”,i);
} }
printf(“out”); printf(“out”);
output : output :
0 0
1 1
2 2
out 4
when i=3, it will break the loop and come out
out of loop. When i=3, it will continue the loop and
then display for next value. So 4 will be
printed.

 

(vii)      How can you assign hexadecimal and octal value to integer value?

 

To write numbers in octal, precede the value with a 0. Thus, 023 is 238 (which is 19 in base 10).

 

To write numbers in hexadecimal, precede the value with a 0x or 0X. Thus, 0x23 is 2316 (which is 35 in base 10).

 

(viii) Explain any two string functions. i). strcmp() Function

 

The strcmp function compares two string identified by the arguments and has a value 0 if they are equal. If they are not, it has numeric difference between the first no matching characters in the string. It take the form:

strcmp(string1,string2);

 

String1 and string2 may be string variables or string constants. Examples are: strcmp(name , name2);

 

strcmp(name , ”john”); strcmp(“Rom” , “Ram”);

II). strcpy() Function

 

The strcpy function works almost like a string-assignment operator. It takes the form:

strcpy(String1,string2);

 

Q-2 Do as directed:

 

a). write a program to display the following pattern for say n=7

B A Z Y X W V

  U T S R Q P

   O N M L K

     J I H G

     F E D

     C B

      A

 

OR

a). write the different features of macro and compare it with variable.

Macro Variable
a macro defines a const standing for a variable can contain a value and it
antother constant exporession. can change that value.
Macros are for Code Static Statements Variables are for Dynamic Changing Values
Eg. #include <stdio.h> Eg. #include <stdio.h>
#define a 100;
main ()
main () {
{ printf ( The value of a= %d”, 100);
printf ( The value of a= %d”, a); }
}

 

b). Define the structure called “inventory” that will describe the information like Ino, Iname, price, and qty. Read record for 5 items and write a program to sort on name of the item. Display records in appropriate form.

#include<stdio.h>
#include<conio.h> 

struct inventory
{
int ino;

char iname[50]; int price,qty;

}inv[5]; 

void main()
{
int i,j;

char temp[50]; clrscr(); for(i=0;i<5;i++)
{

printf("Enter Inum-->"); scanf("%d",&inv[i].ino); printf("Enter Iname-->"); scanf("%s",inv[i].iname); printf("Enter price-->"); scanf("%d",&inv[i].price); printf("Enter qty-->"); scanf("%d",&inv[i].qty);
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(strcmp(inv[i].iname,inv[j].iname)<0)
	{
	strcpy(temp,inv[i].iname);
	strcpy(inv[i].iname,inv[j].iname);
	strcpy(inv[j].iname,temp);
	}
}	
}	
printf("\n	---------------------------");

printf("\nInum IName Price Qty"); for(i=0;i<5;i++)
{
printf("\n%d %s %d %d",inv[i].ino,inv[i].iname,inv[i].price,inv[i].qty);
}
getch();
}

 

Q-3 Attempt the following:

(a)    write a menu driven program to carry out the following operation on array:

(a)  Traversing

(b)  Insertion

(c)  Deletion

(d)  Sorting

OR

 

(a) Explain operators in C with their precedence. 

Ans:-

 

Operator: c is supports rich sets of blit in operations. We already use some of them like +,-,*, / , & , < , > , etc….

 

An operator is a symbol that tells the computer to performs centre mathematical and logical manipulations.

C operators can be classified in following categories.

 

C – Operator Types

What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. C language supports following type of operators.

  1. Arithmetic Operators
  2. Logical (or Relational) Operators
  3. Bitwise Operators
  4. Assignment Operators
  5. Misc Operators

Lets have a look on all operators one by one.

 

[1]Arithmetic Operators:

There are following arithmetic operators supported by C language:

Assume variable A holds 10 and variable holds 20 then:

Operator

Description

Example

+ Adds two operands A + B will give 30
Subtracts second operand from the first A – B will give -10
* Multiply both operands A * B will give 200
/ Divide numerator by denumerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0

 

[2]Logical (or Relational) Operators:

There are following logical operators supported by C language

Assume variable A holds 10 and variable holds 20 then:

Operator

Description

Example

== Checks if the value of two operands is equal or not, if yes then condition becomes true. (A == B) is not true.
!= Checks if the value of two operands is equal or not, if values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.
&& Called Logical AND operator. If both the operands are non zero then then condition becomes true. (A && B) is true.
|| Called Logical OR Operator. If any of the two operands is non zero then then condition becomes true. (A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is false.

 

[3]Bitwise Operators:

Bitwise operator works on bits and perform bit by bit operation.

Assume if A = 60; and B = 13; Now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

—————–

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

There are following Bitwise operators supported by C language

Operator

Description

Example

& Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the efect of ‘flipping’ bits. (~A ) will give -60 which is 1100 0011
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111

 

[4]Assignment Operators:

There are following assignment operators supported by C language:

Operator

Description

Example

= Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assigne value of A + B into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C – A
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A

 

[5]Misc Operators

There are few other operators supported by C Language.

Operator

Description

Example

sizeof() Returns the size of an variable. sizeof(a), where a is interger, will return 4.
& Returns the address of an variable. &a; will give actaul address of the variable.
* Pointer to a variable. *a; will pointer to a variable.
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

Operators Categories:

All the operators we have discussed above can be categorised into following categories:

  • Postfix operators, which follow a single operand.
  • Unary prefix operators, which precede a single operand.
  • Binary operators, which take two operands and perform a variety of arithmetic and logical operations.
  • The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.
  • Assignment operators, which assign a value to a variable.
  • The comma operator, which guarantees left-to-right evaluation of comma-separated expressions.

 

Precedence of C Operators:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:

For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedenace than + so it first get multiplied with 3*2 and then adds into 7.

Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedenace operators will be evaluated first.

 

(b)  Explain different branching structure in ‘c’. 

1)Branching:

if statement

This is the most simple form of the branching statements.

if the Condition  is true then the statement or block of statements gets executed otherwise these statements are skipped.

NOTE: Expression will be assumed to be true if its evaulated values is non-zero.

if statements take the following form:

Syntax:

if (Condition)
  {
    Block of statements;
  }

or

if (Condition)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

or

if (expression)
  {
    Block of statements;
  }
else if(expression)
  {
    Block of statements;
  }
else
  {
    Block of statements;
  }

 

 

Example:

Int n=10;
If(n>0)
{
   printf(“Positive”);    
}
Else if (n<0)
{
   printf(“Negative”);    
}
Else
{
  Printf(“Zero”);    
}

 

switch statement:

  • The switch statement is much like a nested if .. else statement.
  • It match the case if any match  is matched , it will execute that block any program control will come out from switch using break command.
  • switch statement can be slightly more efficient and easier to read.
  • It is better than if else statement in that case  where only one condition is true from multiple condition.

Syntax:

Switch( expression )
     {
        case expression1:
                         statements1;
        case expression2:	
                         statements2;    
        case expression3:
                         statements3;
        default :
                         statements4;
     }

 

Example:

Void main()
{
int n; 
printf(“Enter any No.”);
scanf(“%d”,&n); 
Switch(n)
     {
        case 1:
                printf(“Sunday !!!!”);
		   break;
        case 2:	
                printf(“Monday !!!!”);
		   break;
        case 3:
                printf(“Tuesday !!!!”);
		   break;
        case 4:
                printf(“Wednesday !!!!”);
		   break;
        case 5:
                printf(“Thursday !!!!”);
		   break;
        case 6:
                printf(“Friday !!!!”);
		   break;
        case 7:
                printf(“Saturday !!!!”);
		   break;
        default :
                printf(“Invalid Choice !!!!”);
     }
}

 

Using break keyword:

  • If a condition is met in switch case then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword.

What is default condition:

  • If none of the listed conditions is met then default condition executed.

 

 

Q-4 Answer the following :

(a) Write a program to display circular matrix of given number N. if input is 4 then

16 15 14 13

5   4    3   12

6   1    2    11

7  8    9    10

#include <conio.h> 
#include <stdio.h> 

void main()
{
int a[16][16];

int c=1,i,j,k,m=0,n,x; clrscr();

printf("Enter the number of rows and columns for the square matrix \n"); scanf("%d",&n);
x=n;
while(n>=1)
{
for(k=0;k<n;k++)
a[m][k+m]=c++;

for(k=1;k<n;k++) a[k+m][n-1+m]=c++; for(k=n-2;k>=0;k--)

a[n-1+m][k+m]=c++; for(k=n-2;k>0;k--)

a[k+m][m]=c++; n=n-2; m=m+1;

}
for(i=0;i<x;i++)
{
for(j=0;j<x;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
getch();
}

 

OR

(a) Explain the type conversion. Also describe the conditions in which explicit and implicit conversion is used.

 

Type Conversion:

 

Type conversion means to convert a higher data type to lower data type. For the purpose of type conversion we required to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses.

 

There are two type of type conversion:

 

  1. Implicit type conversion
  2. Explicit type conversion
  1. Implicit type conversion
    • C permits mixing of constant and variables of different type in an expression. C automatically converts any intermediate values to proper type so that the expression can be evaluated without losing any significance
    • This automatic conversion is known as implicit type conversion
    • Conversion Hierarchy
    • C Datatype Conversion Hierarchy
    • The final result of an expression is converted to type of the variable on the left of the assignment sign before assigning the value to it. However, the following changes are introduced during the final assignment.
      1. Float to int causes truncation of the fractional part.
      2. double to float causes rounding of digit.
      3. long int to int causes dropping of the excess higher order bits
  2. Explicit type conversion
  • In implicit type conversion, casting is based on automatically and lower to higher.
  • We have just discussed how C performs type conversion automatically. However, there are instances when we want to force a type conversion in a way that is different from the automatic conversion.
  • The process of such a local conversion is known as explicit conversion or casting a value.
  • Where type-name is one of the standard C data types. The expression may be constant, variable or an expression.
  • Some examples of casts and their actions are shown in:

Example:-

  • x= (int) 7.5
  • a= (int) 21.3/ (int) 4.5
  • b= (double)sum/n
  • y= (int) (a+b)
  • z= (int)a+b

 

 

Q-5 do as directed:

(a) How can you format an output of a ‘C’ program?

printf() function is used to display values of variable on screen or display any message in screen. The general form of printf() function is given as under.

printf(“format string”, variable1, variable2, ..variable n);

 

Here format string specifies the variable in that type of data is to be stored in appropriate variable and second part contains sequence of variable list which stores the actual data entered by the used through keyboard .There are many format specifies are available for different data type and many format specifies character is displayed with % symbol. With variable name it is required to specify & symbol for indicating address of that variable.

 

Example:-

#include<stdio.h>
#include<conio.h> 

void main()
{
printf(“Enter any number : “);
}

 

(b) How can ‘.’ Operator is use inside a union and a structure?

The ‘.’ Operator is use two access the member of structure. To call any structure member we all ways need a object of Structure c provide a special operator , the structure member or ‘.’ Operator

For Example,

1.Structure 

#include<stdio.h> 
#include<conio.h> 
struct stud
{
int id;
char name[50];
};

void main()
{

struct stud s; clrscr();

printf(“Enter Student id ===> “); scanf(“%d”,&s.id);

printf(“Enter Student Name ===> “); scanf(“%d”,&s.id);

printf(“\n Std id std Name “); printf(“\n%d\t %s,s.id,s.name); getch()
}

 

2. Union

Union student
{

Int r_no; Char nm[50]; Int mark[3];
};
Student s1;

 

(c) Explain about structured programming.

A structure is a collection of logically related Data item grouped to gather under a single name called Structure tag. The data item that make up a structure are called its members, components or fields & can be of different types.

Syntax:

structure tag_name
{

Data type member 1; Data type member 2;
:
:
:
Data type member N;
};

 

Where struct is a keyword that introduce a Structure Definition.

tag_name is the name of the Structure.

Example:-

1)	struct stdunt 
{ 
int stno; 

char stnm[35]; char stset; 
int step; 
}; 

2)	struct Date 
{

int day; int month; int year;

}; OR

struct Date
{
int day, int month, int year;
};

#include<stdio.h>

#include<conio.h> struct stud
{
int id;
char name[50];
};

void main()
{

struct stud s; clrscr();

printf(“Enter Student id ===> “); scanf(“%d”,&s.id);

printf(“Enter Student Name ===> “); scanf(“%d”,&s.id);

printf(“\n Std id std Name “); printf(“\n%d\t %s,s.id,s.name); getch()
}

OR

 

(d) write a program to input a word and display the variations of the word. 

E.g. If input is ‘space’ Then output should be Space paces acesp cespa espac.

#include<stdio.h>
#include<conio.h>

#include<string.h> void main()
{

char str[100],tmp; int j=o,i,len; clrscr();

printf(“\n\n Enter any String : \n\n“); len= strlen(str);
clrscr();

printf(“\n\n------------------------------------------\n\n“); printf(“\n You have Enter The Following String ”);
printf(“\n\n------------------------------------------\n\n“);
puts(str);

printf(“\n\n------------------------------------------\n\n“);
printf(“\n Rotations of This String Are………\n\n”);
printf(“\n\n------------------------------------------\n\n“);
puts(str);

for(i=0;i<len-1;i++)
{
tmp= str[0];
for(j=o;j<len-1;j++)
{
Str[j] = str[j+1];
}
Str[j] = tmp;
Puts(str);
}
getch();
}

 

 

 

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top