PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Similar documents
ONE DIMENSIONAL ARRAYS

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Unit 4. Input/Output Functions

Multiple Choice Questions ( 1 mark)

C-LANGUAGE CURRICULAM

UNIT-I Input/ Output functions and other library functions

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

Model Viva Questions for Programming in C lab

Basic Elements of C. Staff Incharge: S.Sasirekha

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Unit 7. Functions. Need of User Defined Functions

Basics of Programming

Write a C program using arrays and structure

Questions Bank. 14) State any four advantages of using flow-chart

UNIT- 3 Introduction to C++

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter 2 - Introduction to C Programming

ARRAYS(II Unit Part II)

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Data Types and Variables in C language

Fundamentals of Computer Programming Using C

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

A Fast Review of C Essentials Part I

Lecture 3. More About C

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

INTRODUCTION 1 AND REVIEW

Fundamentals of Programming Session 4

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

3. Types of Algorithmic and Program Instructions

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Operators and Control Flow. CS449 Fall 2017

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Variables and literals

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CS1100 Introduction to Programming

Unit 3 Decision making, Looping and Arrays

1 Lexical Considerations

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Programming for Engineers Introduction to C

C - Basic Introduction

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 8 Character Arrays and Strings

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Course organization. Course introduction ( Week 1)

15 FUNCTIONS IN C 15.1 INTRODUCTION

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Course Outline Introduction to C-Programming

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

'C' Programming Language

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Chapter 2 Basic Elements of C++

File Handling in C. EECS 2031 Fall October 27, 2014

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

INDORE INDIRA SCHOOL OF CAREER STUDIES C LANGUAGE Class B.Sc. - IIND Sem

Should you know scanf and printf?

C mini reference. 5 Binary numbers 12

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Chapter 8: Character & String. In this chapter, you ll learn about;

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Chapter 2, Part I Introduction to C Programming

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Lexical Considerations

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 10. Arrays and Strings

Compiling and Running a C Program in Unix

BLM2031 Structured Programming. Zeyneb KURT

Arrays, Strings, & Pointers

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Q 1. Attempt any TEN of the following:

Computers Programming Course 5. Iulian Năstac

CSCI 171 Chapter Outlines

UNIT 3 OPERATORS. [Marks- 12]

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Visual C# Instructor s Manual Table of Contents

ANSI C Programming Simple Programs

EC 413 Computer Organization

Java+- Language Reference Manual

Review of the C Programming Language for Principles of Operating Systems

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

2 nd Week Lecture Notes

Subject: Fundamental of Computer Programming 2068

UIC. C Programming Primer. Bharathidasan University

Lexical Considerations

Dept. of CSE, IIT KGP

Data types, variables, constants

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Module 6: Array in C

Transcription:

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo 1. (a)what is an algorithm? Draw a flowchart to print N terms of Fibonacci series, where N is greater than or equal to 1 [10] A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem this sequence of steps is called an algorithm Implementation phase implement the program in some programming language (b) Explain user defined data types in C [4] C supports feature known aas type definition that allows users to define an identifier that would represent an existing datatype. It takes the general form Typedef type identifier Eg- typedef int units Another user defined datatype is enumerated datatype provided by ANSI standard.it si defined as follows Enum identifier{value1,value2,value3..,value n; The identifier is a user defined enumerated datatype which can be used to declare variables that can have one of the values encoded within the braces known as enumeration constants.

2(a) Briefly write about storage classes [6] Variables in C can have not only data type but also storage class that provides information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized. There are four storage classes: auto extern register static auto, the default, is most common. When a block is entered, memory is allocated for automatic variables. They are defined and are local to the block. When the block is exited, the system releases the memory that was allocated to the auto variables, and their values are lost. You can explicitly write auto int variable_name; in the declaration, but there s no point as it s implicitly there anyway. extern variables are defined outside blocks and functions, and are global to all blocks and functions that follow. Example: #include <stdio.h> int a = 1, b = 2; /* global variables */ int main (void) { int b = 5; /* local redefinition */ printf( %d, a+b);/* 6 is printed */ return 0; All functions are of type extern. register tells the compiler that the variables should be stored in high-speed memory registers if possible. Only a few such registers are available, and can be assigned to frequently-accessed variables if execution time is a problem. If no register space is free, variables become auto instead. Keep registers in small local blocks which are deallocated quickly static variables are local variables that keep their previous value when the block is reentered. A declaration static int cnt = 0; will set cnt to zero the first time the function is used; thereafter, it will retain its value from previous iterations. This can be useful, e.g., for debugging: you can insert code like this anywhere without interfering with the rest of the program { /* debugging starts here */ static int cnt = 0; printf( *** debug: cnt = %d, v = %d\n, ++cnt, v); The variable cnt is local to the block and won t interfere with another variable of the same name elsewhere in an outer block; it just increases by one every time this block is encountered.

(b) Explain the basic types of constants in C [4] Integer constant can be specified in decimal, octal, and hexadecimal.1234 is a decimal integer constant, 037 which has prefix 0 is an octal integer constant, and 0x1f or 0x1F which has prefix 0x is a hexadecimal integer constant. char constant is an integer, written as one character within single quotes,such as x, including escape sequences like \n. These escape sequences look like two characters, but represent only one. A string constant is a sequence of zero or more characters surrounded by double quotes, as in it is a string, or /* the empty string */. The quotes are not the part of the string. 3 (a) Explain the formatted input/output functions in C with examples [6] Formatted input The general form of scanf is: scanf("control string", arg1,arg2,...,argn); The control string specifies the field format in which the data is to be entered and the arguments arg1, arg2,..., argn specify the address of locations where the data is stored. Control string and arguments are separated by commas Control string contains field specifications, which direct the interpretation of input data. It may include: 1. Field (or format) specifications, consisting of the conversion character %, a data type character (or type specifier), and an optional number, specifying the field width. 2. Blanks, tabs, or newlines. Blanks, tabs and newlines are ignored. The data type character indicates the type of data that is to be assigned to the variable associated with the corresponding argument. The field width specifier is optional. The field specification for reading an integer number is: %wd Following are the specifications for reading character strings: %ws or %wc Formatted output The general form of printf statement is printf("control string",arg1,agr2,...,argn); String consists of three types of items: 1. Characters that will be printed on the screen as they appear. 2. Format specifications that define the output format for display of each item. 3. Escape sequence characters such as \n, \t, and \b. The control string indicates how many arguments follow and what their types are. The arguments arg1, arg2,..., argn are the variables whose values are formatted and printed according to the

specifications of the control string. The arguments should match in number, order and type with the format specifications. The format specification for printing an integer number is %wd The output of a real number may be displayed in decimal notation using the following format specification: %w.p f A single character can be displayed in a desired position using the format %wc The format specification for outputting strings is similar to that of real numbers. It is of the form %w.ps (b) Find the output of the following if [4] char text[] = Programming in C is challenging and creative activity! (i) printf( 20%s,text ); (ii) printf( 20.10%s,text); (ii) (iii) printf( -20.10%s,text); (iv) printf( -20%s,text); 1) Programming in C is challenging and creative activity! 2) Programmin 3) Programmin 4) Programming in C is challenging and creative activity! 4 Explain loop construct in programming. Briefly discuss the different loop constructs available in C [10] The while loop Executes a block of statements as long as a specified condition is TRUE. The general while loop construct, while (condition) statement(s); next_statement; The (condition) may be any valid C expression. The statement(s) may be either a single or a compound (a block of code) C statement. When while statement encountered, the following events occur: 1. The (condition) is evaluated. 2. If (condition) evaluates to FALSE (zero), the while loop terminates and execution passes to the next_statement. 3. If (condition) evaluates as TRUE (non zero), the C statement(s) is executed. Then, the execution returns to step number 1 until condition becomes FALSE The do-while loop Executes a block of statements as long as a specified condition is true at least once. Test the condition at the end of the loop rather than at the beginning, as demonstrated by the for and while loops. The do-while loop construct is, do statement(s); while (condition) next_statement;

(condition) can be any valid C expression. statement(s) can be either a single or compound (a block of code) C statement. When the program encounter the do-while loop, the following events occur: The statement(s) are executed. The (condition) is evaluated. If it is TRUE, execution returns to step number 1. If it is FALSE, the loop terminates and the next_statement is executed. This means the statement(s) in the do-while will be executed at least once The for statement Executes a code block for a certain number of times. The code block may have no statement, one statement or more. The for statement causes the for loop to be executed in a fixed number of times. The following is the for statement form, for(initial_value;condition(s);increment/decrement) statement(s); next_statement; initial_value, condition(s) and increment/decrement are any valid C expressions. The statement(s) may be a single or compound C statement (a block of code). When for statement is encountered during program execution, the following events occurs: The initial_value is evaluated e.g. intnum = 1. Then the condition(s) is evaluated, typically a relational expression. If condition(s) evaluates to FALSE (zero), the for statement terminates and execution passes to next_statement. If condition(s) evaluates as TRUE (non zero), the statement(s) is executed. Next, increment/decrement is executed, and execution returns to step no. 2 until condition(s) becomes FALSE. 5 (a) What are arrays? How are they declared and initialized in C [4] An array is a sequenced collection of related data items that share a common name. For instance, we can use an array name salary to represent a set of salaries of a group of employees in an organization. We can refer to the individual salaries by writing a number called index or subscript in brackets after the array name. For example, salary[10] represents the salary of 11th employee. While the complete set of values is referred to as an array, individual values are called elements. The types of array are One-dimensional arrays Two-dimensional arrays Multidimensional arrays a)declaration of one-dimensional arrays type variable-name[size]; initialization- type array-name[size]={list of values; (explain in detail) b)two-dimensional arrays are declared as follows: type array_name [row_size][column_size]; intilization - int table[2][3]={0,0,0,1,1,1,;/ int table[2][3]={{0,0,0,{1,1,1; c) The general form of a multi-dimensional array is

type array_name[s1][s2][s3]...[sm]; where s i is the size of the ith dimension. Some example are: int survey [3][5][12]; float table [5][4][5][3]; (b) Write a program to find the smallest element in an array [7] int main() { int a[30], i, num, smallest; printf("\nenter no of elements :"); scanf("%d", &num); for (i = 0; i < num; i++) scanf("%d", &a[i]); smallest = a[0]; for (i = 0; i < num; i++) { if (a[i] < smallest) { smallest = a[i]; printf("\nsmallest Element : %d", smallest); return (0); 6 Explain the various operators in C with suitable examples [10] C operators can be classified into a number of categories. (Explain in detail each one ) 1. Arithmetic operators. 2. Relational operators. 3. Logical operators. 4. Assignment operators. 5. Increment and decrement operators. 6. Conditional operators. 7. Bitwise operators. 8. Special operators 7 (a) What is a string? Write a program that reads a string from the keyboard and determines whether the string is palindrome or not [6] A string is a sequence of characters that is treated as a single data item. Any group of characters defined between double quotation marks is a string constant. int main(){ char string1[20]; int i, length; int flag = 0; printf("enter a string:"); scanf("%s", string1); length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i]!= string1[length-i-1]){ flag = 1; break;

if (flag) { printf("%s is not a palindrome", string1); else { printf("%s is a palindrome", string1); return 0; (b) Explain any four string handling functions, with examples [4] Following are the most commonly used string-handling functions. Function strcat() Action concatenates two strings strcmp() compares two strings strcpy() strlen() copies one string over another finds the length of a string The strcat function joins two strings together. It takes the following form: strcat(string1,string2); string1 and string2 are character arrays. When the function strcat is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. The string at string2 remains unchanged. The strcmp function compares two strings identified by the arguments and has a value 0 if they are equal. If they are not, it has the numeric difference between the first nonmatching characters in the strings. It takes the form: strcmp(string1,string2); The strcpy function works almost like a string-assignment operator. It takes the form strcpy(string1,string2); and assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. This function counts and returns the number of characters in a string. It takes the form n=strlen(string); Where n is an integer variable, which receives the value of the length of the string. The argument may be a string constant. The counting ends at the first null character. 8 (a) Define user-defined functions? Brief the categories of functions [7] A large program in c can be divided to many subprogram

The subprogram posses a self contain components and have well define purpose.the subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main(). Function can be divided into 4 categories: (explan in detail) 1. A function with no arguments and no return value 2. A function with no arguments and a return value 3. A function with an argument or arguments and returning no value 4. A function with arguments and returning a values (b) Given the following declaration int x=10,y=10; int * p1=&x; int *p2 = &y; What is the value of the following expressions i) (*p1)++; ii) - - (*p2); iii) *p1+ (*p2) - - [3] i)10 ii) 8 iii)20 *******************************