Vous êtes sur la page 1sur 9

MAY 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 Book ID: B0678 1. Write a program to print the factors of a given number. Ans: //program to print the factors of a given number #include<stdio.h> #include<conio.h> void main() { clrscr(); int n=0,i=0; printf("\n Enter the number:"); scanf("%d",&n); printf("\n The factors of %d are:",n); for(i=1;i<=n;i++) { if(n%i==0) printf(" %d ",i); } getch(); }

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

2. With the help of suitable examples, explain type conversions using various data types. Ans: In computer science, type conversion or typecasting refers to changing an entity of one DATA type into another. This is done to take advantage of certain features of type hierarchies. For instance, values from a more limitedset, suchas integers, can be stored in a more compact format and later converted to a different format enabling operations not previouslypossible, such as division with several decimal places' worthof accuracy. In object-oriented programminglanguages, type conversion allows programs to treat objects of one type as one of their ancestor types to simplify interacting with them. There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion. The most common form of explicit type conversion is known as casting. Explicit type conversion can also be achieved with separately definedconversion routines such as an overloaded object constructor. Each programminglanguage has its own rules on how types can be converted. In general, both objects and fundamental DATA types can be converted.

IMPLICIT TYPE CONVERSION

Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. Some languages allow, or even require, compilers to provide coercion. In a mixed-type expression, DATA of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal Clanguage code:

double d;
MC0061 - Computer Programming C Language Roll No. XXXXXXXXX

long l; int i; if (d > i) d= i; if (i > l) l= i; if (d ==l) d*= 2;

Although d,landibelong to different DATA types, they will be automatically converted to equal DATA types each time a comparison or assignment is executed. This behavior should be used withcaution, as unintendedconsequences can arise. DATA can be lost when floating-point representations are converted to integral representations as the fractional components of the floating-point values will be truncated (rounded towards zero). Conversely, converting from an integral representation to a floating-point one can also lose precision, since the floating-point type may be unable to represent the integer exactly(for example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can). This can lead to situations such as storing the same integer value into two avalchandas of type integer and type float which return false if compared for equality.

EXPLICIT TYPE CONVERSION

Explicit type conversion is a type conversion which is explicitly definedwithin a program (instead of being done by a compiler for implicit type conversion). USING CASTING double da = 5.5; double db = 5.5;

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

int result = static_cast<int>(da) + static_cast<int>(db); //Result would be equalto 10 instead of 11. There are several kinds of explicit conversion.

CHECKED Before the conversion is performed, a runtime checkis done to see if the destination type can hold the source value. If not, an error condition is raised.

UNCHECKED No check is performed. If the destination type cannot hold the source value, the result is undefined.

BIT PATTERN The raw bit representation of the source is copiedverbatim, andit is reinterpreted accordingto the destination type. This can also be achieved via aliasing. In object-oriented programminglanguages, objects can also be downcasted : a reference of a base class is castedto one of its derived classes.

USING OVERLOADED OBJECT CONSTRUCTOR

class Myclass { public: double myD; Myclass(double d) : myD(d) {};

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

}; int main(int argc, char *argv[]) { Myclass obj = 5.2; // here is the type conversion return 0; }

3. Write a program to multiply two matrices. Ans: // program to multiply two matrices #include<stdio.h> main() { int i,j,k,l; int matrix[3][3],matri[3][3],matr[3][3]; printf("Enter 1st matrix: \n"); for (i=0;i<3;++i) { printf("\nEnter #%d row: ",(i+1)); for (j=0;j<3;++j) scanf("%d",&matrix[i][j]); } printf("\n\n"); printf("Enter 2nd matrix: \n"); for (i=0;i<3;++i) { printf("\nEnter #%d row: ",(i+1)); for (j=0;j<3;++j) scanf("%d",&matri[i][j]); }
MC0061 - Computer Programming C Language Roll No. XXXXXXXXX

/*printf("\n\n"); /for (i=0;i<3;++i) /{ /for (j=0;j<3;++j) }*/ matr[i][j]={{0,0,0},{0,0,0},{0,0,0}} for (j=0;j<3;++j) { for (i=0;i<3;++i) { for (l=0;l<3;++l) matr[i][j] = matr[i][j] + (matrix[i][l])*(matri[l][j]); } } printf("The resultant matrix is:\n\n"); for (i=0;i<3;++i) { for (j=0;j<3;++j) printf("%4d",matr[i][j]); printf("\n\n"); } }

4. 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

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

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 ternary 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 expressions 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.

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
Roll No. XXXXXXXXX

MC0061 - Computer Programming C Language

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 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.

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

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); }

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

For full assignments Contact us:

Prakash: 9686515230
Email: info@assignmentsclub.com / prm.01986@gmail.com Website: www.assignmentsclub.com

MC0061 - Computer Programming C Language

Roll No. XXXXXXXXX

Vous aimerez peut-être aussi