1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Structure.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Programming with Collections Collections in Java Using Arrays Week 9.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Arrays.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
Arrays One-Dimensional initialize & display Arrays as Arguments Part I.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
Chapter 8 Arrays and Strings
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 Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Array, Structure and Union
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Arrays.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Module 1: Array ITEI222 - Advance Programming Language.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
EGR 2261 Unit 10 Two-dimensional Arrays
Arrays Arrays exist in almost every computer language.
One-Dimensional Array Introduction Lesson xx
Programming Fundamental
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Programming Fundamental
Presentation transcript:

1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays and Strings Array Fundamentals Two Dimensional Arrays Function Declaration with Array Argument Arrays of Structures Arrays As Class Member Data Arrays of Objects C-Strings The Standard C++ string Class 2

Arrays and Strings Array Fundamentals Definitions of array Use of Arrays Arrays Elements Initialization of Arrays Programs: Basic Array with for loop Calculate no. of days of year Calculate average sales Average of marks Copying array Array of characters 3

Arrays Fundamentals Definition: An array is an aggregate data type that lets you access multiple variables of same type through a single name by use of an index. OR An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. 4

Arrays Fundamentals  Use of Arrays: When multiple, similar type of variables are used to store data. Example 1: We can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier. Example 2: To store the record of students in class we need 50 variables with different names, by using array we could manage data by just one name. 5

Arrays Fundamentals Arrays Elements: The items in an array are called elements (in contrast to the items in a structure, called members). As we noted, all the elements in an array are of the same type; only the values vary. Figure 7.2 shows the elements of the array age. Arrays are LIFO (Last-in First-out) structure. It means elements included in Last will be manipulated First. 6

Arrays Fundamentals Definition of Arrays Definition of arrays includes three parts: 1. Datatype of array: Type of data elements included in array. 2. Name of array: Any name relevant to data elements. 3. Size of array: It defines how many data elements are included in array. It is antonym of structure. Structure is collection of different types of data members. 7

Arrays Fundamentals 8 Figure7.1: Definition of Array (Datatype, Name & Size) Figure 7.2: Arrays Elements

Arrays Fundamentals (Index) The elements of the array are manipulated using the index. The index of array starts from zero and is one less than array's size. Index of array is also called subscript. 9 The indexing of the array starts from zero, not from one. So in the above example, the index of the array C will be from C[0] to C[9].

Arrays (Declaration) Every array has a data type i.e. name and size. The rules of variable naming convention apply to array names. The size of the array tells how many elements are there in the array. The arrays occupy the memory depending upon their size and have contiguous area of memory. Declaration: Syntax: data_typearray_name [size] ; Example: int ages[10]; The array occupies the contiguous area, in this case it will occupy forty bytes (one int = 4 bytes) 10

Arrays (Declaration) Arrays may be declared with simple variables in a single line. int i, age [10]; int height [10], length [10] ; To access array, we can’t use the whole array at a time. We access arrays element by element. An index (subscript) may be used to access the first element of the array. In this case, to access first element we write like age[0]. To access the 5 th element, we will write age[4] and so on. 11

Arrays Fundamentals Initialization of Arrays It includes definition and starting values of each element of array. Note: Don't use the default initialization of arrays. Compiler may assign some value to each declared array. 12 Figure 7.3: Syntax of Initialization of Arrays

Arrays Fundamentals (Initialization…) 13 int i, age [10]; for ( i = 0; i < 10 ; i++ ) { age[i] = 0; } With the help of this simple loop, we have initialized all the elements of array age to zero. int age [10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; With the help of this simple loop, we have initialized all the elements of array age to zero. int age [10] = { 0}; Same could be performed by using following shortcut. int age [ ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; The compiler detect the initialization list which consists of ten 0’s. Therefore, it creates an array of 10 integers and initializes all the elements with zero.

Arrays Fundamentals (Program: Basic Array with for loop) #include int main() { int age[4] ; //array ‘age’ of 4 integers for(int j=0; j<4; j++) //get 4 ages { cout << “Enter an age: “; cin >> age[j]; //access array element } for(j=0; j<4; j++) //display 4 ages cout << “You entered “ << age[j] << endl; } 14 Write a program using array, which get ages of four persons and return back all ages in same sequence.

Arrays Fundamentals (Program: Calculate no. of days of year) #include int main() { int month, day, total_days; int days_per_month[12] = { 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 }; cout << “\nEnter month (1 to 12): “; //get date cin >> month; cout << “Enter day (1 to 31): “; //insert date of month cin >> day;//date of month saved in day variable total_days = day; //separate days for(int j=0; j<month-1; j++) //add days each month total_days += days_per_month[j]; cout << “Total days from start of year is: “ << total_days<< endl; } 15 Write a program which calculates the number of days from the beginning of the year to a date specified by the user.

Arrays Fundamentals (Program: Calculate average sales) //Sales.cpp #include int main() { const int SIZE = 6; //size of array double sales[SIZE]; //array of 6 variables cout << "Enter sales for 6 days\n"; for(int j=0; j<SIZE; j++) //put figures in array cin >> sales[j]; double total = 0; for(j=0; j<SIZE; j++) //read figures from array total += sales[j]; //to find total double average = total / SIZE; // find average cout << "Average = " << average << endl; } 16 Write a program which calculates the number of average sales of six days.

Arrays Fundamentals (Program: average of marks) #include int main() { int sum=0; int marks[3]; for(int a=0;a<3;a++) { cout<<"please insert marks of subject "<<endl; cin>>marks[a]; cout<<endl; sum=sum+marks[a]; } cout<<"The average is: "<<sum/(sizeof(marks)/2)<<endl; } 17 Write a program using array, which gets marks of 4 subjects and provide sum and average of marks. Also use sizeof() function.

Arrays Fundamentals (Program: Copying arrays) #include int main() { int ages[4]={5,10,15,20}; int same_age[4]; same_age[0]=ages[0]; same_age[1]=ages[1]; same_age[2]=ages[2]; same_age[3]=ages[3]; for(int k=0;k<4;k++) cout<<"Output of new array: "<<same_age[k]<<endl; //or for(int i=0;i<4;i++) { same_age[i]=ages[i]; cout<<"Output of new array: \t"<<same_age[i]<<endl; } 18 Write a program which copy values from one array to another array.