C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

CHAPTER 10 ARRAYS II Applications and Extensions.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Computer Science 1620 Loops.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Arrays Multi-dimensional initialize & display Sample programs Sorting Searching Part II.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 2006 Pearson Education, Inc. All rights reserved Arrays.
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
Chapter 8 Arrays and Strings
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
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.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 6 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
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.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
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 as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Objectives You should be able to describe: One-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Search,Sort,Recursion.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Array Data Structure Chapter 6
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:

C++ Arrays

Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional arrays Reading and Writing one-dimensional arrays Reading and Writing one-dimensional arrays Passing one-dimensional arrays to functions Passing one-dimensional arrays to functions Manipulating one-dimensional arrays: Manipulating one-dimensional arrays: Searching for something Searching for something Rearranging stored values Rearranging stored values Computing and storing series of values Computing and storing series of values

What Is An Array? An array is a collection of values that have the same data type, e.g. An array is a collection of values that have the same data type, e.g. A collection of int data values or A collection of int data values or A collection of bool data values A collection of bool data values We refer to all stored values in an array by its name We refer to all stored values in an array by its name If we would like to access a particular value stored in an array, we specify its index (i.e. its position relative to the first array value) If we would like to access a particular value stored in an array, we specify its index (i.e. its position relative to the first array value) The first array index is always 0 The first array index is always 0 The second value is stored in index 1 The second value is stored in index 1 Etc. Etc.

How To Declare Arrays To declare an array in C++, you should specify the following things To declare an array in C++, you should specify the following things The data type of the values which will be stored in the array The data type of the values which will be stored in the array The name of the array (i.e. a C++ identifier that will be used to access and update the array values) The name of the array (i.e. a C++ identifier that will be used to access and update the array values) The dimensionality of the array: The dimensionality of the array: One dimensional (i.e. list of values ), One dimensional (i.e. list of values ), Two-dimension array (a matrix or a table), etc. Two-dimension array (a matrix or a table), etc. The size of each dimension The size of each dimension Examples Examples int x[10]; // An integer array named x with size 10 int x[10]; // An integer array named x with size 10 float GPA[30]; // An array to store the GPA for 30 students float GPA[30]; // An array to store the GPA for 30 students int studentScores[30][5]; // A two-dimensional array to store the scores of 5 exams for 30 students int studentScores[30][5]; // A two-dimensional array to store the scores of 5 exams for 30 students

One-dimensional Arrays We use one-dimensional (ID) arrays to store and access list of data values in an easy way by giving these values a common name, e.g. We use one-dimensional (ID) arrays to store and access list of data values in an easy way by giving these values a common name, e.g. int x[4]; // all values are named x x[0] = 10; // the 1 st value is 10 x[0] = 10; // the 1 st value is 10 x[1] = 5; // the 2 nd value is 5 x[1] = 5; // the 2 nd value is 5 x[2] = 20; // the 3 rd value is 20 x[2] = 20; // the 3 rd value is 20 x[3] = 30; // the 4 th value is 30 x[3] = 30; // the 4 th value is x[0] x[1] x[2] x[3]

Array Indices and Out-of-bound Run-time Error All C++ one-dimensional arrays with N entries start at index 0 and ended at index N-1 All C++ one-dimensional arrays with N entries start at index 0 and ended at index N-1 const int N=5; const int N=5; int v[N]; // this array contains 5 entries int v[N]; // this array contains 5 entries It is a common error to try to access the N th entry, e.g. v[5] or V[N], since the index of the last array entry is N-1, not N It is a common error to try to access the N th entry, e.g. v[5] or V[N], since the index of the last array entry is N-1, not N

Initializing One-dimensional Arrays There are two common ways to initialize one-dimensional arrays There are two common ways to initialize one-dimensional arrays Using for loop, e.g. Using for loop, e.g. int x[10]; int x[10]; for( int index=0; index<10; index++) x [index] = index+1 ; for( int index=0; index<10; index++) x [index] = index+1 ; Specifying list of values while declaring the 1D array, e.g. Specifying list of values while declaring the 1D array, e.g. int x[10] = {1,2,3,4,5,6,7,8,9,10}; int x[10] = {1,2,3,4,5,6,7,8,9,10}; int y[ ] = {0,0,0}; // this array contains 3 entries with 0 values int y[ ] = {0,0,0}; // this array contains 3 entries with 0 values double z[100] = {0}; // this array contains 100 double z[100] = {0}; // this array contains 100 // entries, all of which are initialized by 0 // entries, all of which are initialized by 0 double w[20] = {5,3,1}; // this array contains 20 entries, double w[20] = {5,3,1}; // this array contains 20 entries, // the first three entries are initialized by 5, 3, and1 respectively // the first three entries are initialized by 5, 3, and1 respectively // while the remaining 17 entries are automatically initialized by 0 // while the remaining 17 entries are automatically initialized by 0 bool pass[10] = {true, true}; // this array contains 10 entries. bool pass[10] = {true, true}; // this array contains 10 entries. // The first two entries are initialized to true, while the remaining 8 // The first two entries are initialized to true, while the remaining 8 // entries are automatically initialized to false // entries are automatically initialized to false

Storing Values in 1D Arrays Wrong way: Wrong way: Int x[10]; cin >> x ; // you cannot do that unless you // overload the >> operator! // overload the >> operator! // Wait until you take CSCI-110 // Wait until you take CSCI-110 Correct way: Correct way: int x[10]; int x[10]; // Reading one value at a time from the user: // Reading one value at a time from the user: for (int index=0; index<10; index++) for (int index=0; index<10; index++) cin >> x[index]; cin >> x[index];

Displaying Values Stored in 1D Arrays Wrong way: Wrong way: Int x[10]; cout << x; // you cannot do that unless you // overload the << operator! // overload the << operator! // Wait until you take CSCI-110 // Wait until you take CSCI-110 Correct way: Correct way: int x[10]; int x[10]; // Displaying one value per line to the user: // Displaying one value per line to the user: for (int index=0; index<10; index++) for (int index=0; index<10; index++) cout << x[index] << endl; cout << x[index] << endl;

Example: Read Values and Print them in Reverse Order #include #include void main() { int x[5], index; int x[5], index; cout << “Please enter five integer values: “; cout << “Please enter five integer values: “; for( index=0; index > x[index]; for( index=0; index > x[index]; cout << “The values in reverse order are: “; cout << “The values in reverse order are: “; for (index=4; index>=0; index--) for (index=4; index>=0; index--) cout << setw(3) << x[index]; cout << setw(3) << x[index]; cout << endl; cout << endl;}

Passing 1D Arrays to Functions By default, arrays are passed to functions by reference even though we do not even write the reference operator (&) in front of the array name, why? By default, arrays are passed to functions by reference even though we do not even write the reference operator (&) in front of the array name, why? Since arrays store huge number of values, it is much faster and more economical to pass arrays by reference instead of wasting the time and memory copying the values of the arrays into another arrays Since arrays store huge number of values, it is much faster and more economical to pass arrays by reference instead of wasting the time and memory copying the values of the arrays into another arrays Example Example The following function signature accepts an array x as a reference parameter and it also accepts the number of entries in the array (i.e. its size) The following function signature accepts an array x as a reference parameter and it also accepts the number of entries in the array (i.e. its size) void process (int x[], int size);

Passing 1D Arrays to Functions Why don’t we use the following signature to pass an array to a function? Why don’t we use the following signature to pass an array to a function? void process (int x[10]); Unfortunately, using the above signature means that this particular function will only process a 1D array with size =10 (no more no less!) Unfortunately, using the above signature means that this particular function will only process a 1D array with size =10 (no more no less!) Of course we would like to write a function to handle different array sizes and that is why we use the following signature instead Of course we would like to write a function to handle different array sizes and that is why we use the following signature instead void process (int x[ ], int size); Visual Studio in particular does not issue any warnings or syntax error if you use the first signature and pass different array size, but other compliers do! Visual Studio in particular does not issue any warnings or syntax error if you use the first signature and pass different array size, but other compliers do!

Example 2: Passing 1D Array to A Function #include #include using namespace std; void display (string prompt, int x[ ], int n) { cout << prompt ; cout << prompt ; for (int i=0; i<n; i++) for (int i=0; i<n; i++) cout << setw(3) << x[i]; cout << setw(3) << x[i]; cout << endl; cout << endl;} void main() { int v[7] = {1,2,3,4}; int v[7] = {1,2,3,4}; int w[4] = {10,20,30,40}; int w[4] = {10,20,30,40}; display ("The values of v array : ", v, 7); display ("The values of v array : ", v, 7); display ("The values of w array : ", w, 4); display ("The values of w array : ", w, 4);}

Passing 1D Arrays As const Reference Parameters It is a good idea to pass arrays as const reference parameters if you want to assure that the functions will never change the values stored in the arrays, e.g. It is a good idea to pass arrays as const reference parameters if you want to assure that the functions will never change the values stored in the arrays, e.g. void display (const int x[ ], int size) { for (int i=0; i<size; i++) for (int i=0; i<size; i++) cout << x[i] << endl; cout << x[i] << endl;}

Array Manipulation I. Searching for Something…

Example: Searching for the smallest value in an array int smallest (const int x[ ], int n) { /* Assume for a moment that x[0] contains /* Assume for a moment that x[0] contains the smallest value */ the smallest value */ int min = x[0]; int min = x[0]; for (int i=1; i x[i]) min = x[i]; for (int i=1; i x[i]) min = x[i]; return min; return min;}

Example: Searching for The Index of The Smallest Value in An Array int min_index (int x[ ], int size, int start=0) { int index = start; int index = start; int min = x[index]; int min = x[index]; for (int i=index+1; i<size; i++) for (int i=index+1; i<size; i++) if (min>x[i]) if (min>x[i]) { min = x[i]; min = x[i]; index = i; index = i; } return index; return index;} Notice that: 1.The array is not sorted! 2.We use the parameter start to specify the start searching index. The 0 is the default value of this parameter 3.Later on, we will use this function to sort an array

Example: Searching for A Key in Unsorted Array Using Sequential Search int seqSearch (int x[ ], int size, int key) { for (int i=0; i<size; i++) for (int i=0; i<size; i++) if (key == x[i]) return i; if (key == x[i]) return i; return -1; return -1;} This function will return the index of the first value that equals to the key if exists, otherwise, it returns -1

Example: Searching for A Key in A Sorted Array Using Binary Search int bSearch (int x[ ],int n,int key) { int left=0, right=n-1; int left=0, right=n-1; do do { int mid = (left+right)/2; int mid = (left+right)/2; if (x[mid] == key) return mid; if (x[mid] == key) return mid; if (x[mid]<key) if (x[mid]<key) right=mid-1; right=mid-1; else else left=mid+1; left=mid+1; } while (left<=right); } while (left<=right); return -1; return -1;} NB. 1.The array x must be sorted! 2.Each loop iteration eliminate half of the current length of the array which makes this searching technique more faster than the sequential search 3.We stop searching when left>right and in this case we return with -1 to indicate that we could not find the key in the array

Array Manipulation II. Compute Something…

Example: Compute The Sum of Values Stored in An Array int sum (const int x[ ], int n) { int s = 0; int s = 0; for (int i=0; i<n; i++) s+=x[i]; for (int i=0; i<n; i++) s+=x[i]; return s; return s;}

Example: Compute The Mean Value of An Array double mean (const int x[ ], int n) { return (double) sum(x,n) / n; return (double) sum(x,n) / n;}

Example: Compute The Standard Deviation of An Array double stdiv (const int x[ ], int x) { double ss = 0.0, xBar = mean(x,n); double ss = 0.0, xBar = mean(x,n); for(int i=1; i<n; i++) ss+=pow(x[i]-xBar,2); for(int i=1; i<n; i++) ss+=pow(x[i]-xBar,2); if (n<30) if (n<30) return sqrt(ss/(n-1)); return sqrt(ss/(n-1)); else else return sqrt(ss/n); return sqrt(ss/n);}

Example: Compute The Range of Numbers int range (const int x[ ], int n) { return max (x, n) – min (x, n); return max (x, n) – min (x, n);}

Example: Compute The Median double median( double x[], int n) { sort (x, n); //assume sort( ) is already defined sort (x, n); //assume sort( ) is already defined if (n % 2 == 1) if (n % 2 == 1) return x[n/2+1]; return x[n/2+1]; else else return (x[n/2]+x[n/2+1)/2; return (x[n/2]+x[n/2+1)/2;}

Example: Multiplying An Array with A Constant Value void multiply( int x[ ], int n, const int value) { for (int i=0; i<n; i++) x[i] *= value; for (int i=0; i<n; i++) x[i] *= value;}

Example: Dot Product of Two Arrays int dotProduct( int x[ ], int y[ ], int n) { int sum = 0; int sum = 0; for (int i=0; i<n; i++) for (int i=0; i<n; i++) sum += x[i] * y[i]; sum += x[i] * y[i]; return sum; return sum;}

Example: Adding/Subtracting Two arrays void add (const int x[], const int y[], int z[], nt n) { for (int i=0; i<n; i++) z[i] = x[i] + y[i]; for (int i=0; i<n; i++) z[i] = x[i] + y[i];} void subtract (const int x[], const int y[], int z[], int n) { for (int i=0; i<n; i++) z[i] = x[i] - y[i]; for (int i=0; i<n; i++) z[i] = x[i] - y[i];}

Example: Set Intersection void intersection (const int set1[], int size1, const int set2[], int size2, int s[], int &size) { size=0; size=0; for(int i=0; i<size1; i++) for(int i=0; i<size1; i++) if (seqSearch(set2, size2, set1[i])>-1) if (seqSearch(set2, size2, set1[i])>-1) set[size++] = set1[i]; set[size++] = set1[i];}

Example: Set Difference void difference (const int set1[], int size1, const int set2[], int size2, int s[], int &size) { size=0; size=0; for(int i=0; i<size1; i++) for(int i=0; i<size1; i++) if (seqSearch(set2, size2, set1[i]) == -1) if (seqSearch(set2, size2, set1[i]) == -1) set[size++] = set1[i]; set[size++] = set1[i];}

Example: Set Union void union (const int set1[], int size1, const int set2[], int size2, int s[], int &size) { size = size1; size = size1; for(int i=0; i<size; i++) set[i]=set1[i]; for(int i=0; i<size; i++) set[i]=set1[i]; for(int i=0; i<size2; i++) for(int i=0; i<size2; i++) if (seqSearch(set, size, set2[i]) == -1) if (seqSearch(set, size, set2[i]) == -1) set[size++] = set2[i]; set[size++] = set2[i];}

Example: Converting An Integer Decimal Number to Binary Number #include #include void main() { short int binary[64]={0}; short int binary[64]={0}; int index, bits=0; int index, bits=0; unsigned int n; unsigned int n; cout << "Please enter an integer value: "; cout << "Please enter an integer value: "; cin >> n; cin >> n; cout << "The binary number is "; cout << "The binary number is "; if (n==0) if (n==0) cout << 0 << endl; cout << 0 << endl; else else { while (n>0) while (n>0) { binary[bits] = n % 2; binary[bits] = n % 2; n /= 2; n /= 2; bits++; bits++; } for (index=bits-1;index>=0;index--) for (index=bits-1;index>=0;index--) cout << binary[index]; cout << binary[index]; cout << endl; cout << endl; }} Guess what happens when you enter -1!

Example #2: Converting an integer decimal number to binary number #include #include void main() { short int binary[64]={0}; short int binary[64]={0}; int index, bits=0; int index, bits=0; unsigned int n; unsigned int n; cout << "Please enter an integer value: "; cout << "Please enter an integer value: "; cin >> n; cin >> n; cout << "The binary number is "; cout << "The binary number is "; if (n==0) if (n==0) cout << 0 << endl; cout << 0 << endl; else else { while (n>0) while (n>0) { binary[bits] = n % 2; binary[bits] = n % 2; n /= 2; n /= 2; bits++; bits++; } for (index=bits-1;index>=0;index--) for (index=bits-1;index>=0;index--) cout << binary[index]; cout << binary[index]; cout << endl; cout << endl; }} Guess what happens when you enter -1!

Example 4: Search for The Largest Value in 1D Array #include #include int largest (const int x[ ], int size) { // Assume for a moment that the largest // Assume for a moment that the largest // value is stored in x[0] // value is stored in x[0] int max = x[0]; int max = x[0]; for (int i=1; i<size; i++) if (max < x[i]) max = x[i]; for (int i=1; i<size; i++) if (max < x[i]) max = x[i]; return max; return max;}

Example 4 (Cont.) void main() { int array[10] = {4,1,6,3,-5,2,9,5,7,8}; int array[10] = {4,1,6,3,-5,2,9,5,7,8}; cout << “The largest stored value is “ cout << “The largest stored value is “ << largest (array,10) << largest (array,10) << endl; << endl;}

Exercise #1 Write a function to search for the smallest value stored in a ID array. Your function signature should be like this Write a function to search for the smallest value stored in a ID array. Your function signature should be like this int smallest (const int x[ ], int size); // purpose – find the smallest value // input – an array x and its size // output – the smallest value Write a C++ program to test your function

Example 5: Searching for The Index of the Largest Value in 1D Array #include #include using namespace std; int indexOfMaxValue(const int x[ ], int size) { // Assume for a moment that the largest value is stored in x[0] // Assume for a moment that the largest value is stored in x[0] int max = x[0]; int max = x[0]; int index = 0; int index = 0; for (int i=1; i<size; i++) for (int i=1; i<size; i++) if (max < x[i]) if (max < x[i]) { max = x[i]; max = x[i]; index = i; index = i; } return index; return index;}

Exercise#2 Write a function to search for the index of the smallest value stored in 1D array. Your function signature should be like this Write a function to search for the index of the smallest value stored in 1D array. Your function signature should be like this Int indexOfMinValue (const int x [ ], int size); // purpose – get the index of the smallest value stored in the array x // input – the array x and its size // output – the index of the smallest value Write a C++ program to test your function

Example 6: Insertion Sort void insertionSort(int x [], int n) { for(int i=0; i<n; i++) for(int i=0; i<n; i++) swap(x[indexOfMaxValue(x,n-i)], x[n-i-1]); swap(x[indexOfMaxValue(x,n-i)], x[n-i-1]);} void printArray(string p, int x[ ], int size) { cout << p; cout << p; for (int i=0; i<size; i++) cout << setw(3) << x[i]; for (int i=0; i<size; i++) cout << setw(3) << x[i]; cout << endl; cout << endl;} Get the index of the largest value stored in the range of [0,n-i] and swap this value in that index with x[n-i-1]

Example 6 (Cont.) void main() { int array[10] = {4,1,3,2,7,6,10,9,5,8}; int array[10] = {4,1,3,2,7,6,10,9,5,8}; printArray("Unsorted array:",array, 10); printArray("Unsorted array:",array, 10); insertionSort(array, 10); insertionSort(array, 10); printArray("Sorted array: ",array, 10); printArray("Sorted array: ",array, 10);}

Array Manipulation II. Compute… The sum of the values stored in an array The sum of the values stored in an array The mean and the standard deviation for the array values The mean and the standard deviation for the array values The range of the values stored in an array The range of the values stored in an array Multiplying array values by a constant Multiplying array values by a constant Dot-product of two arrays Dot-product of two arrays The intersection of two arrays (acting as sets) The intersection of two arrays (acting as sets) The union of two arrays (acting as sets) The union of two arrays (acting as sets) The difference between two arrays acting as sets The difference between two arrays acting as sets Binary representation for a given integer number Binary representation for a given integer number Generate Fibonacci series Generate Fibonacci series Generate geometric series Generate geometric series

Array Manipulation III. Rearranging values of an array Sorting an array (in ascending or descending order) using bubble sort Sorting an array (in ascending or descending order) using bubble sort Remove duplicate values from an array Remove duplicate values from an array Reversing the content of an array Reversing the content of an array