CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays
|
|
- Jocelin Melinda Hill
- 6 years ago
- Views:
Transcription
1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays Prof. Amr Goneid, AUC 1
2 Arrays Prof. Amr Goneid, AUC 2
3 1-D Arrays Data Structures The Array Data Type How to Declare an Array Operations on Arrays Passing to and from Functions Examples Example Functions Prof. Amr Goneid, AUC 3
4 1. Data Structures Data Set Linear Tree Graph Prof. Amr Goneid, AUC 4
5 Data Structures Sets: No structure, just membership. Linear: Sequential, one-to-one. e.g Arrays, Strings and Streams Tree: Non-Linear, one-to-many. Graph: Non-Linear, many-to-many. Arrays, Structures and Classes are used to model different data structures. Prof. Amr Goneid, AUC 5
6 2. The Array Data Type Array elements have a common name The array as a whole is referenced through the common name Array elements are of the same type the base type Individual elements of the array are referenced by sub_scripting the group name by an index Prof. Amr Goneid, AUC 6
7 1-D Arrays Prof. Amr Goneid, AUC 7
8 1-D Arrays Linear Data Structure (One-To-One) Fixed Size n (Static) All elements are of the same type An element is accessed by an ordinal index with values between a lower bound (0) and an upper bound (n-1) 0 index n -1 Prof. Amr Goneid, AUC 8
9 Array Size Size = No. of Elements = n UB = n - 1 Number of Bytes = size * Element size. All elements are of the same size. Maximum size of 64 Kbytes elements = 500 bytes 249 int 0 26 elements = 104 bytes 25 float Prof. Amr Goneid, AUC 9
10 3. How to Declare an Array Syntax: <basetype> <name> [size] ; e.g. int a [20] ; float x [101] ; const Maxelem = 200; typedef int itemtype; itemtype y [Maxelem+1]; string name [51]; enum color {red, green, blue}; color pixel [201]; int npixels[3]; Prof. Amr Goneid, AUC 10
11 Sample Declarations Suppose const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000; Prof. Amr Goneid, AUC 11
12 Sample Declarations Then the following are all correct array declarations. int A[10]; char B[MaxStringSize]; float C[M*N]; int Values[MaxListSize]; Rational D[N-15]; Prof. Amr Goneid, AUC 12
13 4. Operations on Arrays Declaration with Initialization: e.g. int x [6] = {12, 23, 56, 34, 18, 20}; char g[ ] = { A, B, C, D, F }; //This sets the size of the array g to 5 elements Prof. Amr Goneid, AUC 13
14 Operations on Arrays Accessing an Element: <array name>[index] 0 =< index =< UB index (location of element in the array) can be a const, variable or any integral / ordinal expression. e.g. x[2] or y[i] or x[2*n+1] Prof. Amr Goneid, AUC 14
15 Operations on Arrays Index manipulation: int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; Prof. Amr Goneid, AUC 12 15
16 Operations on Arrays Warning C++ will not flag an error if the array subscript goes out of bounds Ex: int B[10]; // declare B with 10 elements B[20] = 2; // subscript out of range This is allowed by the compiler, but will lead to unpredictable behavior Beware! Prof. Amr Goneid, AUC 12 16
17 Operations on Arrays Retrieve an element: e.g. z = x[ i ] ; Update an element: e.g. name[ i ] = Ann ; pixel[100] = red; npixels[green] ++ ; Input an element of an array: cin >> x[i]; Output an element of an array: cout << x[i]; Prof. Amr Goneid, AUC 17
18 5. Passing to and from Functions Passing an array element as a parameter: e.g. void swap ( int &a, int &b); invoke as swap ( x[i], x[j] ) ; Passing an entire array by reference: e.g. int findmax ( int x [ ], int size ); invoke as m = findmax ( x, n); Prof. Amr Goneid, AUC 18
19 Remember Arrays are always passed by reference Can use const if array elements are not to be modified, e.g. int findmax ( const int x [ ], int size ); You do not need to include the array size within the brackets when defining an array parameter. This is because the array name is the base address of the array, and the size is already known. Prof. Amr Goneid, AUC 19
20 6. Examples A function to input n elements of an integer array and return the array and n as parameters: const MAX_SIZE = 200; int a [MAX_SIZE]; void input_array ( int a[ ], int &n ) { int n = 0; int v; while((n < MAX_SIZE) && (cin >> v)) { a[n] = v; n++; } } Prof. Amr Goneid, AUC 20
21 Examples A function that receives an integer array and lists the first n elements of the array : void output_array ( const int a[ ], int n ) { for (int i = 0 ; i < n ; i++) cout << a[i] << ; cout << endl; } Prof. Amr Goneid, AUC 21
22 Examples A function to receive an integer array and return the index of the minimum element in the sub-array starting at index (s) and ending at index (e): int index_of_min ( int a[ ], int s, int e ) { int imin = s; for (int i = s+1; i <= e ; i++) if (a[i] < a[imin]) imin = i ; return imin ; } Prof. Amr Goneid, AUC 22
23 Examples A function to receive a real array and return the average value of the elements. float average( float x[ ], int n ) { float sum = 0; for (int i = 0; i < n ; i++) sum += x[i]; return (sum / float (n)) ; } Prof. Amr Goneid, AUC 23
24 7. Example Functions Function1: Computing the Average and Standard Deviation of a list of numbers. Function2: Linear (Sequential) Search in an array. Function3: Sorting an array using Selection Sort. Function4: Sorting an array using Bubble Sort Function5: Searching an array using Binary Search Prof. Amr Goneid, AUC 24
25 Function 1: Average & Standard Deviation The average (av) of array x[0..n-1] is computed as the sum of all elements divided by n. The variance is defined as the average of the squared deviations from the average value. The Standard Deviation is the square root of the variance. n 1 n x = xi, v = i = n n i= 0 i= 0 2 ( x x ), σ v Prof. Amr Goneid, AUC 25
26 Function stat void stat ( const float x[ ], int n, float &av, float &sd) { float d; float var = 0; av = average(x,n); for (int i = 0; i < n ; i++) { d = x[i] av ; var += d*d; } sd = sqrt(var / float(n)); } Prof. Amr Goneid, AUC 26
27 Function 2: Linear Search The idea of a linear search is to walk through the entire array until a target value is located. If found, its location is returned. If the target is not located some type of indicator needs to be returned Prof. Amr Goneid, AUC 27
28 Linear Search Function // Searches an integer array of size (n) // for a given element (the target) // Array elements ranging from 0 to // n - 1 are searched for an element // equal to target. // Returns the subscript of target if // found; otherwise, returns -1. Prof. Amr Goneid, AUC 28
29 linsearch Function (Cont.) int linsearch (const int a[ ], int target, int n) { for (int i = 0; i < n; i++) if (a[i] == target) return i; // All elements were tested without success. return -1; } // end linsearch Prof. Amr Goneid, AUC 29
30 Function 3: Selection Sort Assume elements to be in locations 0..n-1 Let (i) be the start of a sub-array of at least 2 elements, i.e. i = 0.. n-2 for each i = 0.. n-2 find smallest element in sub-array a[i] to a[n-1]. swap that element with that at the start of the sub-array. Prof. Amr Goneid, AUC 30
31 How it works Prof. Amr Goneid, AUC 31
32 Selection Sort Algorithm void selectsort (itemtype a[ ], int n) { int i, j, m; for (i = 0; i < n-1; i++) { m = i ; for ( j = i+1; j < n; j++) if (a[j] < a[m]) m = j ; swap (a[i], a[m]); } } Prof. Amr Goneid, AUC 32
33 Function 4: Bubble Sort The general idea is to compare adjacent elements and swap if necessary Assume elements to be in locations 0..n-1 Let (i) be the index of the last element in a subarray of at least 2 elements, i.e. i = n Prof. Amr Goneid, AUC 33
34 Bubble Sort Algorithm for each i = n-1 1 Compare adjacent elements a j and a j+1, j = 0..i-1 and swap them if a j > a j+1 This will bubble the largest element in the sub-array a[0..i] to location (i). Prof. Amr Goneid, AUC 34
35 How it works Prof. Amr Goneid, AUC 35
36 Bubble Sort Algorithm void bubblesort (itemtype a[ ], int n) { int i, j; bool swapped; for (i = n; --i >= 0; ) { swapped = false; for (j = 0; j < i; j++) { if (a[j] > a[j+1] ) { swap(a[j], a[j+1]); swapped = true; } } if (!swapped) return; } } Prof. Amr Goneid, AUC 36
37 Function 5: Binary Search Prof. Amr Goneid, AUC 37
38 Binary Search: How it works Assume elements in locations a[1]..a[n] to be already sorted in ascending order put the key (x) we are searching for in location a[0]. Set Low index L=1 and High index H=n Let (m) be the index of the approximate middle between L and H do compute middle location (m) if (L > H) there is no hope to find (x) in the array, so set m = 0 else if x < a[m] discard a[m]..a[h] by setting H = m-1, else if x > a[m] discard a[1]..a[m] by setting L = m+1, else, x is found at a[m] while x is not yet found at a[m] return the location (m), if zero it is not found Prof. Amr Goneid, AUC 38
39 Binary Search Algorithm int binsearch (itemtype a[ ], int n, itemtype x) { int L, m, H; a[0] = x; L = 1; H = n; do { m = (L + H)/2; if (L > H) m = 0; else if (x < a[m]) H = m-1; else L = m+1; } while (a[m]!= x); return m; } Prof. Amr Goneid, AUC 39
40 2-D Arrays Prof. Amr Goneid, AUC 40
41 2-D Arrays Declaration & Indexing 2-D Arrays as Parameters Simple Matrix Operations Other Applications Prof. Amr Goneid, AUC 41
42 2-D Arrays Useful in representing a variety of data, e.g. Tables, Matrices, Graphs and Images Day Fri Sat Sun j City Cairo i Tanta Alex A Table of Temp. A Matrix of Integers Prof. Amr Goneid, AUC 42
43 2-D Arrays Bit Map Color Image Zoomed showing Pixels Prof. Amr Goneid, AUC 43
44 Declaration & Indexing Declaration: <element-type> <arrayname> [size 1][size2]; e.g. double table[nrows] [NCOLS]; Indexing: table[2] [4]; Row# 0.. NROWS Column# 0.. NCOLS Prof. Amr Goneid, AUC 44
45 Example const int NRows = 11; const int Seats_In_Row = 9; string seatplan [NRows][Seats_In_Row]; Prof. Amr Goneid, AUC 45
46 Initialization const int NRows = 2; const int NCols = 3; float matrix[nrows][ncols] ={{5.0,4.5, 3.0}, {-16.0, -5.9, 0.0}}; We use nested loops with twodimensional arrays Prof. Amr Goneid, AUC 46
47 2-D Arrays as Parameters In 1-D arrays, it is not necessary to specify the size: void display (int A[ ], int n); To follow the same convention with 2-D arrays: void display (int B[ ] [ ], int n, int m); This will not work, because 2-D arrays are stored as 1-D arrays of arrays (1-D arrays of rows) Hence, beside the base address (name) we must also specify the number of columns, e.g. void display (int B[ ][Ncol], int n, int m); Prof. Amr Goneid, AUC 47
48 SumArray.cpp // FILE: SumArray.cpp // CALCULATES THE SUM OF THE ELEMENTS IN THE // FIRST rows ROWS OF AN ARRAY OF FLOATING POINT // VALUES WITH NCOLS (A CONSTANT) COLUMNS. // Pre: The type int constant NCOLS is // defined (NCOLS > 0) and the // array element values are defined and rows > 0. // Post: sum is the sum of all array element // values. // Returns: Sum of all values stored in the // array. Prof. Amr Goneid, AUC 48
49 SumArray.cpp float sumarray (float a[ ][NCOLS], int rows) { float sum = 0.0; // Add each array element value to sum. for (int r = 0; r < rows; r++) for (int c = 0; c < NCOLS; c++) sum += a[r][c]; } return sum; Prof. Amr Goneid, AUC 49
50 Higher Dimensions const int people = 10; const int years = 5; money sales[people][years][12]; Prof. Amr Goneid, AUC 50
51 Simple Matrix Operations Matrix Addition Matrix Transpose Matrix Multiplication Prof. Amr Goneid, AUC 51
52 Matrix Addition Given two matrices of the same dimensions A nm and B nm. Their sum is a matrix of the same dimensions C nm such that C ij = A ij + B ij Prof. Amr Goneid, AUC 52
53 Matrix Addition // N, M are global constants void matadd (float A[ ] [M], float B[ ] [M], float C[ ] [M]) { for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) C[i][j] = A[i][j] + B[i][j]; } Prof. Amr Goneid, AUC 53
54 Matrix Transpose Given a matrix A nm, its transpose is A T = B mn with elements B ji = A ij // N, M are global constants void mattranspose (float A[ ] [M], float B[ ] [N]) { for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) B[j][i] = A[i][j] ; } Prof. Amr Goneid, AUC 54
55 Example:Matrix Multiplication // Multiply matrix A(N rows x L columns) by matrix // B(L rows x M columns) to produce a matrix // C(N rows x M columns). // The elements of matrix C are: // C(i,j) = sum over k from 1 to L of {A(i,k) * B(k,j)} // with i = 1 to N, j = 1 to M // N, M, L are global constants Prof. Amr Goneid, AUC 55
56 Matrix Multiplication void matmult (float A[ ] [L], float B[ ] [M], float C[ ] [M]) { for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { C[i][j] = 0; for (int k = 0; k < L; k++) C[i][j] += A[i][k] * B[k][j]; } } Prof. Amr Goneid, AUC 56
57 Image Application Image A has random gray levels between 0(Black) and 255(White). What is the algorithm used to convert Image A to the binary image B? Image A Image B Prof. Amr Goneid, AUC 57
58 Image Application Same algorithm would do this. 256 Gray Levels 2 Levels(Binary) Prof. Amr Goneid, AUC 58
59 Flip Left to Right Prof. Amr Goneid, AUC 59
60 Flip Upside Down Prof. Amr Goneid, AUC 60
61 Flip 90 Degrees to the Left Prof. Amr Goneid, AUC 61
9.1 The Array Data Type. Data Structures Arrays and Structs. Arrays. Arrays. Array Declaration. Arrays. Array elements have a common name
9.1 The Array Data Type Data Structures Arrays and Structs Chapter 9 Array elements have a common name The array as a whole is referenced through the common name Array elements are of the same type the
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using
More informationMatrices. Jordi Cortadella Department of Computer Science
Matrices Jordi Cortadella Department of Computer Science Matrices A matrix can be considered a two-dimensional vector, i.e. a vector of vectors. my_matrix: 3 8 1 0 5 0 6 3 7 2 9 4 // Declaration of a matrix
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 1. Introduction Prof. amr Goneid, AUC 1 1. Introduction Software for Problem Solving Software Production Process Top-Down Design Modules
More informationArrays. Week 4. Assylbek Jumagaliyev
Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays
More informationCSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions)
CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion (Solutions) Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return
More informationLab #10 Multi-dimensional Arrays
Multi-dimensional Arrays Sheet s Owner Student ID Name Signature Group partner 1. Two-Dimensional Arrays Arrays that we have seen and used so far are one dimensional arrays, where each element is indexed
More informationChapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:
Chapter 8 Arrays and Strings Objectives In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationC++ PROGRAMMING SKILLS Part 4: Arrays
C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median
More informationBITG 1113: Array (Part 1) LECTURE 8
BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)
More informationLecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)
Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of
More information(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.
CS 7A - Fall 2016 - Midterm 1 10/20/16 Write responses to questions 1 and 2 on this paper or attach additional sheets, as necessary For all subsequent problems, use separate paper Do not use a computer
More informationArrays. Arrays are of 3 types One dimensional array Two dimensional array Multidimensional array
Arrays Array is a collection of similar data types sharing same name or Array is a collection of related data items. Array is a derived data type. Char, float, int etc are fundamental data types used in
More informationCS 261 Data Structures. Big-Oh Analysis: A Review
CS 261 Data Structures Big-Oh Analysis: A Review Big-Oh: Purpose How can we characterize the runtime or space usage of an algorithm? We want a method that: doesn t depend upon hardware used (e.g., PC,
More informationBITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)
BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional
More informationAn array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.
Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous
More informationLecture 6 Sorting and Searching
Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list
More informationReview of Important Topics in CS1600. Functions Arrays C-strings
Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why
More informationKingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS
Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table
More informationMaltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers
Maltepe University Computer Engineering Department BİL 133 Algoritma ve Programlama Chapter 8: Arrays and pointers Basics int * ptr1, * ptr2; int a[10]; ptr1 = &a[2]; ptr2 = a; // equivalent to ptr2 =
More informationAlgorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu Measure Algorithm Efficiency Space utilization: amount of memory required Time efficiency: amount of time required to accomplish
More informationCSE 230 Intermediate Programming in C and C++ Arrays and Pointers
CSE 230 Intermediate Programming in C and C++ Arrays and Pointers Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Definition: Arrays A collection of elements
More informationHigh Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd
El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays
More informationChapter 01 Arrays Prepared By: Dr. Murad Magableh 2013
Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013 One Dimensional Q1: Write a program that declares two arrays of integers and fills them from the user. Then exchanges their values and display the
More informationCSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion
CSCE 110 Dr. Amr Goneid Exercise Sheet (7): Exercises on Recursion Consider the following recursive function: int what ( int x, int y) if (x > y) return what (x-y, y); else if (y > x) return what (x, y-x);
More informationMatrix Multiplication
Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2013 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2013 1 / 32 Outline 1 Matrix operations Importance Dense and sparse
More informationChapter 10 - Notes Applications of Arrays
Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a
More informationArrays. Systems Programming Concepts
Arrays Systems Programming Concepts Arrays Arrays Defining and Initializing Arrays Array Example Subscript Out-of-Range Example Passing Arrays to Functions Call by Reference Multiple-Subscripted Arrays
More informationMatrix Multiplication
Matrix Multiplication CPS343 Parallel and High Performance Computing Spring 2018 CPS343 (Parallel and HPC) Matrix Multiplication Spring 2018 1 / 32 Outline 1 Matrix operations Importance Dense and sparse
More informationChapter 7 Array. Array. C++, How to Program
Chapter 7 Array C++, How to Program Deitel & Deitel Spring 2016 CISC 1600 Yanjun Li 1 Array Arrays are data structures containing related data items of same type. An array is a consecutive group of memory
More informationPrinciples of Programming. Chapter 6: Arrays
Chapter 6: Arrays In this chapter, you will learn about Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Simple Searching
More informationData Structures and Algorithms for Engineers
0-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationObjectives of This Chapter
Chapter 6 C Arrays Objectives of This Chapter Array data structures to represent the set of values. Defining and initializing arrays. Defining symbolic constant in a program. Using arrays to store, list,
More informationUNIT 2 ARRAYS 2.0 INTRODUCTION. Structure. Page Nos.
UNIT 2 ARRAYS Arrays Structure Page Nos. 2.0 Introduction 23 2.1 Objectives 24 2.2 Arrays and Pointers 24 2.3 Sparse Matrices 25 2.4 Polynomials 28 2.5 Representation of Arrays 30 2.5.1 Row Major Representation
More informationHow to declare an array in C?
Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous values.
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)
Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8
More informationSolutions to Chapter 8
Solutions to Chapter 8 Review Questions 1. a. True 3. b. False 5. b. False 7. c. index 9. d. ary[0] = x; 11. e. sorting 13. e. sequential 15. a. A two-dimensional array can be thought of as an array of
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types
Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using
More informationIntroduction to Arrays
Introduction to Arrays One Dimensional Array. Two Dimensional Array. Inserting Elements in Array. Reading Elements from an Array. Searching in Array. Sorting of an Array. Merging of 2 Arrays. What is an
More informationChapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 Chapter 6 Arrays Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 6 - Arrays 6.1 Introduction 6.2 Arrays 6.3 Declaring Arrays 6.4 Examples Using Arrays
More informationArrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Array Many applications require multiple data items that have common
More informationCreating a C++ Program
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.
More informationLecture 2. Two-Dimensional Arrays
Lecture 2 Two-Dimensional Arrays 1 Lecture Content 1. 2-D Array Basics 2. Basic Operations on 2-D Array 3. Storing Matrices with Special Properties 4. Applications of 2-D Array 2 1. 2-D Array Basics An
More informationCSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011
CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011 Date: 01/18/2011 (Due date: 01/20/2011) Name and ID (print): CHAPTER 6 USER-DEFINED FUNCTIONS I 1. The C++ function pow has parameters.
More informationFor loops, nested loops and scopes. Jordi Cortadella Department of Computer Science
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science Outline For loops Scopes Nested loops Introduction to Programming Dept. CS, UPC 2 Calculate x y Algorithm: repeated multiplication
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 8 Array typical problems, Search, Sorting Department of Computer Engineering Outline
More informationOne Dimension Arrays 1
One Dimension Arrays 1 Array n Many applications require multiple data items that have common characteristics In mathematics, we often express such groups of data items in indexed form: n x 1, x 2, x 3,,
More informationC Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables
1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position
More informationProblem Solving: Storyboards for User Interaction
Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:
More informationArrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal
Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review what an array is Review how to declare arrays Review what reference variables are Review how to pass arrays to methods Review
More informationObjectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:
Objectives Chapter 8 Arrays and Strings In this chapter, you will: Learn about arrays Declare and manipulate data into arrays Learn about array index out of bounds Learn about the restrictions on array
More informationProgramming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,
More informationC How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.
C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of
More informationTotal 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.
The American University in Cairo Computer Science & Engineering Department CSCE 106 Instructor: Final Exam Fall 2010 Last Name :... ID:... First Name:... Section No.: EXAMINATION INSTRUCTIONS * Do not
More informationThe American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science
The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Khalil Final Exam Fall 2013 Last Name :... ID:... First Name:... Form
More informationCSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid
CSCE 110 Notes on Recursive Algorithms (Part 12) Prof. Amr Goneid 1. Definition: The expression Recursion is derived from Latin: Re- = back and currere = to run, or to happen again, especially at repeated
More informationThe American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science
The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science Instructor: Dr. Howaida Ismail Final Exam Spring 2013 Last Name :... ID:... First Name:...
More informationColumns A[0] A[0][0] = 20 A[0][1] = 30
UNIT Arrays and Strings Part A (mark questions). What is an array? (or) Define array. An array is a collection of same data type elements All elements are stored in continuous locations Array index always
More informationArrays and Applications
Arrays and Applications 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2014 Instructor: Dr. Asish Mukhopadhyay What s an array Let a 0, a 1,, a n-1 be a sequence
More informationProgramming 1. Lecture 6 Structured data types. Arrays
Programming 1 Lecture 6 Structured data types. Arrays Objectives Understand the difference between simple and structured data types Manage the following structured data types: one-dimensional and two-dimensional
More informationOutline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x
Outline For loops, nested loops and scopes For loops Scopes Jordi Cortadella Department of Computer Science Nested loops Calculate x y Algorithm: repeated multiplication x x x x y times y x i p=x i 4 3
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities
PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities Continuous Internal Evaluation Test 2 Date: 0-10- 2017 Marks: 0 Subject &
More informationOutline. Arrays. Function Arguments. Arrays. Adding Two Arrays /* */ /* This program adds 2 arrays */ ELEC 330 1
ELEC 330 1 Arrays ELEC 06 Computer Applications for Electrical Engineers Dr. Ron Hayne Outline Arrays Statistical Measurements Functions Revisited 06_C6 Arrays One-dimensional array List of values Arranged
More informationBefore we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.
Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1 Consider the bubble function one more
More informationArrays and functions Multidimensional arrays Sorting and algorithm efficiency
Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing
More informationCHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Arrays 3. Declaring Arrays 4. Examples Using Arrays 5. Multidimensional Arrays 6. Multidimensional Arrays Examples 7. Examples Using
More informationMaltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays
Maltepe University Computer Engineering Department BİL 133 Algorithms and Programming Chapter 8: Arrays What is an Array? Scalar data types use a single memory cell to store a single value. For many problems
More informationArrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Arrays CS10001: Programming & Data Structures Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Array Many applications require multiple data items that have common
More informationArrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection
Morteza Noferesti Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #04
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Arrays and Strings Practice Sheet #04 Date: 24-01-2017 Instructions: For the questions consisting code segments,
More informationFeatures of C. Portable Procedural / Modular Structured Language Statically typed Middle level language
1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC
More informationProgramming. C++ Basics
Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationArrays, Vectors Searching, Sorting
Arrays, Vectors Searching, Sorting Arrays char s[200]; //array of 200 characters different type than class string can be accessed as s[0], s[1],..., s[199] s[0]= H ; s[1]= e ; s[2]= l ; s[3]= l ; s[4]=
More informationArrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept
Arrays in C Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur 1 Basic Concept Many applications require multiple data items that have common characteristics.
More informationC/C++ Programming Lecture 18 Name:
. The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search
More information8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017
8. The C++ language, 1 Programming and Algorithms II Degree in Bioinformatics Fall 2017 Hello world #include using namespace std; int main() { } cout
More informationName MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs
More informationLECTURE 17. Array Searching and Sorting
LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationARRAYS(II Unit Part II)
ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful
More informationAPS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:
Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationCS Spring 05 - MidTerm
CS1411-160 - Spring 05 - MidTerm March 8, 2005 1. When working at the keyboard, the user generates a newline character by pressing the Enter or Return key. 2. In the design of a flag-controlled loop, the
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationChapter4: Data Structures. Data: It is a collection of raw facts that has implicit meaning.
Chapter4: s Data: It is a collection of raw facts that has implicit meaning. Data may be single valued like ID, or multi valued like address. Information: It is the processed data having explicit meaning.
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists Prof. amr Goneid, AUC 1 Linked Lists Prof. amr Goneid, AUC 2 Linked Lists The Linked List Structure Some Linked List
More informationWhy arrays? To group distinct variables of the same type under a single name.
Lesson #7 Arrays Why arrays? To group distinct variables of the same type under a single name. Suppose you need 100 temperatures from 100 different weather stations: A simple (but time consuming) solution
More informationChapter 6 part 1. Data Types. (updated based on 11th edition) ISBN
Chapter 6 part 1 Data Types (updated based on 11th edition) ISBN 0-321 49362-1 Chapter 6 Topics Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationCSE030 Fall 2012 Final Exam Friday, December 14, PM
CSE030 Fall 2012 Final Exam Friday, December 14, 2012 3-6PM Write your name here and at the top of each page! Name: Select your lab session: Tuesdays Thursdays Paper. If you have any questions or need
More informationLinear Search. Sorting Algorithms. linear search code. Sorting in Ascending Order Selection Sort. Selection sort algorithm
Linear Search Additional CPSC1620 topics Searching, Sorting, Big(O) template functions, classes The idea of a linear search is to walk through the entire list until the value is found. The list does not
More informationTwo common problems in processing arrays. Examples. Algorithm for searching over a sorted array is much more efficient than over an unsorted array.
Search and Sort an Array Programming Examples Using Arrays Pei-yih Ting Two common problems in processing arrays Searching an array to determine the location of a particular value. Sorting an array to
More information