Computer Programming Unit-3 PDF

Summary

This PDF document provides information on arrays and strings in computer programming. It covers concepts such as declaring, initializing, and accessing arrays and strings in C programming, along with examples.

Full Transcript

10/20/2024 Array:  What is array?  How to read array elements?  Declaration  Initialization  Array operations Dr. Ujjaval Patel #3110003 (PPS) – Array and String 1 1 Need of Array Variable  Suppose we need to store rollno of...

10/20/2024 Array:  What is array?  How to read array elements?  Declaration  Initialization  Array operations Dr. Ujjaval Patel #3110003 (PPS) – Array and String 1 1 Need of Array Variable  Suppose we need to store rollno of the student in the integer variable. Declaration int rollno;  Now we need to store rollno of 100 students. Declaration int rollno101, rollno102, rollno103, rollno104...;  This is not appropriate to declare these many integer variables. e.g. 100 integer variables for rollno.  Solution to declare and store multiple variables of similar type is an array.  An array is a variable that can store multiple values. Dr. Ujjaval Patel #3110003 (PPS) – Array and String 2 2 1 10/20/2024 Definition: Array  An array is a fixed size sequential collection of elements of same data type grouped under single variable name. … int rollno; Fixed Size Sequential Same Data type Single Name Here, the size of an It is indexed to 0 to 99 All the elements (0-99) All the elements (0-99) array is 100 (fixed) to in sequence will be integer will be referred as a store rollno variables common name rollno Dr. Ujjaval Patel #3110003 (PPS) – Array and String 3 3 Declaring an array Syntax  By default array index starts data-type variable-name[size]; with 0.  If we declare an array of size 5 then its index ranges from Integer Array 0 to 4. int mark;  First element will be store at mark and last element integer will be stored at mark not mark. Float Array  Like integer and float array float avg; we can declare array of type char. float Dr. Ujjaval Patel #3110003 (PPS) – Array and String 4 4 2 10/20/2024 Compile time Initialing and Accessing an Array Declaring, initializing and accessing single integer variable int mark=90; //variable mark is initialized with value 90 printf("%d",mark); //mark value printed Declaring, initializing and accessing integer array variable int mark={85,75,76,55,45}; //mark is initialized with 5 values printf("%d",mark); //prints 85 printf("%d",mark); //prints 75 printf("%d",mark); //prints 65 printf("%d",mark); //prints 55 printf("%d",mark); //prints 45 mark 85 75 65 55 45 Dr. Ujjaval Patel #3110003 (PPS) – Array and String 5 5 Run time initialization Reading array without loop Reading array using loop 1 void main() 1 void main() 2 { 2 { 3 int mark; 3 int mark,i; printf("Enter array element="); for(i=0;i

Use Quizgecko on...
Browser
Browser