Computer Programming - I (24CP101T) Lecture Notes PDF

Document Details

UndauntedMagenta279

Uploaded by UndauntedMagenta279

Pandit Deendayal Energy University

Dr. Rajeev Kumar Gupta

Tags

computer programming C programming arrays programming fundamentals

Summary

These lecture notes cover introductory concepts in computer programming, focusing on C programming language. They detail topics including arrays, types of conversions, and conditional operators. The notes are presented for a computer programming course.

Full Transcript

Computer Programming – I (24CP101T) Dr. Rajeev Kumar Gupta Assistant Professor Pandit Deendayal Energy University Gandhinagar, Gujarat 1 Con...

Computer Programming – I (24CP101T) Dr. Rajeev Kumar Gupta Assistant Professor Pandit Deendayal Energy University Gandhinagar, Gujarat 1 Conditional or Ternary Operator  The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.  It is also known as the ternary operator in C as it operates on three operands. variable = (condition) ? Expression2 : Expression3; 113 int main() { int m = 5, n = 4; (m > n) ? printf("m is greater than n that is %d > %d", m, n) : printf("n is greater than m that is %d > %d", n, m); m is greater than n that is 5 > 4 return 0; } 114 Type Conversion/Type casting  Type conversion is used to convert variables from one data type to another data type.  Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator.  Syntax: (type_name) expression; Without Type Casting (implicit): 1. float f= 9/4; 2. printf("f : %d\n", f );//Output: 2 With Type Casting: (some information may lost 1. float f=(float) 9/4; 2. printf("f : %f\n", f );//Output: 2.250000 115 Rules for implicit type casting 1. char or short type operands will be converted to int during an operation and the outcome data type will also be int. 2. If an operand of type long double is present in the expression, then the corresponding operand will also be converted to long double same for the double data type. 3. If an operand of float type is present then the corresponding operand in the expression will also be converted to float type and the final result will also be float type. 4. If an operand of unsigned long int is present then the other operand will be converted to unsigned long int and the final result will also be unsigned long int. 5. If an operand of long int is present then the other operand will be converted to long int and the final result will also be long int. 6. If an operand of unsigned int is present then the other operand will be converted to unsigned int and the final result will also be unsigned int. 116 Unit 2: Derived Data types  Array: one dimensional and multidimensional array, Declaration, initialization, Array Manipulations. Matrix operations, String-Basic Concepts, Inbuilt String manipulation Functions, Pointer, Pointer arithmetic, Pointer to pointer, Array of Pointers 117 What are Arrays  C language provides a capability that enables the user to design a set of similar data types, called array. For understanding the arrays properly, let us consider the following program: main() { int x; x=5; x=10; x=20; x=35; printf("\nx=%d",x); } 118  Situations in which we would want to store more than one value at a time in a single variable.  For example, suppose we wish to arrange the percentage marks obtained by 100 students in ascending order.  In such a case we have two options to store these marks in memory: 1) Construct 100 variables to store percentage marks. 2) Construct one variable (called array or subscripted variable). 119 Formal definition of an array  An array is a collective name given to a group of ‘similar quantities’.  What is important is that the quantities must be ‘similar’.  per = {48, 88, 34, 23, 96 }  How to refer second number????  But in C, per,1 is index number.  index number-0 to size-1 120 121 An array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars 122 Array Declaration  Syntax: Data_type Array_Name[Array_Size]; Example: int marks; Here  int specifies the type of the variable.  marks specifies the name of the variable.  The number 30 tells how many elements of the type int will be in our array 123 124 Array Initialisation  Let us now see how to initialize an array while declaring it. int n[]={2,4,12,5,45,5}; // int n[]; it will give error int num={2,4,12,5,45,5}; int num={2,4,12,5}; when an array is partially initialized, the remaining elements 2,4,12,5, 0,0 are automatically set to 0. int n={2,4,12,5,45,5,75,8,67}; It gives error :too many initialisation or segmentation fault 125 Properties of Array  Each element of an array is of same data type and carries the same size, i.e., int = 4 bytes.  Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.  Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. 126 Entering Data into an Array  Here is the section of code that places data into an array: 0 67 65520 1 78 65522 Int marks []; 2 65524 69 for (i=0;i

Use Quizgecko on...
Browser
Browser