FACULTY: Ms. Saritha P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) SUBJECT / CODE: Programming in C and Data Structures- 15PCD13 What is token? What are the different types of tokens available in C Language? Explain. 1a) A token is the smallest element of a C Program. One or more character is grouped in sequence to form meaningful words. These meaningful words are called tokens. Tokens Keywords Operators Identifiers Special Symbols Constant Constant: Data items that do not change their values while the program executes, are called constants. The constant cannot be modified in the program. For example: 1, 3,2.55, z. Types of Constant: 1) Integer Constant: An integer is a whole number without any fractional part. 2) Real Constant: Real Constant is a fractional number. 3) Character Constant: A symbol enclosed within a pair of single quotes ( ) is called a character constant. Each character is associated with a unique value called an ASCII (American Standard Code for Information Interchange) code. ) String Constant: A sequence of characters enclosed within a pair of double quotes ( ) is called a string constant. The string always ends with NULL (denoted by \0) character. For example: 9, a, hello,
1b) \n. 5) Escape Sequence Characters: An escape sequence character begins with a backslash and is followed by one character. A backslash (\) along with some characters give rise to special print effects by changing the meaning of some characters. Identifier: As the name indicates, identifier is used to identify various entities of program such as variables, constants, functions etc. In other words, an identifier is a word consisting of sequence of --- Letters --- Digits or --- _ underscore Keywords: Keywords are those words, which have predefined meaning to the compiler. Each keyword has fixed meaning and that cannot be changed by user. Hence, they are also called reserved- words Keywords cannot be used as a variable or function. All keywords should be written in lower letters. Variable: A variable is an identifier whose value can be changed during execution of the program. The memory location name where the data can be stored. Define Algorithm. Develop an algorithm to compute the area of a circle given radius. An Algorithm is a step by step procedure to solve a given problem in finite number of steps by Accepting a set of inputs and Producing the desired output for the given problem. Example: Write an algorithm to add 2 numbers entered by user. Step 1: Start Step 2: Declare variables a, b and c. Step 3: Read values of a and b. Step : Add a and b and assign the result to c. C=a+b Step 5: Display c Step 6: Stop 2a) What are data types? Mention the different data types supported by C Language giving an
example to each. Data Types: The data type defines the types of data stored in a memory location. Character Data type: Keyword for character is char. It takes 1 byte in memory. Chars, signed and unsigned: Signed: When the variable is having signed like positive or negative. Unsigned: When the variable value is having no signed. A signed char is same as an ordinary char and has a range from -128 to 127; whereas, an unsigned char has a range from 0 to 255. Integer Data Type: Keyword for integer is int. Integer, long and short: Integer can also be classified as short int and long int. The intension of providing these variations is to provide integers with different ranges. Example: short int I; or short I; long int j; or long j; Integers, signed and unsigned: When the value stored in a given integer variable having positive or negative signed that comes under signed integer. Example: signed int I; When the variable is being used to only count things having no signed then it comes under unsigned integer. Example: unsigned int j; Floats and Doubles: A float occupies btyes in memory. If this is insufficient, then C offers a double data type that occupies 8 bytes in memory. If this is insufficient, then C offers a long double data type that occupies 10 bytes in memory. Example: float I; double j; long double f; Data Type Range Bytes Format signed char -128 to +127 1 %c unsigned char 0 to 255 1 %c short signed int -32768 to +32767 2 %d short unsigned int 0 to 65535 2 %u signed int -32768 to +32767 2 %d unsigned int 0 to 65535 2 %u long signed int -2178368 to +2178367 %ld
2b) long unsigned int 0 to 29967295 %lu Float -3.e38 to +3.e38 %f Double -1.7e308 to +1.7e308 8 %lf long double -1.7e932 to +1.7e932 10 %Lf Draw a flowchart to find the larger of two numbers. Start Input two numbers [a,b] True False Is (a>b)? Print a is largest Print b is largest Stop 3a) Explain the structure of C program with example. Preprocessor directive #include<stdio.h> int main() int main() declaration statement 1; int a, b, c; statement 2; printf( hello ); statement 3, return 0; Preprocessor directive: It is a program that processes our source program before it is passed to the compiler. Preprocessor commands (often known as directives). The preprocessor statement starts with symbol #. The normal preprocessor used in all programs is include. The #include directive instructs the preprocessor to include the specified file contents in the beginning of the program. main()
It is called the main function An entry point is where control enters a program or piece of code Every program must have main( ) since this is where the computer begins to execute the program The main() function is divided into 2 parts 1) Declaration Section: The variables that are used within the function main() should be declared in the declaration section only. The variables declared inside a function are called local variables. Ex int p, t, r,s; 2) Executable section: This contains the instruction given to the computer to perform a specific task. The task may be to (a) display message (b) read data (c ) add 2 numbers etc. Comments are portions of the code ignored by the computer. The comments allow the user to make simple notes in the source code. // this is an example for single line comment. /* this is an example for multiple line comment. */ 3b) a) Write a program to swap two numbers a and b without using third variable. #include<stdio.h void main() int a,b,c; clrscr(); printf("\nenter the values of a and b"); scanf("%d%d",&a,&b); printf("\nbefore Swaping the values of a=%d and b=%d",a,b); a=a+b; b=a-b; a=a-b; printf("\nafter Swaping the values of a=%d and b=%d",a,b); What do you mean by unary, binary and ternary operators? 1) Unary Operators: Unary operations operate on a single operand, therefore the number 5 when operated by unary will have the value 5. a) Addition or Unary plus ( +). example: a = +5 b) Subtraction or Unary Minus (-). Example: a= -7 c) Increment or Decrement operator (++, --). Example: If a=5 then ++a will be a=6, b= then b will be b=3. d) Logical Not (!). Example: A=1 then (! A) will be A=0 e) Bitwise Complement (~). Example: A=1001 then (~A) will be A=0110 f) Address of (&). Example: printf( %d,&a) will print the address of A g) Pointer dereference (*). Example: printf( %d,*a) will print the value at that address 2) Binary operators: A binary operator is an operator that operates on two operands
and manipulates them to return a result. Operators are represented by special characters or by keywords and provide an easy way to compare numerical values or character strings. Binary operators are presented in the form: Operand1 Operator Operand2. (a) Arithmetic Operators ( +,-,*,/,%) (b) Relational Operators (<,<=,>,>=,==,!=) (c) Logical Operators (&&, ) (d) Assignment Operators and shorthand assignment operators ( =,+=,-=,etc.) (e) Bitwise Operators (&,,^,<<,>>) 3) Ternary Operators: a ternary operator is an operator that takes three arguments. The arguments and result can be of different types. Many programming languages that use C-like syntax feature a ternary operator,?:, which defines a conditional expression. Exp1?Exp2:Exp3 If exp1 is true ans will be exp 2 otherwise ans will be exp3. b) 5a) Write a program to print the largest of three number using conditional operator #include<stdio.h> void main() int a,b,c,d,e; clrscr(); printf( enter the two numbers); scanf( %d%d%d,&a,&b,&c); d= (a>b)?a:b; e= (d>c)?d:c; printf( %d is largest,e); What is the purpose of a printf( ) statement? Explain the formatted printf ( ) along with the respective examples. The printf function does following tasks: Accept a series of arguments. Apply to each argument a format-specifier contained in the format-string. Output the formatted data to the screen. Syntax : printf ( "format string", list of variables ) ; The format string can contain: Characters that are simply printed as they are Format : printf( Format sting );
Example: printf( Hello World ); Output: Hello World printf( 12356!!!! ); Output: 12356!!!! printf( Let us C ); Output: Let us C Escape sequences that begin with a \ sign The escape sequence characters are also called as backslash character constants. These are used for formatting the output. Although it consists of two characters, it represents single character. Every combination starts with back slash(\) They are non-printing characters. It can also be expressed in terms of octal digits or hexadecimal sequence. Each escape sequence has unique ASCII value. Conversion specifications that begin with a % sign Format: printf( format string, list of variables); Example: printf( %d%c%f,&a,&b,&c); 5b) 6 Write a C program that computes the size of int, float, double and char variables. #include<stdio.h> Void main() printf( size of int %d,sizeof(int)); printf( size of float %d, sizeof(float)); printf( size of char %d, sizeof(char)); printf( size of double %d, sizeof(double)); Explain the Syntax of nested if statement. Write a C program to read a year as input and find whether it is a leap year or not. Also consider end of the centuries. Nested if statement is same like if statement, where new block of if statement is defined in existing if or block statement. The nested if statement in C programming language is used when multiple conditions need to be tested. The inner statement will execute only when outer if statement is true otherwise control won't even reach inner if statement. Syntax: if(condition 1) 8
statement1; if(condition 2) statement 2; statement 3; statement1; if(condition 2) statement 2; statement 3; Example: #include<stdio.h> void main() int a,b,c; printf("enter the values of a,b and c:"); scanf("%d%d%d",&a,&b,&c); if(a>b) // if statement contain if- statement if(a>c) printf(" a is larger "); // Means (c>a) printf(" c is larger "); // Means (b>a) if(b>c) printf(" b is larger "); // Means (c>b) printf(" c is larger "); // End of main() #include<stdio.h> int main() int year; printf("enter valid year\n"); scanf("%d",&year); if((year%==0) && (year%100!=0) (year%00 ==0)) //check for leap year printf("%d is leap year", year); printf("%d is not a leap year",year);
7a) int a, b; 2 float x; a=5, b=2; x=a/b; printf( %f, x); Implicit Conversion: When two operands in a binary exp are of different types, C automatically carries out implicit conversion If a compiler converts one type of data into another type of data automatically, it is known as implicit conversion. The conversion always takes place from lower rank to higher rank. For ex: int a =22, b=11; float c= a; //c becomes 21.000000. int a, b; 2.500000 float x; Explicit Conversion a=5, b=2; When the data of one type is converted x= ( float )a/b; explicitly to another type with the help of some pre defend functions, it is called as printf( %f, x); explicit conversion. There may be data loss in this process. Syntax : data_ type1 v1; data_type2 v2=(data_type2) v1; 7b) Write a program to find if the given integer odd or even without using modulo division operator. #include <stdio.h> #include <stdlib.h> void main() int a; printf("enter the number"); scanf("%d",&a); if( (a & 1)==0) printf("even"); printf("odd");
8a) What do you do you understand by Operator Precedence and Associativity. Each operator in c has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators of the same precedence are evaluated either from left to right or from right to left depending on the level. This is known as associativity property of an operator. Example: 8b) 9 What will be the output of the code Assuming x=5, y=0. If x=1, y=2, z=3 then if (x==0 x && y) if (! y) A= z + x * y / z y; printf( %d, A); z=1; z=2; z=3; printf( %d,z); Output: 1 Output: 3 Write a C program to do the operations - addition, subtraction, multiplication and division of two numbers using switch. #include<stdio.h> #include<stdlib.h> void main() int ch; float a,b,res; printf( Enter two numbers: ); scanf( %f%f,&a,&b); 8
printf( nmenun1.additionn2.subtractionn3.multiplicationn.division ); printf( nenter your choice: ); scanf( %d,&ch); switch(ch) case 1: res=a+b; break; case 2: res=ab; break; case 3: res=a*b; break; case : if (b==0) printf( Invalid number ); exit(0); res=a/b; break; default: printf( Wrong choice!!npress any key ); printf( nresult=%f,res); Design and develop a flowchart that takes three coefficients (a, b, and c) of a Quadratic equation 8 10 (ax2+bx+c=0) as input and compute all possible roots. Implement a C program for the developed flowchart and execute the same to output the possible roots for a given set of coefficients with appropriate messages. #include<stdio.h> #include<math.h> int main() float a, b, c, disc; float root1,root2,real,imag; printf("enter a,b,c values\n"); scanf("%f%f%f",&a,&b,&c); if( (a == 0) && (b == 0) &&(c==0)) printf("invalid coefficients\n"); printf(" Try Again with valid inputs!!!!\n"); disc = b*b - *a*c; if(disc == 0) printf("the roots are real and equal\n"); root1 = root2 = -b/(2*a); printf("root1 = %.3f \nroot2 = %.3f", root1,root2); if(disc>0)
printf("the roots are Real and Distinct\n"); root1 = (-b+sqrt(disc)) / (2*a); root2 = (-b-sqrt(disc)) / (2*a); printf("root1 = %.3f \nroot2 = %.3f",root1,root2); printf("the roots are Real and Imaginary\n"); real = -b / (2*a); imag = sqrt(fabs(disc)) / (2*a); //fabs() returns only number ignoring sign printf("root1 = %.3f + i %.3f \n",real,imag); printf("root2 = %.3f - i %.3f",real,imag); Flowchart:
start Read T Is F Is a=0 T Print & a=0 Invalidroots b=0? F Disc =b*b-*a*c Is disc F Is disc F T T Print linear equation Print Real Print Real Print & equal and distinct imaginary Root 1 = -c/b Root 1 = -b/(2*a) Root 2 = root 1 Root 1 = (-b+sqrt(disc))/(2*a) Root 1 = (-b-sqrt(disc))/(2*a) Real = -b/(2*a) Image = sqrt(f abs(disc))/(2*a) Print root1 Print root 1, root 2 Print Root 1 = real+ I imag stop