Created
September 12, 2022 23:24
-
-
Save m00zi/7d6c69bcb82ed8256db3b5949ed67ed7 to your computer and use it in GitHub Desktop.
Week2-OOP-Mon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
// get max element of array | |
int findMax(int myArr[]) { | |
int max = myArr[0]; | |
for (int i = 0; i < 5; i++) { | |
if (myArr[i] > max) { | |
max = myArr[i]; | |
} | |
} | |
return max; | |
} | |
// get min element of array | |
int findMin(int myArr[]) { | |
int min = myArr[0]; | |
for (int i = 0; i < 5; i++) { | |
if (myArr[i] < min) { | |
min = myArr[i]; | |
} | |
} | |
return min; | |
} | |
// increment | |
void increment(int *n) { *n += 1; } | |
// main | |
int main() { | |
int myArr[5] = {1, 2, 3, 4, 5}; | |
int max = findMax(myArr); | |
int min = findMin(myArr); | |
cout << "Max => " << max << endl; | |
cout << "Min => " << min << endl; | |
int i = 7; | |
increment(&i); | |
cout << i << endl; | |
int arr2[5] = {2, 4, 6, 8, 10}; | |
int *ptr = &arr2[0]; | |
for (int i = 0; i < 5; i++) { | |
// cout << arr2[i] << endl; | |
cout << *(ptr + i) << endl; | |
} | |
// Heap (can grow or shrink depend on need) use "new" keyword | |
// Stack (Scope variables) or if we know the size | |
// Global (Global Variable) | |
// | |
int size; // this will be stored in stack space | |
cout << "Enter the size: "; | |
cin >> size; | |
int *myheapArr = new int[size]; // this will be created in "HEAP" space; | |
// (because of "new" keyword) | |
myheapArr[0] = 76; | |
*(myheapArr + 1) = 88; | |
int *nPtr = new int; | |
*nPtr = 8; | |
ptr = new int; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment