Structured data type

27
STRUCTURED DATA TYPE: ARRAY BY OMKAR MAJUKAR XI-A

Transcript of Structured data type

Page 1: Structured data type

STRUCTURED DATA TYPE:

ARRAYBY

OMKAR MAJUKARXI-A

Page 2: Structured data type

ARRAY An array is collection of variable of

same datatype that are referenced by a common name.

Types of arrays- 1) SINGLE DIMENSIONAL ARRAYS. 2) TWO DIMENSIONAL ARRAYS.

Page 3: Structured data type

SINGLE DIMENSIONAL

ARRAYS The simplest form of an array is a single dimentional array.

The array is given a name and its elements are referred to by their subscripts or indices.

C++ array’s index numbering starts with 0.

Page 4: Structured data type

BASE DATATYPE The data type of array elements is

known as the base type of the array.

type array-name[size]; The above statement declared

array marks has 50 elements, marks[0] to marks[49].

Page 5: Structured data type

EXAMPLE-#include<iostream.h>void main(){ const int size = 50; float marks[size]; for(int i=0; i<size; i++) { cout<<“Enter marks of students”<<i+1<<“\n”; cin>>marks[i]; } cout<<\n”; for(i=0; i<size; i++) cout<<“marks[“<<i<<“]=“<<marks[i]<<“\n”; return 0;}

Page 6: Structured data type

OUTPUT-Enter marks of student189Enter marks of student298Enter marks of student388Marks[0] = 89Marks[1] = 98Marks[2] = 88

Page 7: Structured data type

MEMORY REPRESENTATION OF SINGLE DIMENSIONAL

ARRAYS Single dimension arrays are essentially lists of

same information of the same type and their elements are stored in contiguous memory location in their index order.

For example, an array grade of type char with 8 declared as

char grade[8]; will have element grade[0] at 1st allocated memory

location, grade[1] at the next, and so forth.

Page 8: Structured data type

Since grade is a char type array, each element size of 1 byte(a character size is 1 byte) and it will be represented in the memory as shown below-

GRADE

ADDRESS 2000 2001 2002 2003 2004 2005 2006 2007

*An 8 element character array beginning at location 2000 .

Total bytes = size of base datatype * no. of elements

Grade[0]

Grade[1]

Grade[2]

Grade[3]

Grade[4]

Grade[5]

Grade[6]

Grade[7]

Page 9: Structured data type

ARRAY TRAVERSAL Traversal- Accessing each element of array. Example-#include<iostream.h>Void main(){ float PER[10]; int c; for(c=0; c<10; c++) { cout<<“Enter percentage of student”<<c+1; cin>>PER[c]; } cout<<endl<<“Contents of array”<<endl; for(c=0; c<10; ++c) { cout<<“Percentage of student”<<c+1<<“=“<<PER[c]<<endl; }}

Page 10: Structured data type

SEARCHING FOR AN ELEMENT IN A 1-D

ARRAY Sometimes we need to search an

element in an array. This can be only done by comparing the

search-item with each and every element of the array.

This process for searching for an element in an array is called linear search.

Page 11: Structured data type

EXAMPLE-#include<iostream.h>void main(){ const int maxsize = 10; int ARR[maxsize],srch,ndx,found=0; cout<<endl<<“Enter array values”<<endl; for(int c=0; c<maxsize; ++c) cin>>ARR[c]; cout<<“Enter the value to be searched”; cin>>srch; for(c=0; c<maxsize; ++c) { if(ARR[c]==srch) { found=1; ndx=c; break; } } if(found==1) cout<<“First occurance is at index”<<ndx; elseCout<<“Value not found in the array”; }

Page 12: Structured data type

CHARACTER ARRAY-STRINGS

C++ stores sequence of characters I,e string into character arrays.

In character arrays arrays C++ puts a special character after the last character within the string to mark the end of string.

The special character is known as null character or \0.

For reading spaces within a string gets() or getline() methods should be used and for reading one word only cin can be used.

Page 13: Structured data type

EXAMPLE-#include<iostream.h>#include<stdio.h>#include<ctype.h>void main(){ char st[50]; int c; cout<<“Enter a string of characters”; gets(st); for(c=0; st[c]!=‘\0’; c++){ if(c%2==0) st[c]=toupper(st[c]); else st[c]=tolower(st[c]);}cout<<“Converted string = “<<st;}

Page 14: Structured data type

2 DIMENSIONAL ARRAYS

A 2-D array is an array in which each element is itself an array.

For instance, an array A[m][n] is an M by N table with M rows and N columns containing M*N elements.

No. of elements in 2-D array = No. of rows * No. of columns

The simplest form of a multi dimensioned array is 2-D arrays, is an array having single dimensioned array as its elements.

The general form of the 2-D array declaration is type array-name[rows][columns]

Page 15: Structured data type

PROCESSING 2-D ARRAYS

To read or process a 2-D array, you need to use nested loops.

One loop processes the rows and other the columns.

If outer loop is for rows and inner loop is for columns, then for each row index, all columns are processed and then the same process is repeated for next row index.

Page 16: Structured data type

EXAMPLE-#include<iostream.h>void main(){ float marks[4][3],sum,avg; char grade[4]; int i,j; for(i=0; i<4; i++) { sum = avg = 0; cout<<“Enter 3 scores of student”<<i+1<<“:”; for(j=0; j<3; j++) { cin>>marks[i][j]; sum+=marks[i][j]; } avg = sum\3; if(avg<45.0) grade[i] = ‘D’; else if(avg<60.0) grade[i] = ‘C’; else if(avg<75.0) grade[i] = ‘B’; else grade[i] = ‘A’; } for(i=0; i<4; i++) { cout<<“student”<<i+1<<“\tTotal Marks = “; cout<<marks[i][0] + marks[i][2] cout<<“\tGrade=“<<grade[i]<<“\n”; } return 0; }

Page 17: Structured data type

OUTPUT-Enter 3 scores of student 1 : 78 65 46Enter 3 scores of student 2 : 56 65 66Enter 3 scores of student 3: 90 89 92Enter 3 scores of student 4: 45 56 43Student 1 Total Marks = 189 Grade = BStudent 2 Total Marks = 187 Grade = BStudent 3 Total Marks = 271 Grade = AStudent 4 Total Marks = 144 Grade = C

Page 18: Structured data type

MEMORY REPRESENTATION OF 2 DIMENSIONAL ARRAYS

Two dimensional arrays are stored in a row-column matrix, where the first index indicates the row and second indicates the column.

This means the second index changes faster than the first index when accessing the elements in the array in the order in which they are actually stored in memory.

Page 19: Structured data type

0 1 2 3 4 5 0 1 2 3 4*A two dimensional array pay[5][7] in

memory. Total bytes=no. of rows*no. of

columns*size of base data type.

Pay[2][3]

Pay[3][1]

Page 20: Structured data type

MATRICES AS 2-D ARRAYS

Matrix is a useful concept of mathematics.

A matrix is a set of mn numbers arranged in the form of a rectangular array of m rows and n columns.

Such a matrix is called m*n(m by n) matrix.

Matrices can be represented through 2-D arrays.

Page 21: Structured data type

EXAMPLE-#include<iostream.h>void main(){ int A[3][3],B[3][3],r,c; //Read values in matrices cout<<“Enter first matrix row wise\n”; for(r=0;r<3;r++) { for(c=0;c<3;c++) { cin>>B[r][c]; } } int flag=0; //loop to check equality for(r=0;r<3;r++) { for(c=0;c<3;c++) { if(A[r][c]!=B[r][c]) { flag=1; break; } if(flag==1) break; } if(flag!=0) cout<<“Matrices are unequal\n”; else cout<<“Matrices are equal\n”; return 0;}

Page 22: Structured data type

OUTPUT-Enter first matrix row wise 1 2 3 4 5 6 7 8 9Enter second matrix row wise 1 2 3 4 5 6 7 8 9Matrices are equal

Page 23: Structured data type

ARRAY INITIALIZATION C++ provides the facility of array initialization at

the time of declaration. The arrays are initialized in the same way as other

variables are. The general form of array initialization is Type array-name[size1]……[size N]={value-list}; Following code initializes an integer array with 12

elements: int days of month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

Page 24: Structured data type

CALLING FUNCTIONS WITH ARRAYS

When an arrays argument is passed to a function, C++, in most contexts treats the name of an array as if it were a pointer i,e., memory address of some element.

That, if age is an int array to hold 10 integers then age stores the address of age[0], the first element of an array i,e., the array name age is a pointer to an integer which is the first element of an array age[10].

Page 25: Structured data type

EXAMPLE-#include<iostream.h>void main(){ float large(float arr[],intn); //Function prototype char ch; int i; float amount[50],big; for(i=0;i<50;i++) { cout<<“\nEnter element no”<<i+1<<“\n”; cin>>amount[i]; cout<<“\nWant to enter more?(y/n)\n”; cin>>ch; if(ch!=‘y’) break; } if(i<50) i++; //In case of incomplete loop big=large(amount,i); //Call function large() by passing the array and its number of elements cout<<“\nThe largest element of the array is :”<<big<<“\n”; return 0;} float large(float arr[],int n) { if(arr[j]>max) max=arr[j]; }return(max);}

Page 26: Structured data type

EXAMPLE-Enter element no. 2 5Want to enter more?(y/n) yEnter element no. 3 33Want to enter more?(y/n) yEnter element no. 4 13Want to enter more?(y/n) nThe largest element of the array is :33

Page 27: Structured data type

THANK YOU