1 of 32

Data Structure

Hoang-Giang Cao (高黃江)

Fall - 2024

2 of 32

MIDTERM - Data Structure

  • Mid-term: Wednesday 05 November 2025
  • Duration: 90 mins�Format: Paper Questions + On-site programing
  • Topics:
  • Input, output, comparison & logical operators, data type in C+
  • Basic Python programming (Function, If, Else, For-While)
    • Function with array: min,max, avg, sum, search for conditioned number, etc

3 of 32

Schedule

Content

Date

Lecture: For loop / while loop

13 Oct

Lecture: functions

22 Oct

Homework HW1: If-else, for loop

22-29 Oct

Homework HW2: functions

22 Oct-05 Nov

Quiz QZ1: Pre-MIDTERM

29 Oct

MIDTERM

05 November

4 of 32

Quiz 1- Data Structure

  • Quiz 1: Wednesday 29 October 2024 (next week)
  • Practice Midterm
  • Topics:
  • Basic C++ programming (Function, If, Else, For-While)
    • Function with array
    • min,max, avg, sum
    • positive, negative, divisible, conditioned number, etc

5 of 32

MIDTERM - Data Structure

  • Mid-term: Monday 04 November 2024
  • 90 mins - On-site programing - 4-6 coding problems
  • Topics:
  • Basic C++ programming (Function, If, Else, For-While)
    • Function with array
    • min,max, avg, sum
    • positive, negative, divisible, conditioned number, etc
  • Basic OOP (Simple class)

6 of 32

Review Basic C++

Functions with Array

7 of 32

Basic C++

What will we learn today?

  • Function with Array
  • Basic algorithm in Array

8 of 32

Function with array input

int[] float[] double []

9 of 32

Function

Get len (number of element) of an array:

#include <iostream>

using namespace std;

int main (){

int arr[] = {-12, 5, -7, 20, 33, -16};

int len = sizeof(arr)/sizeof(int);

cout << "Array has" << len <<" number"<<endl;

return 0 ;

}

datatype arr[] = {.......};

int len = sizeof(arr)/sizeof(datatype);

#include <iostream>

using namespace std;

int main() {

float arr[] = {-12.5, -7.2, 20.8};

int len = sizeof(arr) / sizeof(float);

cout << "Array has " << len << " numbers" << endl;

return 0;

}

Size of integer array

Size of float array

10 of 32

Function

Syntax

Function with array need to consider 2 things:

1. Array parameter with [ ] (Square Brackets)

Indicates that the parameter is an array.

2. int size (Size Argument)

Usually, functions need the size of the array to be passed as an additional argument.

This allows the function to know how many elements are in the array.

Arrays do not store their size in C++, so this parameter is essential.

void function (int arr[], int size) {

}

int arr[] → Input array parameter

int size → Size of the array

11 of 32

Function with Array

Function to print element in the array

#include <iostream>

using namespace std;

void print_array(int arr[], int N){

for (int i =0; i < N; i++) {

cout << arr[i] << endl;

}

}

int main() {

int arr[] = {-12, 5, -7, 20, 33, -16};

int len = sizeof(arr) / sizeof(int);

cout << "Array has " << len << " numbers" << endl;

print_array(arr,len);

return 0;

}

void function (int arr[], int size) {

}

int arr[] → Input array parameter

int size → Size of the array

12 of 32

Function with Array

Function to print element in the array

#include <iostream>

using namespace std;

void print_array(float arr[], int N){

for (int i =0; i < N; i++) {

cout << arr[i] << endl;

}

}

int main() {

float array[] = {-12.5, -7.2, 20.8};

int len = sizeof(array) / sizeof(float);

cout << "Array has " << len << " numbers" << endl;

print_array (array,len);

return 0;

}

void function (int arr[], int size) {

}

int arr[] → Input array parameter

int size → Size of the array

Match the type of parameter

13 of 32

Function with Array

Function to print element in the array

#include <iostream>

using namespace std;

void print_array(float arr[], int N){

for (int i =0; i < N; i++) {

cout << arr[i] << endl;

}

}

int main() {

float array[] = {-12.5, -7.2, 20.8};

int len = sizeof(array) / sizeof(float);

cout << "Array has " << len << " numbers" << endl;

print_array (array,len);

return 0;

}

void function (int arr[], int size) {

}

int arr[] → Input array parameter

int size → Size of the array

Match the type of parameter

14 of 32

Function with Array in C++

When working with the array

(most of the case)

Function need at least 2 parameters: array and array size

15 of 32

Exercises Basic C++

Function with Array

16 of 32

Simple Example

Function with array

17 of 32

Exercises 1a

Write a function to print all value of an integer array.

The function take 2 input parameters: an array and the size of the array

��

Input

{-12, 5, -7, 20, 33, -16}

Test case (array):

18 of 32

Exercises 1a

Write a function to print all value of an integer array.

The function take 2 input parameters: an array and the size of the array

��

#include <iostream>

using namespace std;

void print_array(int arr[], int N){

for (int i =0; i < N; i++) {

cout << arr[i] << endl;

}

}

int main() {

int arr[] = {-12, 5, -7, 20, 33, -16};

int len = sizeof(arr) / sizeof(int);

print_array(arr,len);

return 0;

}

function

testing

Input

{-12, 5, -7, 20, 33, -16}

Test case (array):

19 of 32

Exercises 1b

Write a function to print (no return) all even numbers in an integer array

Even number is the number that divisible by 2. I.e %2==0 is True��

Array

Expected output (printing)

{15, -9, 24, 7, -6, 18}

24 -6 18

{3, -1, -25, 17, 8, -3}

8

{-45, -10, -5, -22, -7, -1}

-10 -22

{29, 14, 37, 6, 12, 25}

14 6 12

{0, -100, 50, 34, -45, 75}

0 -100 50 34

Test case (array):

20 of 32

Exercises 1c

Write a function to print all (no return) negative odd numbers in an integer array

Odd number is the number that NOT divisible by 2. I.e %2== 1 is True��

Array

Expected output (printing)

{15, -9, 24, 7, -6, 18}

-9

{3, -1, -25, 17, 8, -3}

-1, -25, -3

{-45, -10, -5, -22, -7, -1}

-45, -5, -7, -1

{29, 14, 37, 6, 12, 25}

{0, -100, 50, 34, -45, 75}

-45

Test case (of function):

21 of 32

Exercises 1d

Write a function to print all (no return) positive numbers divisible by 3 in an integer array��

Array

Expected output (printing)

{15, -9, 24, 7, -6, 18}

15 24 18

{3, -1, -25, 17, 8, -3}

3

{-45, -10, -5, -22, -7, -1}

{29, 14, 37, 6, 12, 25}

6 12

{0, -100, 50, 34, -45, 75}

0 75

Test case (of function):

22 of 32

Exercises 2a

Write a function to find & return the maximum value of an integer array

The function take 2 input parameters: an array and the size of the array

The function should return the maximum value of an integer array

��

Array

Output

{15, -9, 24, 7, -6, 18}

24

{3, -1, -25, 17, 8, -3}

17

{-45, -10, -5, -22, -7, -1}

-1

{29, 14, 37, 6, 12, 25}

37

{0, -100, 50, 34, -45, 75}

75

Test case (array):

23 of 32

Exercises 2b

Write a function to find & return the minimum value of an float array

The function take 2 input parameters: an array and the size of the array

The function should return the minimum value of an float array

��

Array

Output

{12.5, -7.3, 8.9, 14.6, -5.2, 9.8}

-7.3

{0.0, -4.4, 3.7, 10.1, -11.9, 6.5}

-11.9

{-15.6, -8.8, -2.1, -3.5, -0.9, -4.7}

-15.6

{5.5, 11.1, 3.3, 7.2, 2.4, 8.8}

2.4

{9.9, 4.2, -6.6, -10.5, 15.8, 12.7}

-10.5

Test case (array):

24 of 32

Exercises 3a

Write a function to calculate & return the sum all value of an integer array

The function take 2 input parameters: an array and the size of the array

The function should return the sum all value of an integer array

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

49

{3, -1, -25, 17, 8, -3}

-1

{-45, -10, -5, -22, -7, -1}

-90

{29, 14, 37, 6, 12, 25}

123

{0, -100, 50, 34, -45, 75}

-16

Test case (array):

25 of 32

Exercises 3b

Write a function to calculate & return the sum all value of an float array

The function take 2 input parameters: an array and the size of the array

The function should return the sum all value of an float array

��

Array

Output (return value)

{12.5, -7.3, 8.9, 14.6, -5.2, 9.8}

33.3

{0.0, -4.4, 3.7, 10.1, -11.9, 6.5}

4

{-15.6, -8.8, -2.1, -3.5, -0.9, -4.7}

-35.6

{5.5, 11.1, 3.3, 7.2, 2.4, 8.8}

38.3

{9.9, 4.2, -6.6, -10.5, 15.8, 12.7}

25.5

Test case (array):

26 of 32

Exercises 4

Write a function to find & return the average value of an integer array

The function take 2 input parameters: an array and the size of the array

The function should return the sum all value of an integer array

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

8.166666667

{3, -1, -25, 17, 8, -3}

-0.1666666667

{-45, -10, -5, -22, -7, -1}

-15

{29, 14, 37, 6, 12, 25}

20.5

{0, -100, 50, 34, -45, 75}

2.333333333

Test case (array):

27 of 32

Exercises 5

Write a function to calculate & return the sum all negative value of an integer array

The function take 2 input parameters: an array and the size of the array

The function should return the sum all negative value of an integer array

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

-15

{3, -1, -25, 17, 8, -3}

-29

{-45, -10, -5, -22, -7, -1}

-85

{29, 14, 37, 6, 12, 25}

0

{0, -100, 50, 34, -45, 75}

-145

Test case (array):

28 of 32

Exercises 6

Write a function to count & return how many numbers divisible by 3 in the array

The function take 2 input parameters: an array and the size of the array

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

4

{3, -1, -25, 17, 8, -3}

2

{-45, -10, -5, -22, -7, -1}

1

{29, 14, 37, 6, 12, 25}

2

{0, -100, 50, 34, -45, 75}

3

Test case (array):

Explanation:

That is 15, -9, 24, 6, 18

That is 3, -3

That is -45

That is 6, 12

That is 0, -45, 75

29 of 32

Exercises 7

Write a function to count & return how many numbers positive divisible by 3 in the array

The function take 2 input parameters: an array and the size of the array

��

Test case (array):

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

3

{3, -1, -25, 17, 8, -3}

1

{-45, -10, -5, -22, -7, -1}

0

{29, 14, 37, 6, 12, 25}

2

{0, -100, 50, 34, -45, 75}

1

Explanation:

That is 15, 24, 6, 18

That is 3

No positive number divisible by 3

That is 6, 12

That is 75.

0 is not negative, not positive => no count

30 of 32

Exercises 5

Write a function to return the first negative number divisible by 3

If not found any number, return -1

The function take 2 input parameters: an array and the size of the array

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

-9

{3, -1, -25, 17, 8, -3}

-3

{-45, -10, -5, -22, -7, -1}

-45

{29, 14, 37, 6, 12, 25}

-1

{0, -100, 50, 34, -45, 75}

-45

Test case (array):

Explanation:

Not found any negative number that divisible by 3

31 of 32

Exercises 9

Write a function to return the index of first negative number divisible by 3

Note: Index of an array starting from 0.

If not found any number, return -1

��

Array

Output (return value)

{15, -9, 24, 7, -6, 18}

1

{3, -1, -25, 17, 8, -3}

5

{-45, -10, -5, -22, -7, -1}

0

{29, 14, 37, 6, 12, 25}

-1

{0, -100, 50, 34, -45, 75}

5

Test case (array):

Explanation:

Found -9 at index 1

Found -3 at index 5

Found -45 at index 0

Not found any negative number that divisible by 3

Found -45 at index 5

32 of 32

Data Structure

Good luck to you with this course!

Any questions?