Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

2000 Prentice Hall, Inc. All rights reserved. 1 Capitolo 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Copyright © 2003 Pearson Education, Inc. Slide 1.
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.
Chapter 7: Arrays In this chapter, you will learn about
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2.
Dynamic memory allocation
Programming In C++ Spring Semester 2013 Practice 1-3 Lectures Programming In C++, Lecture 3 By Umer Rana.
Programming In C++ Spring Semester 2013 Practice 2 Programming In C++, Practice By Umer Rana.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Spring Semester 2013 Lecture 5
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Programming In C++ Spring Semester 2013 Lecture 13 Programming In C++, Lecture 13 By Umer Rana.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 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 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Systems Programming Concepts
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 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.
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.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
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.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
EKT120 : Computer Programming
Arrays Declarations CSCI N305
Arrays (Operations on Arrays)
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
C Arrays Systems Programming.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
CS-161 Computer Programming Lecture 14: Arrays I
EKT150 : Computer Programming
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
EKT120: Computer Programming
To refer to an element, specify
Arrays Arrays A few types Structures of related data items
Lecture 14: Problems with Lots of Similar Data
C++ Array 1.
Programming Fundamental
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana

Array Programming In C++, Lecture 6 By Umer Rana Array provides the facility to store a collection of data of same type under single variable name in memory. Just like the ordinary variable, the array should also be declared properly. The declaration of array includes the type of array, the array name and maximum number of elements.

Array Programming In C++, Lecture 6 By Umer Rana To refer to a particular location or element in the array, we specify the name of the array and the index of the particular element in the array. The index specifies the location of the element in the array. The array index starts from zero. The maximum index value will be equal to the size of the array minus one.

Declaring Array Programming In C++, Lecture 6 By Umer Rana When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[numberOfElements]; Examples: int c[ 10 ]; Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ]; Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[9] c[8] c[7] c[5] c[4]

Array Programming In C++, Lecture 6 By Umer Rana Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name Position number Format: arrayname[ position number ] First element at position 0 n element array named c: c[ 0 ], c[ 1 ]...c[ n – 1 ] Array elements are like normal variables c[ 0 ] = 45; printf( "%d", c[ 0 ] ); Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[9] c[8] c[7] c[5] c[4]

Initializers One Dimension Array Programming In C++, Lecture 6 By Umer Rana Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } All elements 0 If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array

Example Array Programming In C++, Lecture 6 By Umer Rana #include void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nTemperature of day %d: ",(day+1)); scanf("%d",&temper[day]); } printf("\nThanks for Entering Weekly temperature"); getch(); }

Example Array Programming In C++, Lecture 6 By Umer Rana void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nTemperature of day %d: ",(day+1)); scanf("%d",&temper[day]); } for (day=0;day<7;day++) { printf("\nTemperature of day %d is %d. ",(day+1),temper[day]); }getch(); }

Referring to individual Element of the Array Programming In C++, Lecture 6 By Umer Rana Once the Array has been established we need a way to refer to its individual elements. This is done with subscripts the number in brackets following the array name, this number specifies the element’s position in the array. All the array elements are numbered starting at 0. Example: temper[2]; temper[6];

Referring Example Programming In C++, Lecture 6 By Umer Rana void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nPlease Enter the Temperature of day %d: ",(day+1)); scanf("%d",&temper[day]); } printf("\nWhich Day Temperature you want to see:"); scanf("%d",&day); printf("\nTemperature of day %d is %d. ",day,temper[day-1]); getch(); }

Points to Remember Programming In C++, Lecture 6 By Umer Rana If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. It is important to remember that arrays are not automatically initialized to zero. The programmer must at least initialize the first element to zero for the remaining elements to be automatically zeroed. For Example: int n[ 10 ] = { 0 };

More than one Dimension Programming In C++, Lecture 6 By Umer Rana The Two dimensional array is also called a matrix. Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is: Data_type Array_name [row size][column size]; The type may be any valid type supported by C. The rule for giving the array name is same as the ordinary variable. The row size and column size should be an individual constant.

Two Dimension Array Programming In C++, Lecture 6 By Umer Rana The following declares a two-dimensional 3 by 2 array of integers. Data_type Array_name [row size][column size]; int matrix [3][2];

Two Dimension Array Programming In C++, Lecture 6 By Umer Rana int main(void) { int stud[4][2]={ {1,56}, {2,44}, {3,67}, {4,77} }; int i; printf("\n Enterd Reg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); }

Two Dimension Array Programming In C++, Lecture 6 By Umer Rana int main(void) { int stud[4][2]; int I; for (i=0;i<=3;i++) { printf("\n Enter Reg.No and Marks Please\n"); scanf("\n%d%d",&stud[i][0],&stud[i][1]); } printf("\n Enterd Reg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); }

Three Dimension Array Programming In C++, Lecture 6 By Umer Rana The declaration form of Three-dimensional array is : Data_type Array_name [size1][size2][size3]; Data_type Array_name [No of Sets][Set Rows][Set Column];

Three Dimension Array Programming In C++, Lecture 6 By Umer Rana Example: int arr[3][2][2]; A Three dimensional array can be thought of as an array of arrays of arrays. The outer array has three elements, each of which is a two dimensional array.

Three Dimension Array Programming In C++, Lecture 6 By Umer Rana Int arr[3][2][2]= { 1{1,1},{3,3} }, { 2{2,2},{3,3} }, { 3{3,2},{4,4} } };

Write Array of 5 number in ascending order Programming In C++, Lecture 6 By Umer Rana

int main() { int a[5],i,j,temp; printf("Enter 5 nos.\n\n"); for (i=0;i<5;i++) scanf("%d",&a[i]); for (i=0;i<5;i++) {for(j=i+1;j<5;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("Ascending Order is:"); for(j=0;j<5;j++) printf("\n%d",a[j]); getch(); } Programming In C++, Lecture 6 By Umer Rana

Write Array of 5 number in Descending order Programming In C++, Lecture 6 By Umer Rana

main () { int i,j,temp,a[5]; printf ("Enter the numbers \n"); for (i=0; i<5; ++i) scanf ("%d",&a[i]); for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } printf (“Descending order are given below\n"); for (i=0; i<n; ++i) printf ("%d\n",a[i]); getche(); } Programming In C++, Lecture 6 By Umer Rana

String Programming In C++, Lecture 6 By Umer Rana A string in C is actually a character array. As an individual character variable can store only one character, so we need an array of characters to store strings. Each character in a string occupies one location in an array. A string constant is one dimensional array of characters terminated by the null character ‘\0’, is put after the last character. This is done so that program can tell when the end of the string has been reached. Character arrays or strings are used to manipulate text such as words and sentences.

String Programming In C++, Lecture 6 By Umer Rana For example, char str1[25]; the string ”I LIKE C PROGRAMMING” is stored as follow. Thus, in C, a string is a one-dimensional array of characters terminated a null character. The terminating null character is important. ILIKECPROGRAMMING\0

String Programming In C++, Lecture 6 By Umer Rana void main(void) { char str1[25]={'I',' ','l','i','k','e',' ','C',' ','P','r','o','g','r','a','m','m','i','n','g'}; int i=0; while(str1[i]!='\0') { printf("%c",str1[i]); i++; } getch(); }

String Programming In C++, Lecture 6 By Umer Rana void main(void) { char str1[25]={'I',' ','l','i','k','e',' ','C',' ','P','r','o','g','r','a','m','m','i','n','g'}; int i=0; printf("%s",str1); getch(); }

String Programming In C++, Lecture 6 By Umer Rana #include int main(void) { char name[25]; scanf("%s",&name); printf("%s",name); getch(); }

String Programming In C++, Lecture 6 By Umer Rana #include void main(void) { char name[25]; gets(name); puts(name); getch(); }

Two Dimensional String Programming In C++, Lecture 6 By Umer Rana A string is an array of characters; so, an array of strings is an array of arrays of characters. We can declare a two dimensional character array of MAX strings of size SIZE as follows: char names[MAX][SIZE];

Two Dimensional String Programming In C++, Lecture 6 By Umer Rana void main(void) { char name[4][10]={ "Umer", "Ali", "Sarah", "Adil" }; for(int i=0;i<4;i++) { printf("%s\n",name[i]); } getch(); }

Programming In C++, Lecture 6 By Umer Rana String Functions:- strlen returns the length of a string strlwr it will convert all characters stored in the character array string to lowercase strupr it will convert all characters stored in the character array string to UPPERCASE strcat strcat(str1,str2) concatenates a copy of the string pointed by str2 to the string pointed by str1 and terminates str1 with a null) strcpy strcpy(str1,str2) copies the string pointed to by str2 to the string pointed to by str1 and terminates str1 with a null.

Programming In C++, Lecture 6 By Umer Rana strcmp strcmp(str1,str2) compares str1 and str2. Returns a negative value if str1 str2. strchr strchr(str,ch) finds ch in str. Returns a pointer to the first occurrence of the character ch in str, if ch does not occur in str,strchr returns NULL. strstr strstr(str1,str2) finds the first occurrence of substring str2 in str1.Returns a pointer to the elements in str1 that contain str2, or NULL if str2 does not occur in str1. strrev strrev(str) reverses all characters in str.

Programming In C++, Lecture 6 By Umer Rana #include int main(void) { char name[25]="Umer"; int a=strlen(name); printf("%d",a); char name2[4][10]={ "Umer", "Ali", "Sarah", "Adil" }; puts("\n\n 2 Dimention String"); for(int i=0;i<4;i++) { printf("%d\n",strlen(name2[i])); } getch(); } strlen String function

Programming In C++, Lecture 6 By Umer Rana Quiz Q. An array is a collection of? Q. Is this correct array declaration? int num(25); Q. Which element of the array does this expression refference? num[4]; Q. What is the difference between the 5’s in these two expressions? int num[5]; num[5]=10; Q. Which is declaration of two dimension array? int num[4][2]; int num[3][4][2];

Programming In C++, Lecture 6 By Umer Rana Quiz Q. string is a collection of? Q. Is this correct character array declaration? char str[25]; Q. Which is more appropriate for reading in multi-word string? gets()printf()scanf()puts() Q. What is the difference between the 5’s in these two expressions? char num[5]; num[5]=‘x’; Q. Which string function use to get length of the string? strlen() strcpy()