MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Similar documents
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Computers Programming Course 6. Iulian Năstac

IV Unit Second Part STRUCTURES

Subject: Fundamental of Computer Programming 2068

A flow chart is a graphical or symbolic representation of a process.

Operators in C. Staff Incharge: S.Sasirekha

UNIT - V STRUCTURES AND UNIONS

Computers Programming Course 5. Iulian Năstac

Object Oriented Pragramming (22316)

Dynamic Memory Allocation

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

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

3.3 Structures. Department of CSE

ESC101N: Fundamentals of Computing End-sem st semester

Bit Manipulation in C

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Prepared by: Shraddha Modi

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

C library = Header files + Reserved words + main method

Q 1. Attempt any TEN of the following:


Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Array. Arrays. Declaring Arrays. Using Arrays

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

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

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Pointers and Arrays 1

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

OBJECT ORIENTED PROGRAMMING

High Performance Computing in C and C++

COMPUTER APPLICATION

Fundamental of Programming (C)


Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Lecture 02 C FUNDAMENTALS

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

Type Checking. Prof. James L. Frankel Harvard University

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

PROGRAMMAZIONE I A.A. 2017/2018

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

Fundamentals of Programming

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

A Fast Review of C Essentials Part I

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C: How to Program. Week /Mar/05

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Presented By : Gaurav Juneja

Solutions to Assessment

Computers Programming Course 7. Iulian Năstac

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

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

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Dynamic memory allocation

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Programming Multiple. Choice

Unit 3 Decision making, Looping and Arrays

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Self-referential Structures and Linked List. Programming and Data Structure 1

Language comparison. C has pointers. Java has references. C++ has pointers and references

Multi-Dimensional arrays

SAE1A Programming in C. Unit : I - V

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Arrays, Pointers and Memory Management

Dynamic memory allocation (malloc)

Chapter 2: Introduction to C++

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Variation of Pointers

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Fundamental of Programming (C)

DC54 DATA STRUCTURES DEC 2014

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Kurt Schmidt. October 30, 2018

Pointers. Introduction

Unit IV & V Previous Papers 1 mark Answers

C-Programming. Claude Fuhrer 5 août 2017

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CCE RR REVISED & UN-REVISED KARNATAKA SECONDARY EDUCATION EXAMINATION BOARD, MALLESWARAM, BANGALORE G È.G È.G È..

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Programming for Engineers Arrays

UNIT 3 OPERATORS. [Marks- 12]

Lesson 7. Reading and Writing a.k.a. Input and Output

Introduction to C. Systems Programming Concepts

Advanced C Programming and Introduction to Data Structures

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Transcription:

Summer 2012 MCA Semester 1 4 Credits Assignment: Set 1 (40 Marks) Q1. 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. Answer: 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. 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 ^ Bitwise Exclusive Example, << Shift left >> Shift right 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) 0 1 0 1 0 1 1 0 & 0 0 1 1 0 0 1 0 -------------------- 0 0 0 1 0 0 1 0 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);

Q2. Explain the following with a programming example for each: a. Arrays of Structures b. Unions Answer: 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;

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); 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; 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). Example: #include <stdio.h> #include <conio.h> Void main () Union Int I; Float f; Double d; u; Printf ( %d, sizeof (u)); u.i = 100; Printf ( %d%f%f, u.i, u.f, u.d); u.f = 0.5; Printf ( %d%f%f, u.i, u.f, u.d); u.d = 0.0166667; Printf ( %d%f%f, u.i, u.f, u.d);

Q3. Write a program demonstrating the usage of pointers with one dimensional and two dimensional arrays. Answer: 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. 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 I th element in a 1-D array is - *(pointer name + I) Formula for accessing I th row, J th 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. An example program which shows how to access 2-D array using pointer : #include <stdio.h> #include <stdlib.h> main() long mat[5][5], **ptr; 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

Q4. 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 Remaining answers are available in the full assignments. For full assignments contact us: Global Education Rajdeep: 098662 48187 / 077958 40110 Email: support@smuassignments.com / global.education_smu@yahoo.in Website: www.smuassignments.com Note: The diagonally marked watermark (Sample Assignment of smuassignments.com) will not appear in the paid assignments.