-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yuth
committed
Sep 24, 2024
1 parent
d2c831e
commit 683fb7a
Showing
61 changed files
with
602 additions
and
292 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,5 +31,6 @@ | |
*.out | ||
*.app | ||
|
||
bin/ | ||
build/ | ||
.vscode/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<img align="right" src="./doc/cover.png" width="300"> | ||
|
||
# Lab C++ | ||
This Package is create to collect some of my cpp code that I learn. [For Mom!] | ||
This Package is create to collect some of my cpp code that I have learnt. [For Mom!] | ||
|
||
### Reference [List](./doc/) | ||
There are detail of the package, read more in the references list link above. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "math_lib.h" | ||
#include <cmath> | ||
#include <cstdlib> | ||
#include <iostream> | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc < 2) { | ||
std::cout << "Usage: " << argv[0] << " number" << std::endl; | ||
return 1; | ||
} | ||
double inputValue = atof(argv[1]); | ||
std::cout << "The square root of " << inputValue << " is " << mysqrt(inputValue) << std::endl; | ||
return 0; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "math_lib.h" | ||
|
||
double mysqrt(double value) { | ||
return sqrt(value); | ||
} |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
#include <string> | ||
|
||
int main() { | ||
|
||
int arry[5] = {10, 20, 30, 40, 50}; | ||
int *arry2[5]; | ||
|
||
for (int i = 0; i < 5; ++i) { | ||
arry2[i] = &arry[i]; | ||
} | ||
|
||
for (int i = 0; i < 5; ++i) { | ||
std::cout << *arry2[i] << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
|
||
// return output by function paramter - done by using reference | ||
|
||
void function_reference(int &x, int &y) { | ||
int z = x; | ||
x = y; | ||
y = z; | ||
} | ||
|
||
int first = 31; | ||
int secnd = 55; | ||
|
||
void cal_int(int a, int b, int &c) { | ||
c = a + b; | ||
}; | ||
|
||
int main() { | ||
|
||
std::cout << first << std::endl; | ||
std::cout << secnd << std::endl; | ||
function_reference(first, secnd); | ||
std::cout << first << std::endl; | ||
std::cout << secnd << std::endl; | ||
|
||
int a = 10; | ||
int b = 20; | ||
int c{}; | ||
cal_int(a, b, c); | ||
std::cout << c << std::endl; | ||
|
||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
double getAverage(int *arr, int size) { | ||
int sum = 0; | ||
double avg; | ||
|
||
for (int i = 0; i < size; ++i) { | ||
sum += arr[i]; | ||
} | ||
avg = double(sum) / size; | ||
|
||
return avg; | ||
} | ||
|
||
int main() { | ||
// an int array with 5 elements. | ||
int balance[5] = {1000, 2, 3, 17, 50}; | ||
double avg; | ||
|
||
// pass pointer to the array as an argument. | ||
avg = getAverage(balance, 5); | ||
|
||
// output the returned value | ||
cout << "Average value is: " << avg << endl; | ||
|
||
return 0; | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "h_compare.h" | ||
|
||
double find_max(double a, double b) { | ||
if (a > b) { | ||
return a; | ||
} else { | ||
return b; | ||
}; | ||
} | ||
double find_min(double a, double b) { | ||
if (a < b) { | ||
return a; | ||
} else { | ||
return b; | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
double find_max(double a, double b); | ||
double find_min(double a, double b); | ||
|
||
// define #pragma once to prevent duplicate include. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "h_compare.h" | ||
#include <iostream> | ||
|
||
int main() { | ||
std::cout << " C is " << find_max(575, 2.255) << std::endl; | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
|
||
// &variable = to get the address of the variable | ||
// *address = to get the value inside of address | ||
|
||
std::string name = "Johnyuth"; // the original string variable "name" that has the value of "Johnyuth" | ||
std::string *neam = &name; // a pointer of type string "neam" that used to store the memory address of the reference variable "name" | ||
|
||
int main() { | ||
// create original variable | ||
std::cout << "Original = " << name << std::endl; | ||
|
||
// print out the memory address of name | ||
std::cout << "Adrs of Original = " << &name << std::endl; | ||
|
||
// create a pointer to the original variable and call the pointer 'neam' and print the 'neam' out | ||
std::cout << "Pointer address of Original = " << neam << std::endl; | ||
|
||
// dereference : to get the value back that the memory of that address has. this operation can be done | ||
// with asterisk symbol (*). example : *pointer_variable | ||
std::cout << "Deference back for the value of Pointer = " << *neam << std::endl; | ||
|
||
// change the value of the pointer | ||
*neam = "GOD YUTH"; | ||
|
||
// print out the new pointer value | ||
std::cout << "Changed value of the pointer = " << *neam << std::endl; | ||
|
||
std::cout << "Changed value of the Original = " << name << std::endl; | ||
|
||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() { | ||
// an int array with 5 elements. | ||
int balance[5] = {1000, 2, 3, 17, 50}; | ||
|
||
int *ptr = &balance[0] + 1; // the address can use addition and substract operation | ||
cout << *ptr << endl; | ||
|
||
// In array | ||
// the array address is the first element of the array address | ||
// all the code below print the address of the array all the same | ||
cout << balance << endl; | ||
cout << &balance << endl; | ||
cout << &balance[0] << endl; | ||
|
||
return 0; | ||
} |
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <cstdio> | ||
#include <iostream> | ||
|
||
template <typename T> | ||
void find_val(T a, T b, T &res) { | ||
res = a + b * a + b; | ||
} | ||
|
||
int main(int argc, char const *argv[]) { | ||
int res; | ||
find_val(14, 12, res); | ||
printf("the result is : %i", res); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class T { | ||
int x; | ||
|
||
void foo() { | ||
x = 6; // same as this->x = 6; | ||
this->x = 5; // explicit use of this-> | ||
} | ||
|
||
void foo() const { | ||
// x = 7; // Error: *this is constant | ||
} | ||
|
||
void foo(int x) // parameter x shadows the member with the same name | ||
{ | ||
this->x = x; // unqualified x refers to the parameter | ||
// 'this->' required for disambiguation | ||
} | ||
|
||
int y; | ||
T(int x) : x(x), // uses parameter x to initialize member x | ||
y(this->x) // uses member x to initialize member y | ||
{} | ||
|
||
T &operator=(const T &b) { | ||
x = b.x; | ||
return *this; // many overloaded operators return *this | ||
} | ||
}; |
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
Binary file not shown.
Oops, something went wrong.