Document Details

Uploaded by Deleted User

Tags

C++ programming arrays data structures computer science

Summary

This document provides an overview of compound data types, particularly arrays, in C++. It includes foundational concepts like array declaration, initialization, and accessing elements. The document also touches upon dynamic arrays, structures, and strings.

Full Transcript

Lecture - 4 Compound Data Types Outline ▪ Arrays ▪ Character Sequence ▪ Pointers ▪ Dynamic Memory ▪ Data Structures 2 Introduction An array is a collection of elements of the same data type stored in contiguo...

Lecture - 4 Compound Data Types Outline ▪ Arrays ▪ Character Sequence ▪ Pointers ▪ Dynamic Memory ▪ Data Structures 2 Introduction An array is a collection of elements of the same data type stored in contiguous memory locations. Array is a kind of data structure that can store fixed size(finite) sequential collection of homogeneous element. C++ supports the following data types: ✓Primary or Built-in or Fundamental data type ✓Derived data types ✓User-defined data types 3 …continued Compound data types (a.k.a composite data types) are data types that can be constructed from fundamental data types (other compound data types). Each compound data type has its own unique properties as well. syntax data_type array_name[size]; Example: 4 int numbers; // Declares an integer array with 5 elements. …continued …continued Some invalid array declaration: int v ; float v[0.5]; Float v[-90]; Char v[$]; Array Initialization: Data-type array-name[expression]={elemnt1,elmnt2,…elm-n}; Eg:- int num={0,1,2,3,4}; Float v={0.5,1,1.5,-4,}; Char sex={‘M’ , ’F’}; Char name={‘R’ , ‘ a ’ , ‘ v ’ , ‘i’ , ‘ c ’}; 6 Accessing array elements How can we insert lists of arrays ? How can we display the inserted arrays? Coping Arrays: const int SIZE=10 int x [SIZE] ; int y [SIZE] ; for (int i = 0 ; i < SIZE; i++) // Loop to do copying, one element at a time x[i] = y[i]; 7 Introduction to Array For example 1 , an array to contain 5 integer values of type int called foo could be represented like this where each blank panel represents an element of the array. These elements are numbered from 0 to 4, with 0 being the first while 4 being the last. In C++, the index of the first array element is always zero. 2 Introduction to Array For example : Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate variables, we can simply create an array. Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is: Type array_Name [ size ]; typical declaration for the above examples: ✓example 1 => int foo; ✓example 2 => double grade; 2 Types of Array One Dimensional Array Multidimensional arrays Types 1 0 11 One Dimensional Array It stores elements in a single dimension. And, in this array a single specification is required to describe elements of the array. The diagram below shows that it arranged all the elements in row wise in a single dimension, one after other. Below is illustration of array. 12 One Dimensional Array It is a collection of same data types. 1-D array is declared as: Syntax: data_type variable_name[size] ✓ data_type is the type of array, like int, float, char, etc. ✓ variable_name is the name of the array. ✓ size is the length of the array which is fixed. The Size must be an integer constant greater than zero. Note: The location of the array elements depends upon the data 2 type we use. 13 Example ❖int A; An array of ten integers ❖Char str; ▪An array of twenty characters. ❖ int a[ 100 ], b[ 27 ] ; ▪Defining multiple arrays of same type 2 Array Initialization: We can explicitly initialize arrays at the time of declaration. Syntax: data_type array_name[size]={value1, value2,……..valueN}; Value1, value2, valueN are the constant values known as initializers, which are assigned to the array elements one after another. ▪Example: ▪Define an array temperature of 5 elements contains float numbers , and Initialize it with these numbers : 12.3 , 7.5 , 65 , 72.1, 87.5. 2 15 Array Initialization: double temperature = {12.3 , 7.5 , 65 , 72.1, 87.5 }; temperature 12.3 temperature 7.5 temperature 65.0 Elements temperature 72.1 temperature [ 4 ] 87.5 Index 16 Array Initialization: int N[ ] = { 1, 2, 3, 4, 5 }; In 1-D arrays it is optional to specify the size of the array. If size is omitted during initialization then the compiler assumes the size of array equal to the number of initializers. int N = { 0 } ; // the first element of the array is being initialized to 0. int B = {2, 4, 8, 16, 32}; Unspecified elements are guaranteed to be zero. If not enough initializers, rightmost elements become 0. int C = {2, 4, 8, 16, 32}; Error — compiler detects too many initial values. 17 Accessing Array Element: ❖An individual element within array is accessed by use of a subscript (index) that describes the position of an element in the array , it must be an integer or integer expression. We can’t copy the elements of one array to another array by simply assigning it. Example: int a={9,8,7,6,5}; and int b; b=a; //not valid we have to copy all the elements by using for loop. Sizeof operator used to show memory allocation of the given Array element using Sizeof(array name); 18 Accessing Array Element Example: The symbol table for the program Variable Name Address Value a Out put a 0x7ffe58e7cc4 7 19 Array Initialization: Const int SIZE=10 x = y ; // Error - Illegal int x [SIZE] ; int y [SIZE] ; Only individual elements can be assigned to using the index operator, e.g., x = y; To make all elements in 'x' the same as those in 'y' (equivalent to assignment), a loop has to be used. for (int i = 0 ; i < SIZE; i++) // Loop to do copying, one element at a time x[i] = y[i]; This code will copy the elements of array y into x, overwriting the original contents of x. 20 Array Initialization: 1. Write a c++ program that display the elements of the following array Double marks={50,90,30,100,78,68}; 2. Write a c++ program that initialize elements for the following array from the user, and display the values/elements. double scores; 3. Write a c++ Program to Increment every Element of the Array by one & Print Incremented Array for the following array. int array = {100, 200, 300, 400}; 4.Write a c++ Program that display the sum of the following array elements int array = {100, 200, 300, 400}; 5. Write a c++ Program to search the element is found in the given array element 6. Write a c++ Program to revers the given array element 7. Write a c++ program to sort the given array element either in ascending or descending order 21 Array Initialization: Q? Write a c++ program that display the maximum value from the array elements. Q? At what index is the maximum located 22 Multidimensional arrays ▪ A multidimensional array is an array with more than one dimension. ▪ It is the homogeneous collection of items where each element is accessed using multiple indices. ▪ A 2D array also falls under the category of a multidimensional array. ▪ MD array can have any number of dimensions. Data type array_name [size 1][size2]….[size n] where, Data_type: Type of data to be stored in the array. Array_Name: Name of the array. size1, size2,…, sizeN: Size of each dimension. 23 Multidimensional arrays The size of an array is equal to the size of the data type multiplied by the total number of elements that can be stored in an array. We can calculate the total number of elements in an array by multiplying the size of each dimension of a multidimensional array. Here size1, size2 up to sizeN describe the number of dimensions. type array_Name [ x ][ y ]; Int array // two dimensional array int array // three dimensional array 24 Two-dimensional array Example: int a; Reading values in a for(int i=0;ia[i][j]; Displaying values of a for(int i=0;i

Use Quizgecko on...
Browser
Browser