/
Module 7  –  Part I Module 7  –  Part I

Module 7 – Part I - PowerPoint Presentation

pamella-moone
pamella-moone . @pamella-moone
Follow
353 views
Uploaded On 2019-12-09

Module 7 – Part I - PPT Presentation

Module 7 Part I 1D Arrays and Lots of Brackets 1122019 CSE 1321 Module 7 1 Creating and Initializing Arrays 1122019 CSE 1321 2 Array Creation Example 1 Create an empty array By default 0s are stored in the array ID: 769760

1321 int cout array int 1321 array cout myarray 2019 cse endl colors sum list size include create average

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Module 7 – Part I" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Module 7 – Part I 1D Arrays and Lots of Brackets 11/2/2019 CSE 1321 Module 7 1

Creating and Initializing Arrays 11/2/2019 CSE 1321 2

Array Creation Example 1) Create an empty array. By default, 0s are stored in the array. int myArray [5];2) Creates and initializes in one line. The stored values are show in { }; int myArray [] = {10, 20, 30, 40, 50}; 11/2/2019 CSE 1321 3

3. Accessing InformationCopy information out of a particular slot Example: CREATE clientAge // IntegerclientAge ← myArray[4]This copies information from the fifth slot (slot four) into the variable clientAge11/2/2019 CSE 1321 4

Horrible, but works…// creates empty array with 5 slots, 0-4 CREATE myArray [5] //Assigns literal values to each index in the array.myArray [0] ← 10myArray[1] ← 20myArray[2] ← 30myArray[3] ← 40myArray [4] ← 50 11/2/2019 CSE 1321 5 Ps

Accessing and Modifyingint main() {int myArray[]={7,4,5,3,2,1,8};//Accessingint secondNumber = myArray[1]; cout <<secondNumber <<endl;//ModifyingmyArray[3] = 42; cout<<myArray[3]<<endl ;}11/2/2019 CSE 1321 Module 46

Traversing the ArrayYou will use a loop to visit every cell of the array Problem: create an array of 5 bytes and fill each slot with the number 42Solution: CREATE myArray [5] FOR i ← 0 to 4 myArray[i] ← 42END FOR 11/2/2019CSE 1321 7 Ps

Traversing an ArrayUse a FOR, WHILE or DO-WHILE starting at index 0 and going thru .length-1 11/2/2019 CSE 1321 8 int main() { int myArray[]={ 7 , 4 , 5 , 3 , 2 , 1 , 8 }; for ( int i = 0;i<7;i++){cout <<myArray[i]<<endl;}}

Another TraceCREATE smallestSoFarsmallestSoFar ← randomArray[0]FOR counter ← 1 to 4 IF (smallestSoFar > randomArray[counter]) THEN smallestSoFar ← randomArray[counter] END IFEND FOR// Done 0 1 2 3 4 42 17 42 -8 4 -8 smallestSoFar 5 counter Ps

Finding the Minimum using a Method (Example1) int FindMin (int* A, int size ) { int temp = A[0]; for (int i=1;i< size;i++){if (temp > A[ i]) { temp = A[i];}} return temp;} int main() { int myArray[]={7,4,5,3,2,1,8,10}; int size= sizeof(myArray)/sizeof (int);cout <<FindMin( myArray,size )<< endl ; } 11/2/2019 CSE 1321 10

11/2/2019CSE 1321 Module 4 11 Finding the Minimum using a Method (Example2) int FindMin ( int* A ) {   int temp = A[0]; for (int i=1;i< *A;i++){   if (temp > A[i])   { temp = A[i];}  }   return temp;}

Finding the sum or average using a method METHOD FINDAVERAGE ( parameter: nums[]) BEGIN sum ← 0 FOR i ← 0 to nums.length - 1 // MOST IMPORTANT LINE IS HERE sum = sum + nums[i] ENDFOR average = sum / nums.length return average END FINDAVERAGE 11/2/2019 12 CSE 1321 Ps

Finding a sum and or average using a method (Example 1)float FindAverage (int* B,int size) {int sum = 0;   for (int i = 0; i < size; i++) {    sum += B[i];   }return (float)sum / size; } int main() {int myArray[]={7,4,5,3,2,1,8,10,11};int size= sizeof(myArray)/ sizeof(int);cout<< FindAverage(myArray,size ); } 11/2/2019CSE 1321 13

Finding a sum and or average using a method (Example 2)float FindAverage (int* B) { int sum=0;   for (int i = 0; i < *B; i++)   {    sum += B[i];   }return sum/ *B; } 11/2/2019 CSE 1321 14

Defining a 2D arrayCREATE array nums [numRows][numColumns] 11/2/2019 CSE 1321 15 Ps

C++ Define a multi-dimension ArrayTwo dimensional array:int two_d[10][20];Three dimensional array: int three_d[10][20][30]; 11/2/2019 CSE 1321 16

Working with 2D arrays Usually involves nested loops, as shown below. Problem Statement: Create an array of 4 rows and 5 columns. Populate the array with the numbers 1-20. ---------------------------------------------------------------------Create array grid[4][5] count ← 1FOR each element in a row FOR each element in a column    grid[row][col] = count count ← count + 1 END INNER FOREND FOR 11/2/2019 CSE 1321 17

11/2/2019 CSE 1321 18 Working with 2D arrays #include <iostream> using namespace std; int main() { int count = 1; //declare a 2D array with 4 rows and 5 columns int grid [4][5]; //getting the rows in the array for (int row = 0; row < 4; row++) { //populate the values in the array (row) we're currently on from the outer loop for (int column = 0; column < 5; column++){ grid[row][column] = count; count++;}}

11/2/2019CSE 1321 Module 4 19 //for each array in the grid (represented by rows)for(auto& rows: grid){//for each element in the array (row) we're currently on from the outer loop for (auto& elem: rows) {cout <<elem <<","; if (elem % 5 == 0){ cout<<" "<<endl; }}}} Working with 2D arrays (Continue……)

Working with Array Lists#include <iostream> //print and take input#include <string> //use string variables #include <list> // for list operations #include <vector> //use vectors#include <algorithm> #include <iterator> //create iterationusing namespace std; 11/2/2019 CSE 1321 20

11/2/2019CSE 1321 Module 4 21 CONTINUED FROM PREVIOUS SLIDEint main() { list<string> colors; // declaring list // initializing array colors.assign(1,"purple"); colors.push_back("pink"); colors.push_back ("green"); cout<<"Does the arraylist contain the color pink?"<<endl;bool found = (find(colors.begin(), colors.end(), "pink") != colors.end());if (found){cout<<"Yes"<<endl;cout<<"\n";} else { cout<<"No"<<endl;cout<<"\n"; }// Printing the assigned list cout << "The list after inserting multiple elements is : "<<endl ; for (list<string>::iterator i=colors.begin(); i!=colors.end(); i ++) cout << * i << " "; cout << endl ;

Working with Array Lists CONTINUED FROM PREVIOUS SLIDE colors.remove("green"); // Printing the assigned list cout << "The list after inserting multiple elements is : "<< endl; for (list<string>::iterator i=colors.begin (); i!=colors.end(); i++) cout << *i << " “<<endl; // Create iterator pointing to first element list<string>::iterator it = colors.begin(); // Advance the iterator by 1 positions, advance(it, 0); // Now iterator it points to 1st element cout << "1st element = " << *it << endl ;} 11/2/2019 CSE 1321 22

11/2/2019CSE 1321 Module 4 23 Using vector:#include <vector> #include <iostream> #include < numeric> using namespace std; int main() { vector<float> v; float input; cout << " Please enter numbers you want to find the mean of:" <<endl; while ( cin >> input) { v.push_back (input); } float average = accumulate( v.begin (), v.end (), 0.0 )/ v.size (); cout << "The average is" << average << endl; return 0; }

11/2/2019CSE 1321 Module 4 24 References :https://stackoverflow.com/questions/28574346/find-average-of-input-to-vector-c