CSCE 1030
Ice Breaker Food Would You Rather What's your bucket list?
Agenda Array: • Definition & Example • Sorting array 2D-Array: • Definition & Example • Practice
What is array? Its example in real life? Array is an indexed sequence of elements, all the same type.
Example of array, multi-array: • Book pages • A dozen of eggs • An excel sheet
How to declare an array? Data type array name [size]; int array[4]; Initialize array int array [3] = {3, 4,5}; int array[3] ; array[0] = 3; array[1] = 4; array[2] =5; Accessing array For loop
1. for(i=0;i<n;i++) { 2. for(j=i+1;j<n;j++) { 3. if(arr[i]>arr[j]) { 4. temp =arr[i]; 5. arr[i]=arr[j]; 6. arr[j]=temp; 7. } 8. } 9. } 1. int size = 5; 2. int array[size] = {4, 5, 1, 3, 9}; 3. temp=0; int i=0; 4. int j=size- 1; 5. while( i < j){ 6. temp = array[i]; 7. array[i] = array[j]; 8. array[j] = temp; i++; 9. j--; 10. } Sort ascending order 1. for(i=0;i<n;i++) { 2. for(j=i+1;j<n;j++) { 3. if(arr[i]<arr[j]) { 4. temp =arr[i]; 5. arr[i]=arr[j]; 6. arr[j]=temp; 7. } 8. } 9. } Sort descending order Reverse array
Multi- dementional Array • How to initialize a 2D array? Data type array_name [row][col] int 2Darray [3][3] Input data: same as 1D array
Practice
Practice what is the value of sum after this code executes? 1. int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}}; 2. int sum = 0; 3. int col = matrix[0].length - 2; 4. for (int row = 0; row < 4; row++) { 5. sum = sum + matrix[row][col]; 6. }
Ice breaker • What's an "old person" thing that you do?
Agenda - Watch video of passing array as parameter in Function - Review function definition and its example - Practice problem
Function
Passing Array as Parameter in Function
C++ Function Declaration returnType functionName (parameter1, parameter2,...) { // function body } // function declaration void greet() { cout << "Hello World"; }
Example 1. #include <iostream> 2. using namespace std; 3. int addition (int a, int b) { 4. int r; 5. r=a+b; 6. return r; 7. } 8. int main () { 9. int z; 10. z = addition (5,3); 11. cout << "The result is " << z; 12. }
Practices • Write a program that calculates 6^5. Declare your own function to do this.
Practices Write a program with function to find 3 largest number of an array. Array size: 8 Input: {7, 12, 9, 15, 19, 32, 56, 70}

Array_C++_programming_example_and reviews2

  • 1.
  • 2.
    Ice Breaker Food Would YouRather What's your bucket list?
  • 3.
    Agenda Array: • Definition& Example • Sorting array 2D-Array: • Definition & Example • Practice
  • 4.
    What is array?Its example in real life? Array is an indexed sequence of elements, all the same type.
  • 5.
    Example of array, multi-array: •Book pages • A dozen of eggs • An excel sheet
  • 6.
    How to declarean array? Data type array name [size]; int array[4]; Initialize array int array [3] = {3, 4,5}; int array[3] ; array[0] = 3; array[1] = 4; array[2] =5; Accessing array For loop
  • 7.
    1. for(i=0;i<n;i++) { 2.for(j=i+1;j<n;j++) { 3. if(arr[i]>arr[j]) { 4. temp =arr[i]; 5. arr[i]=arr[j]; 6. arr[j]=temp; 7. } 8. } 9. } 1. int size = 5; 2. int array[size] = {4, 5, 1, 3, 9}; 3. temp=0; int i=0; 4. int j=size- 1; 5. while( i < j){ 6. temp = array[i]; 7. array[i] = array[j]; 8. array[j] = temp; i++; 9. j--; 10. } Sort ascending order 1. for(i=0;i<n;i++) { 2. for(j=i+1;j<n;j++) { 3. if(arr[i]<arr[j]) { 4. temp =arr[i]; 5. arr[i]=arr[j]; 6. arr[j]=temp; 7. } 8. } 9. } Sort descending order Reverse array
  • 8.
    Multi- dementional Array • How toinitialize a 2D array? Data type array_name [row][col] int 2Darray [3][3] Input data: same as 1D array
  • 9.
  • 10.
    Practice what is thevalue of sum after this code executes? 1. int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}}; 2. int sum = 0; 3. int col = matrix[0].length - 2; 4. for (int row = 0; row < 4; row++) { 5. sum = sum + matrix[row][col]; 6. }
  • 11.
    Ice breaker • What'san "old person" thing that you do?
  • 12.
    Agenda - Watch videoof passing array as parameter in Function - Review function definition and its example - Practice problem
  • 13.
  • 14.
    Passing Array asParameter in Function
  • 15.
    C++ Function Declaration returnTypefunctionName (parameter1, parameter2,...) { // function body } // function declaration void greet() { cout << "Hello World"; }
  • 16.
    Example 1. #include <iostream> 2.using namespace std; 3. int addition (int a, int b) { 4. int r; 5. r=a+b; 6. return r; 7. } 8. int main () { 9. int z; 10. z = addition (5,3); 11. cout << "The result is " << z; 12. }
  • 17.
    Practices • Write aprogram that calculates 6^5. Declare your own function to do this.
  • 18.
    Practices Write a programwith function to find 3 largest number of an array. Array size: 8 Input: {7, 12, 9, 15, 19, 32, 56, 70}