CSC 113: Computer Programming (Theory = 03, Lab = 01)

Slides:



Advertisements
Similar presentations
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
Advertisements

4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved 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 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 3 Arrays & Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
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.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
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 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
آرايه ها اصول كامپيوتر 1.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
Arrays Kingdom of Saudi Arabia
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
4.1 Introduction Arrays A few types Structures of related data items
Operator Overloading.
Arrays Kingdom of Saudi Arabia
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
7 Arrays.
Chapter 15 - C++ As A "Better C"
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Lecture 2 Arrays & Pointers May 17, 2004
Arrays.
C++ Programming Lecture 20 Strings
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Lecture 2 Arrays & Pointers September 7, 2004
CISC181 Introduction to Computer Science Dr
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.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

CSC 113: Computer Programming (Theory = 03, Lab = 01) Momina Moetesum Computer Science Department Bahria University, Islamabad

Introduction to Arrays Week # 11

Introduction Arrays One-dimesional arrays Character arrays or strings Structures of related data items Static entity - same size throughout program One-dimesional arrays Character arrays or strings Multi-dimensional arrays

Arrays Array To refer to an element, specify Consecutive group of memory locations Same name and type To refer to an element, specify Array name and position number Format: arrayname[ position number ] First element at position 0 n element array c: c[ 0 ], c[ 1 ]…c[ n - 1 ] Array elements are like normal variables c[ 0 ] = 3; cout << c[ 0 ];

  Arrays c[6] -45 6 72 1543 -89 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c

Array Subscripts Performing operations in subscript. 1) If x = 3, c[ 5 – 2 ] == c[ 3 ] == c[ x ] 2) If a =5 and b=6 then c[a + b] +=2 adds 2 to array element c[11] 3) To print the sum of first three elements of array c cout<<c[0]+c[1]+c[2]<<endl; 4) To divide the value of seventh element of array c by 2 x = c[6]/2; *Note: [ ] are considered an operator with same precedence as ( )

Declaring Arrays Declaring arrays - specify: Type of array Name Number of elements Examples int c[ 10 ]; float hi[ 3284 ]; Declaring multiple arrays of same type Similar format as other variables Example int b[ 100 ], x[ 27 ];

Initializing Arrays Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 If too many initializers, a syntax error is generated int n[ 5 ] = { 0 } Sets all the elements to 0 If size omitted, the initializers determine it int n[] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore n is a 5 element array

1 // Fig. 4.4: fig04_04.cpp 2 // Initializing an array 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 int n[ 10 ]; 15 for(int i=0; i<10; i++) for 16 n[i]=0; 17 cout << "Element" << setw( 13 ) << "Value" << endl; 18 for (i = 0; i < 10; i++ ) 19 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 20 21 return 0; 22 }

Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0

1 // Fig. 4.5: fig04_05.cpp 2 // Initializing an array with a declaration 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <iomanip> 9 10 using std::setw; 11 12 int main() 13 { 14 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 15 16 cout << "Element" << setw( 13 ) << "Value" << endl; 17 18 for ( int i = 0; i < 10; i++ ) 19 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 20 21 return 0; 22 }

Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37

1 // Fig. 4.6: fig04_06.cpp 2 // Array size declaration as const 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 #include <iomanip> 7 using std::setw; 8 int main() 9 { 10 const int arraySize = 10; 11 int s[arraySize]; 12 13 for(int i=0; i<arraySize; i++) 14 s[i]=2+2*i; 15 for 16 cout << "Element" << setw( 13 ) << "Value" << endl; 17 18 for ( int j = 0; j < 10; j++ ) 19 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; 20 21 return 0; 22 }

Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20

1 // Fig. 4.6: fig04_06.cpp 2 // Array size declaration as const 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 #include <iomanip> 7 using std::setw; 8 int main() 9 { 10 const int arraySize = 10; 11 int s[arraySize]; 12 13 for(int i=0; i<arraySize; i++) 14 cin>>s[i]; 15 for 16 cout << "Element" << setw( 13 ) << "Value" << endl; 17 18 for ( int j = 0; j < 10; j++ ) 19 cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; 20 21 return 0; 22 }

2 // A const object must be initialized 3 4 int main() 5 { 1 // Fig. 4.7: fig04_07.cpp 2 // A const object must be initialized 3 4 int main() 5 { 6 const int x; // Error: x must be initialized 7 8 x = 7; // Error: cannot modify a const variable 9 10 return 0; 11 } 1. Initialize const variable 2. Attempt to modify variable   Program Output Fig04_07.cpp: Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be initialized in function main() Error E2024 Fig04_07.cpp 8: Cannot modify a const object in function main() *** 2 errors in Compile ***

1 // Fig. 4.7: fig04_07.cpp 2 // A const object must be initialized 3 4 int main() 5 { 6 const int x = 7; 7 8 9 10 return 0; 11 }

Self Practice Define a constant variable arraySize initialized to 10. Declare an array with arraySize elements of type float and initialize the elements to 0. Name the fourth element from the beginning of the array. Refer to array element 4. Assign the value 1.667 to array element 9. Assign the value 3.333 to the seventh element of the array. Print all the elements of array using for repetition structure.

Character arrays or strings Arrays of characters All strings end with null ('\0') Should be declared large enough to accommodate ‘\0’ Examples: char string1[] = "hello"; char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ }; Subscripting is the same as for a normal array String1[ 0 ] is 'h' string1[ 2 ] is 'l‘

Character arrays or strings Input from keyboard char string2[ 10 ]; cin >> string2; Takes user input until first white space character is encountered Side effect: if too much text entered, data written beyond array

2 // Treating character arrays as strings 3 #include <iostream> 1 // Fig. 4_12: fig04_12.cpp 2 // Treating character arrays as strings 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 char string1[ 20 ], string2[] = "string literal"; 12 13 cout << "Enter a string: "; 14 cin >> string1; 15 cout << "string1 is: " << string1 16 << "\nstring2 is: " << string2 17 << "\nstring1 with spaces between characters is:\n"; 18 19 for ( int i = 0; string1[ i ] != '\0'; i++ ) 20 cout << string1[ i ] << ' '; 21 22 cin >> string1; // reads "there" 23 cout << "\nstring1 is: " << string1 << endl; 24 25 cout << endl; 26 return 0; 27 } Inputted strings are separated by whitespace characters. "there" stayed in the buffer. Notice how string elements are referenced like arrays.

Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o string1 is: there

Self Practice Declare and initialize a character array word with your name. Using a for structure, find the size of the array.