UNIT IV.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
Structure.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Arrays part 3 Multidimensional arrays, odds & ends.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Introduction to Programming with C++ Fourth Edition
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
C Programming Lecture 14 Arrays. What is an Array? b An array is a sequence of data items that are: all of the same typeall of the same type –a sequence.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Advanced Data types and Sorting
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 05 ARRAY 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Array, Structure and Union
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Arrays.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Arrays.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Module 1: Array ITEI222 - Advance Programming Language.
REEM ALMOTIRI Information Technology Department Majmaah University.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
ARRAYS.
Array in C# Array in C# RIHS Arshad Khan
CHP-2 ARRAYS.
Pointers and Arrays S.Bhuvaneshwari Assistant Professor/CSE
Declaration, assignment & accessing
MSIS 655 Advanced Business Applications Programming
C++ Array 1.
Presentation transcript:

UNIT IV

2.two-dimensional arrays(2-d arrays) One-Dimensional Arrays: Definition of Array: An array is defined to be a group of logically related data items of similar type, stored in contiguous memory locations, sharing a common name but distinguished by subscript(s) values. Depending on the number of subscripts used, array are classified into 1.one-dimensional arrays(1-d array) 2.two-dimensional arrays(2-d arrays) One-Dimensional Arrays: It termed as one-dimensional array or 1-d array. It is used to store a list of values, all of which share a common name & distinguishable by subscript values. Declaration of One-dimensional Arrays Syntax: data-type variable-name[size]; Where, 1.Data-type refers to any data type supported by C. 2.variable-name refers to array name & should be valid C identifier. 3.size-indicates number of data items of type data-type grouped together.

2 a[0] 5 a[1] 6 a[2] 8 a[3] 9 a[4] Example int a[5]; Here, a is declared to be an array of int type and of size five. Five contiguous memory locations are shown below, Each data item in the array a is identified by the array name a followed by a pair of square brackets enclosing a subscript value.The subscript value ranges from 0 to 4., a[0] denotes first data item,a[1] denotes second data item and a[4] denotes the last data item. 2 a[0] 5 a[1] 6 a[2] 8 a[3] 9 a[4]

Initialization of One-Dimensional Arrays We can initialize one-dimensional arrays also,i.e., locations of the arrays can be given values they are declared. Syntax: data-type variable-name[size]={initializer-list} Example: int a[5]={2,5,6,8,9};

Processing One-dimensional Arrays /* Program for declaration, initialization of a one-dimensional array and display its elements. */ #include<stdio.h> #include<conio.h> void main() { int i,a[5]={2,3,5,6,7}; clrscr(); printf(“The elements of array a \n”); for(i=0;i<=4;i++) printf(“%3d”,a[i]); getch(); }

Passing Arrays to a function An array can be passed to a function as a parameter i.e., the name of the array is used as an argument to a function. When an array is passed to a function, the values of the array are not passed to the function,but only the address of the first array element is passed. Example

#include<stdio.h> void main() { int i,num[5]; clrscr(); for(i=0;i<5;i++) printf(“Enter the value for number:%d”,i+1); scanf(“%d”,&num[i]); } printf(“\n The original list is:\n”); printf(“%d”,num[i]); sort(num[]); printf(“\n The sorted list is:\n”); printf(“\t%d”,num[i]); getch(); sort(int n[]) { int i,j,temp; for(i=0;i<4;i++) for(j=i+1;j<5;j++) if(n[i]>n[j]) temp=n[i]; n[i]=n[j]; n[j]=temp; }

Multidimensional Arrays An array with more than one subscript is generally called a multidimensional array i.e, arrays of two dimensions(2-d arrays), arrays of three dimensions(3-d arrays), arrays of four dimension(4-d) and so on. 1.Two-dimensional Arrays(2-d arrays) An array with two subscripts is termed as two-dimensional array.A two dimensional aray can be group of one or more one-dimensional array(s),all of which share a common name(2-d array name) & distinguishable by subscript values. An two-dimensional arrays are very much associated with matrices & perfect data structures for storing matrices.

b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[2][0] b[2][1] Declaration of two-dimensional arrays Syntax data-type variable-name[rowsize][col-size] Where data-type refers to any valid C data type. variable-name refers name of array. rows & colsize indicates the number of elements in each row. Example int b[3][3] 0 1 2 1 2 b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[2][0] b[2][1] b[2][2]

2 3 4 5 6 7 Initialization of two-dimensional Arrays Syntax data-type variable-name[rowsize][colsize]={initializer-list}; data-type refers to any data type supported by C. variable-name refers to array name. rowsize indicates the number of rows colsize indicates the number of columns. Example int a[2][3]={2,3,4,5,6,7} 2 3 4 5 6 7

Processing of two-dimensional arrays 1.See Matrix Addition 2.See Matrix Subtraction 3.See Matrix Multiplication Three-dimensional Arrays An array with three subscript is termed as a three-dimensional array.A three-dimensional array is a collection of one or more two-dimensional array is a collection of one or more two-dimensional arrays,all of which share a common name and are distinguishable by values of the first subscript of the three-dimensional array. Declaration of three-dimensional arrays Syntax data-type variable-anme[size1][size-2][size-3] Where, data-type refers to any data type supported by C. variable-name refers to the array name

size1 indicates the number of tables being grouped together. size2 indicates the number of rows of each table. size3 indicates the number of columns of each table. Example int a[2][4][5] whare a is declared to be a 3-d array. a[0][0][0] indicates data item in first table,first row,first column. a[1][0][0] indicates data item in second table,first row,first column. a[1][1][2] indicates data item in second table,second row,thrid column.

/. To accept the elements of a 3-d array and display item /* To accept the elements of a 3-d array and display item */ #include<stdio.h> #include<conio.h> void main() { int a[2][2][2],I,j,k; clrscr(); printf(“Enter the elements of a of order 2*2*2(Two tables of size 2*2) \n”); for(i=0;i<2;i++) for(j=0;j<2;j++) for(k=0;k<2;k++) scanf(“%d”,a[i][j][k]); } printf(“\n The 3-d arrary a \n\n”); printf(“a-Table %d \n\n”,i+1); for(j=0;j<2;j++) { for(k=0;k<2;k++) printf(“4d”,a[i][j][k]); printf(“\n”); } printf(“\n\n”); getch(); Input-Output Enter the Elements 1 2 3 4 5 6 7 8 The 3-d array a a-Table 1 1 2 3 4 A-Table 2 5 6 7 8

Arrays and String In C Strings are defined as arrays of characters. Example: char names[7][10] names[0]=“Raj”; names[1]=“Jayaram”; . names[6]=“Aishu”;

Example #include<stdio.h> #include<conio.h”> void main() { char names[5][20]; printf(“Enter five names \n”); for(i=0;i<5;i++) scanf(“%s”,names[i]); printf(“List of Names \n”); printf(“%s \n”,names[i]); getch(); } Input-Output List of Names Devaraj Devaraj Shobana Shobana Jayanthi Jayanthi Shanthi Shanthi Grija Grija

}variable 1,variable 2,….variable n; Structure: A structure can be defined to be a group of logically data items, which may be of different types,stored in contiguous memory locations, sharing a common name, but distinguished by its members. Syntax: struct tag-name { data-type member1; data-type member2; data-type member3; }variable 1,variable 2,….variable n; Declaration of Structure Variables struct tag-name variable-name; Example struct emp int empno; char name[20]; float salary; }e1,e2;

Initialization of Structure Variables Syntax struct tag-name variable-name={member1-value,member2-value, membern-value} Example struct emp e={121,”Anju”,20000}; Operation of Structure Accessing the individual members of a structure variable with help of member operator. struct emp e; e.empno=10; 10 is assigned to empno member of e. strcpy(e.name,”Ram”); The string “Ram” is copied to name member of e.

#include<stdio.h> #include<conio.h> struct emp { int empno; char name[20]; float salary; }; void main() struct e1; clrscr(); printf(“Enter empno,name and salary \n”); scanf(“%d%s%f”,&e1.empno,e1.name, e1.salary); printf(“e1.empno=%d\n”,e1.empno); printf(“e1.name=%s\n”,e1.name); printf(“e1.salary=%8.2f\n\n”,e1.salary); getch(); }

Passing Structure to function Similar to,we pass arrays of basic type(char,int,float,etc.,)to functions,passint arrays of structures to functions as arguments. Example #include<stdio.h> #include<conio.h> struct emp { int empno; char name[20]; float salary; }; void display(struct emp[],int) void main() { struct emp e[10]; int i,n; clrscr(); printf(“Enter no. of employees \n”); scanf(“%d”,&n); printf(“Enter %d employee details \n”,n); for(i=0;i<n;i++) scanf(“%d%s%f”,&e[i].empno, &e[i].name,&e[i].salary); printf(“The list of employees \n”); getch(); }

void display(struct emp e[],int n) { int i; for(i=0;i<n;i++) printf(“%6d %15s %8.2f \n”,e[i].empno,e[i].nme,e[i].salary); }

Self Referential Structures When a member of a structure is declared as a pointer that points at the same structure, then it is called self referential structure. Syntax: struct tag { datatype1 member1; datatype2 member2; datatype3 member3; ……………………… struct tag *name; } When tag refers to the structure name and name refers to the name of the pointer variable. Example: struct node char name[20]; struct node *ptr; };

Where, name->character array ptr->pointer to another structure of same type called ptr.Hence it is called self-referential structure. Self-referential structures are very useful in applications that involve linked data structures, such as linked lists and trees.

}variable1,variable2,….variablen Union: Like a structure, a union is also a derived data type. The members of a union share a single storage space. Only ONE member of each union can be referenced at a time. Amount of space allocated for storage is the amount needed for the largest member of the union. Syntax: union tag { datatype1 member1; datatype2 member2; …………….. datatypen membern }variable1,variable2,….variablen

Example 1 union id { int i; float f; char c; } Example 2 #include<stdio.h> void main() union data int i=10; float f=20.5; }; union data d; printf(“%d”,d.i); printf(“%f”,d.f);

Difference between Structure and Union Every memory has its own memory sapce. All the members use the same memory space to store the values. It can handle all the members(or) a few as required at a time. It can handle only one member at a time,as all the members use the same space. Keyword struct is used. Keyword union is used. It may be initialized with all its members. Only its first member may be initialized. Any member can be accessed at any time without the loss of data. Only one member can be accessed at any time with the loss of previous data. Different interpretation for the same memory location are not possible. Different interpretations for the same More storage space is required. Minimum storage space is required. Syntax: structure strut-name { }var1..varn; union union-name

User-defined data types: 1.Per-defined are int float char void user defined are: structures and union. 2.A user-defined data type is typedef and Enumerated Data type. 3.User defined datatype are nothing but those which are constructed by using structure and union. 1.Enumerated Data Type >Enumerated data type offers us a way of inventing our own data type. Syntax: enum tag-name { enumerator1, enumerator2, enumerator3, . enumeratorn };

Example: enum boolean f; f=false; 2.Typedef Typedef a facility provided by C,enables us to rename existing built-in data types and user-defined data types and thereby help in increasing the degree of readability of source code. Syntax typedef old-name new-name; Where old-name is the name of the existing data type and new-name is the new name given to the data type identified by the old-name. Example struct emp { int empno; char name[20]; float salary; }; typdedef struct emp EMP; Variables of struct emp can now be declared using EMP as the type specifier as: EMP e e has been declared to be a variable of struct emp type.

Bitwise Operation In computer programming, a bitwise operation operates on one or two bit patterns or binary numbers at the level of their individual bits. Bitwise operations are slightly faster than addition and subtraction operations and significantly faster than multiplication and division operations. The bitwise operators utilize something called Binary Math. If you already know binary Math, it is BASE 2.