Dr. Khizar Hayat Associate Prof. of Computer Science

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Chapter 10.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
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
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays.
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)
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS.
INC 161 , CPE 100 Computer Programming
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
EGR 2261 Unit 10 Two-dimensional Arrays
LESSON 2 Basic of C++.
EGR 2261 Unit 9 One-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Arrays Arrays exist in almost every computer language.
Programming fundamentals 2 Chapter 1:Array
Module 2 Arrays and strings – example programs.
JavaScript: Functions.
Arrays Skill Area 315 Part A
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
2.1 Parts of a C++ Program.
Arrays Kingdom of Saudi Arabia
Week 9 – Lesson 1 Arrays – Character Strings
Data type List Definition:
Introduction To Programming Information Technology , 1’st Semester
String in C++.
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
7 Arrays.
Passing Arrays to functions
Arrays Arrays A few types Structures of related data items
C++ Programming Lecture 20 Strings
Strings Skill Area 313 Part C
CS-161 Computer Programming Lecture 15 & 16: Arrays II
CS31 Discussion 1H Fall18: week 6
Introduction to Programming - 1
Library in c++ ( as cmath , iostream , …… etc.)
Programming Fundamental
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
Presentation transcript:

Dr. Khizar Hayat Associate Prof. of Computer Science Arrays Dr. Khizar Hayat Associate Prof. of Computer Science

Arrays An array is a group of elements of the same data type that are placed in contiguous (adjacent) memory locations. Each element of the array is identified by an index. The number of rows/columns of an array are called its dimensions: One Dimensional Array Two Dimensional Array

Declaring the C++ arrays One dimensional arrays are declared in C++ by first specifying the data type, then name and then the number of locations in square brackets ([]), e.g. int varname[50];//declares an integer array of size 50 float real[30];//declares a float array of size 30 char ch[25];//declares a character array of size 25

Initializing array You can declare and initialize arrays in a single statement, e.g. int foo[5]={16, 2, 77, 40, 12071}; This statement declares an array (named foo) that can be represented like this: 1 2 3 4 16 77 40 12071

Initializing array - 2 The number of values between braces, {}, must not be greater than the number of elements. The number may however be lesser, e.g. int bar[6]={10, 20, 30}; This statement declares an array (named bar) that can be represented like this: Another example: int baz[5]={}; 1 2 3 4 5 10 20 30 1 2 3 4 Note that exceeding the element limit is not a syntax error.

Accessing the array values Remember: int foo[5]={16, 2, 77, 40, 12071}; This array is outlined like below: Index Value The index is also called the subscript. 1 2 3 4 foo[0] foo[1] foo[2] foo[3] foo[4]

Example #include<iostream> using namespace std; int main () { int a[5]; a[0]=10; a[1]=20; cout<<“Enter the value of 3rd element”<<endl; cin>>a[2]; a[0]=a[0]*5; cout<<a[0]<<“\t”<<a[1]<<“\t”<<a[2]<<endl; return 0; }

Accessing array elements: Example #include<iostream> using namespace std; int main () { int a[10],i; for (i=0;i<10;i++) //To read and store the inputs cout<<“Enter the value of <<i<<“th element”<<endl; cin>>a[i]; } for (i=0;i<10;i++) //To display the contents of array cout<<“The value of a[“<<i<<“] is”<<a[i]<<endl; return 0; Output at page 8-9 of notes

Two-Dimensional Arrays Two-dimensional Array: a collection of a fixed number of components arranged in two dimensions All components are of the same type The syntax for declaring a two-dimensional array is: dataType arrayName[rowsize][colsize]; where rowsize and colsize are expressions yielding positive integer values

Two-Dimensional Arrays (continued) The two expressions rowsize and colsize specify the number of rows and the number of columns, respectively, in the array Two-dimensional arrays are sometimes called matrices or tables

Two-Dimensional Arrays (continued) A First Book of C++: From Here To There, Third Edition

Accessing Array Components The syntax to access a component of a two-dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position

Processing Two-Dimensional Arrays A two-dimensional array can be processed in three different ways: Process the entire array Process a particular row of the array, called row processing Process a particular column of the array, called column processing

Processing Two-Dimensional Arrays (continued) Each row and each column of a two- dimensional array is a one-dimensional array When processing a particular row or column of a two-dimensional array we use algorithms similar to processing one- dimensional arrays

Examples Adding two matrices Multiplying two matrices Column sums and row sums

Strings A C-style string is a one-dimensional array of characters which is terminated by a null character '\0‘, e.g. char greeting[6] = {'H','e','l','l','o','\0'}; Another easier way: char greeting[] = "Hello"; With this type of initialization, no need to place the null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the array. In C++, any thing enclosed inside double quotes (“”) is a string of enclosed characters plus ‘\0’

Strings: Example #include <iostream> using namespace std; int main () { char greeting[6]={'H','e','l','l','o','\0'}; cout << "Greeting message: "; cout << greeting << endl; return 0; }

Strings: Example #include <iostream> using namespace std; int main () { char greeting[]=“Hello”; cout << "Greeting message: "; cout << greeting << endl; return 0; }

Accessing string elements You can access elements of a string via for loop and array subscripts but: Manipulation of Character Arrays or Strings do not require any for loop You can access the string by just using its identifier (see example - next slide) Note, However, when giving input to a char array, blank spaces are not allowed. Blank space in input means the end of array and anything after it will not be stored.

Accessing string elements #include <iostream> using namespace std; int main () { char question[]=“Please, enter your first name:”; char greeting[]=“Hello ”; char fname[80]; cout<<question<<endl; cin>>fname; cout<<greeting <<“Your first name is…”<<fname<<endl; return 0; } Output Please enter your first name: Asma Hello Your first name is…Asma

String Functions from <cstring> C++ supports a wide range of functions that manipulate null-terminated strings: S.N. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.