PDF Google Drive Downloader v1.1


Báo lỗi sự cố

Nội dung text ANS CPNM DEC 2019 - Vishnu Narayan V.pdf

ME305: COMPUTER PROGRAMMING AND NUMERICAL METHODS, DECEMBER 2019 ANSWER KEY PART A 1 a) All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data- type with which it is declared. Every data type requires a different amount of memory. 1) Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are:  Integer  Character  Boolean  Floating Point  Double Floating Point  Valueless or Void  Wide Character 2) Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely:  Function Array  Pointer  Reference 3).Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes:  Class
 Structure  Union  Enumeration  Typedef defined DataType primitive data types available in C++.  Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647. Syntax: int x; Size: 2  Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255. Syntax: char n;; Size: 1  Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool. Size: 1  Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space. Syntax: float x; Size: 4  Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space. Syntax: double x; Size: 8  void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value. Derived data types available in C++ Function: A function is a block of code or program-segment that is defined to perform a specific well-defined task. A function is generally defined to save the user from writing the same lines of code again and again for the same input. All the lines of code are put together inside a single function and this can be called anywhere required. main() is a default function that is defined in every program of C++. Syntax: FunctionType FunctionName(parameters) Array: An array is a collection of items stored at continuous memory locations. The idea of array is to represent many instances in one variable. DataType ArrayName[size_of_array];
Pointers: Pointers are symbolic representation of addresses. They enable programs to simulate call- by-reference as well as to create and manipulate dynamic data structures. It’s general declaration in C/C++ has the format: Syntax: datatype *var_name; Reference: When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. User defined data types available in C++ Class: The building block of C++ that leads to Object-Oriented programming is a Class. It is a user- defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. Structure: A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Syntax: struct address { char name[50]; char street[100]; char city[50]; char state[20]; int pin; }; Union: Like Structures, union is a user defined data type. In union, all members share the same memory location. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y. Enumeration: Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. Syntax: enum State {Working = 1, Failed = 0}; 1b) Differentiate between break and continue statements used in loops? Jump statements:break,continue In C++ There are two statements break; and continue; specifically to alter the normal flow of a program. Sometimes, it is desirable to skip the execution of a loop for a certain test condition or terminate it immediately without checking the condition. Syntax of break: break ; Syntax of continue: continue ; The continue is another jump statement like the break statement as both the statements skip over a part of the code. But the continue statement is somewhat different from break. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code between
#include // brek and continue; /*break will cause the control to go out of the present loop where it is; continue will cause the control to skip the rest of the statements the loop and continue with the next iteration of the loop */ /*void main() { for(int i =1;i<=15;i++) { if(i%3 == 0) continue;// for 3,6,9,12 it skips the rest and so r not printed cout<< i<< " "; if(i/6 ==2) break; //for 12 it is already skipped as above. for 13 the condition // true and so the loop is broken or exited } } */ 2) C++ Operators C++ is rich in built-in operators and provides the following types of operators:  Arithmetic Operators  Relational Operators  Logical Operators  Assignment Operators Arithmetic Operators There are following arithmetic operators supported by C++ language: Assume variable A holds Operator 10 and variable B holds Description 20, then: Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiplies both operands A * B will give 200 / Divides numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increment operator, increases integer value by one A++ will give 11 -- Decrement operator, decreases integer value by one A-- will give 9 Relational Operators

Tài liệu liên quan

x
Báo cáo lỗi download
Nội dung báo cáo



Chất lượng file Download bị lỗi:
Họ tên:
Email:
Bình luận
Trong quá trình tải gặp lỗi, sự cố,.. hoặc có thắc mắc gì vui lòng để lại bình luận dưới đây. Xin cảm ơn.