Data Structure
Hoang-Giang Cao (高黃江)
Fall - 2024
MIDTERM - Data Structure
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 |
Quiz 1- Data Structure
MIDTERM - Data Structure
Review Basic C++
Functions with Array
Basic C++
What will we learn today?
Function with array input
int[] float[] double []
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
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
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
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
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
Function with Array in C++
When working with the array
(most of the case)
Function need at least 2 parameters: array and array size
Exercises Basic C++
Function with Array
Simple Example
Function with array
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):
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):
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):
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):
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):
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):
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):
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):
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):
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):
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):
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 |
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 |
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 |
|
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 |
Data Structure
Good luck to you with this course!
Any questions?