0% found this document useful (0 votes)
12 views

C++ Notes

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C++ Notes

Uploaded by

derekhaozhe
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

LESSON 1:

8:25 PM 2024-09-08

Starting Code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
cout << "Hello World" ;

return 0;
}

---> Variables are very important, and they are used to store memory

int num;
-> You declare a variable whose name is num
-> Data type is integer
-> Somewhere in memory, there is a location assigned for num

---> Operator:
- Assignment operator
- Right hand side will run first
- Left should be variable

---> Exercise:

#include <bits/stdc++.h>
using namespace std;

int main()
{
int base = 0;
int height = 0;
cin >> base >> height;

int area = base * height / 2;

cout << area;

return 0;

-> In this, we collect the base and height of a triangle then find the area.

---> Declaring Variables


- Must put types in
- When using assignment operators, the left and right have to match.

---> Exercise #2:

#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cin >> a >> b;

int remainder = a%b;

cout << remainder;

return 0;

-> Keep in mind that the answer is "undefined" when it is being divided by 0

LESSON 2:

https://pastebin.com/zkqtDFYP

---> If/Else statements:

int main(){
if(num == 0) {
cout << "Yes"
}
else{
cout << "No"
}

LESSON 3:

---> For the J1 problem that I did recently:

- Add an if statement and it wil work

---> While Loop:

while (num > 0){


//do something

count += 1;
}

---> For Loop:

for(int k = 0; k < 20; k+=1){


//do something
}

LESSON 4:

---> New data type: Character (char)

-> int num = 7


---> Computer analyze this way:
1) Computer knows what data type it is (in this case, int)
2) Convert the value into binary (in this case, 7)
3) Know some terms in computing -> Bit and Byte.

---> Math Theorems:


-> If there are n bits, in total, we can represent 2*n integers (It is the
same as given a set with n elements, in total, there are 2*n subsets)
-> 1 byte = 8 bits
-> 1 byte can represent 2*8 numbers = 256

---> Character Data Type:

char character = 'a';

-> It's value (literal) is quoted by '' (not "" <-- this is string in c/c++,
string is not char)

---> Code:

int main(){
cout << " ch is " << ch << end1;
cout << " ch2 is " << ch << end1;
cout << " ch2 ia " << ch2 << end1;

return 0;
}

---> Excersize:

- Please write a program to ask user to enter two characters A and B which
are both english letters. Then, output all characters between A and B. (Assume all
lower case)

Example:

Input: c k
Output: c d e f g h i j

- How to Solve:
- Use a loop and also use if or else statement(s)

- Solution:

int main(){
char a, b;
cin >> a >> b;

if (a <= b) {
for (char ch = a; ch <= b; ch++) {
cout << ch << " ";
}

}
else {
for (char ch = b; ch <= 'z'; ch++) {
cout << ch << " ";
}
for (char ch = 'a'; ch <= a; ch++) {
cout << ch << " ";
}

return 0;
}

LESSON 5

---> Arrays & Algorithms

-> Notes:
- Review:
- Variables: Datatype, scope of variable and global variable
- Must make code clear with comments as well
- int a = 1;
int b = ++a + ++a++;
// don't write code in this way

- Operators: Data type of operants and data type of result precedence


of operators.

- If/Else: Written in notes

- Loops and rest of review in notes.

- Problems:
- Usually take the worst case, to think of ways that would make it
harder.

---> Code:

int main()
{
int a = 1;
int b = 10000000;

long long total = (a+b)*(b-a+1L)/2;


cout << total
return 0;
}

---> Arrays:

- int num[10];

- Here, we declare an array of int

---> Exercise #1:

- Concept:
- Use a loop to run through the input and then put it all in an array
in reverse order.
- Print at end

- Code:

int main()
{
int n[10];
for (int i = 0; i < 10; i++){
cin >> n[i];
}

for (int i = 9; i >= 0; i--){


cout << n[i] << " "
}

return 0;
}

9/20/24:

---> Brute force algorithm ( We search the string using simple loop ... )

in total we have 26 letters ... I will search 'a', 'b' , 'c' ... 'z'

for each letter in english letter : ( 26 )


for each character in the String: ( for the worst case , it will be 10 * 2^20
)
check if it is the same as the search character, and count in total how
many of them.

26 * 10 * 2^20 = 260 * 2^20 = 2^28 ( it cannot be finished in 1 second, (in CCC,


you can assume 1 second, can compute 2^26)

2^28/2^26 = 2^2 = 4 seconds ...

How to improve it ?

int freq[26];
for(int k=0; k<26; k++){ freq[k] = 0; }
string str = " ";

int len = str.size();


for(int k=0; k<len; k++){
char ch = str[k];
freq[ ch - 'a' ] += 1;
}

Would you please code it .


#include <bits/stdc++.h>
using namespace std ;

int main()
{

int freq[26];
string str;
cin >> str;
for(int k=0; k<26; k++){ freq[k] = 0; }

int len = str.size();


int maxFreq = 0;
char maxCh = 'a';
for(int k=0; k<len; k++){
char ch = str[k];
freq[ ch - 'a' ] += 1;
}
for(int k=0; k<26; k++){
if( freq[k] > maxFreq){
maxFreq = freq[k];
maxCh = k + 'a';
}
}
cout << maxCh << " " << maxFreq << endl;
return 0;
}

(The skill we use here is use index to solve problems... here the index is the
value of the char and the value of array is the
frequency )

---> Boolean Data Type is a type of data in c++

---> Return Statement is extremely important.

---> Number Systems for Binary

--->Given an array of N integers (positive or negative) ( 1 <= N <= 1000000 ),


output YES if there are some sub-array whose sum is 0 , otherwise, output NO.
( for sub-array, the index should be continously in a row )

Do you have simple brute force way ? (not easy, but we have brute force way to do
it... )

Think it in this way: under what case we have sub-array whose sum is 0 ?
Can we use pre-fix sum algorithm to do it ?
1) if we see 0 in pre-fix sum array, then we have sub-array whose sum is 0
2) you only need to know whether there are two spots that are equal to each other
(because we do not learn map yet, you can assume the sum of any sub-array is within
[-1000000,1000000] )

3) For each sum, check it in the check array, if it is checked before, then output
YES, otherwise, check that spot.

#include <bits/stdc++.h>
using namespace std;

int arrSum[1000000];
bool check[2000002]; //one int, it is 4 bytes, so here is 8MB,
int main()
{
for(int k=0; k<2000002; k++){ check[k] = false; }
int N;
cin >> N;
cin >> arrSum[0];
check[ arrSum[0] ] = true;
if( arrSum[0] == 0 ) { cout << "YES"; return 0; }

for(int k=1; k<N; k++){


int temp ;
cin >> temp;
arrSum[k] = arrSum[k-1] + temp;
if( arrSum[k] == 0 || check[ arrSum[k] ]){
cout << "YES" ;
return 1;
}
else{
check[ arrSum[k] ] = true; ]
}
}
cout << "NO" << endl;

return 0;
}

---> Assignment:
- On Document
- Tuesday C++
- Finish Homework
- Check Over
- Take Notes

---> Discuss prime numbers, and discuss two dimensional arrays as well.

---> Exercise: Given an integer, please output true if it's prime


for(int k=2; k<=sqrt(N); k++){
if( N%k == 0){
break;

\\Jump out of the loop since it's not prime


}

---> New Notes:


- Global variables can be changed inside of the program, and macro is just
simply replaced by the compiler, and after that, the N is replaced by the number.

---> Next:
- Finish homework using recursion in loops and such

---> Code:

#include <bits/stdc++.h>

# define N 501
//most of the time, we use macro to define a constant (constant is not a variable)

// and usually, it won't be changed inside your program,


// it is convient for us to write program

using namespace std;


bool prime[N];
int main()
{
for(int k=0; k<N; k++){ prime[k] = true; }

for(int k=2; k<N; k++){


if( prime[k] ){
cout << k << " ";
for(int x=2; x*k <= N-1; x++){ prime[x*k] = false; }
}
}

return 0;
}

---> Exercise #5:


- Use recursion to use a loop to find the prime number in a set of numbers

- Example Input:
- [1,5,8]
- Example Output:
- [5]

- Explanation
- We run through the list of numbers given, and then the program gives
us the prime numbers in the list, so we have to write a program using recursion
- Yes.

- BTW:
- \\ is a comment sign

---> Dynamic Programming


- Avoid recomputing
- Very important concept
- Get and it's simple
- New skill

---> Example #5:

#define N 100000
using namespace std;
bool prime[N+2] ; //we use index [1..N]
int prefX[N+2] ;
int main()
{
for( int k=0; k<= N ; k++){ prime[k] = true; }
prefX[0] = 0;
prefX[1] = 0 ;
for(int k=2; k<= N ; k++){ //for sieve algorithm and prefix sum
prefX[k] = prefX[k-1];
if( prime[k] ){
prefX[k] += k;
for(int j=2; j*k <= N; j = j+1 ){ prime[ j*k ] = false; }
}
}
//next, we do PRE-fs for part of them
int Q;
cin >> Q;
for(int k=0; k<Q; k++){
int A, B;
cin >> A >> B;
cout << prefX[B] - prefX[A-1] << endl ;
}
return 0;
}

// This one pass all the case, but we need to fix the new version of sieve
algorithm ...

---> Explanation:
-

---> How to Sort an Array:


- Important for High School computer science course
- How to use sorting to sort problems

---> Sorting Array Example: Sort a Simple Array:


- In this example, we sort a simple array to be in order so that we can solve
the problem easier for more problems

#include <bits/stdc++.h>
using namespace std;

int main() {
int arr[] = {5, 1, 4, 2, 8}; // int arr[5]; arr[0]=5; arr[1] = 1;
//int n = sizeof(arr) / sizeof(arr[0]); //sizeof will tell you how many bytes
of the variable
int n = 5;
sort(arr, arr + n); // Using the built-in sort function

cout << "Sorted array: ";


for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

return 0; \\ Remember to always include this part of the code, no matter what I
think
}

--> Built-In Sort Function:


- This function is used above
- Check it out and look at variables

---> How the Function Mathematically Works:


- How the Sorting Works:
- The sort function is very fast. It can sort the array with N items in
0( Nlog(N) ) time.

---> Example:
- Given a list of integers, I want to pick up M of them. I want the total sum
of the M numbers maximum, how do I do the picks?

- Hint:
- Use brute force algorithm to solve the problem, which makes the
problem a problem that is easily solved the "greedy" way.
- Code: Source: Teacher's Algorithm

#include<bits/stdc++.h>
using namespace std;

bool bigger(int a,int b){


return a >= b;
}
int main()
{
int N;
int M;
cin >> N;
cin >> M;
int sum = 0;
int arr[N];

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


cin >> arr[i];
}
sort(arr, arr + N, bigger);

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


sum += arr[i];
}
cout << sum;
return 0;
}

---> Explanation:
- In this code, we did the example above and solved it, using the "greedy"
way to solve it.

---> Homework:
- Look at document to find the homework and finish before next Monday

---> Notes:
- To fix the problem in the example question, we have to use the greedy
algorithm. The bug in the code was that the code was not written properly, meaning
that there was a bug in the code, that made the code get a runtime error, where we
didn't et the result that we wanted.

---> More Notes


- Today we will be doing two dimensional arrays
- We are learning more advanced programming
- We will be doing more problems later on, such as J4 and J5s from the CCC
- A lot of two dimensional array problems in CCC

---> Review for TD Array:


- 1) Its set of data type and memory for two-d array
- 2) How to traversing the two-d array
- 3) How to use it.

---> How to Control the Loop:


- Written in the document for more details.
---> Functions:
- Also used in python
- Algo uses recursion to rely on functions.
- Before a function can be used, you have to declare it first:
- To Declare a function:

int findBigger(int a, int b) {


if (a >= b) {
return a;
else {
return b;
}
}

- This example above is a very easy one, and the name of the function is the
most important.

---> Exercise:
- Make a findBigger nuber uncion

int findBiggest(int a, int b, int c){


if(a>= b && a >=c){
return a;
}
else if(b>= a && b >=c){
return b;
}
return c;
}

---> Exercise:
- Write a function findAverage which will return the average of 3 numbers:

double findAverage(double a, double b, double c){


double d = (a + b + c) / 3;
return d;
}
Int main(){
int N;
cin >> N;
for(int i = 1; i <= N/3; i++){
double x, y, z;
cin >> x >> y >> z;
cout << findAverage(x, y, z) << endl;

}
return 0;
}

- In this exercise above, we show clear understanding of the function in C++,


while demonstrating how to find the average in a set group of numbers.

---> Using Two-Dimensional Arrays in the Functions:

int main() {

int num[3][4] = {{3,1,8,9},{2,4,5,0},{-2,20,-3,23}}


cout << num << end1;

- Passing parameters to a function


- Pointer: It is the address data type -> we can define a variable for
address data type.

---> Lists: Today's Topic


- Exercise #2:

You might also like