C++ Notes
C++ Notes
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;
return 0;
-> In this, we collect the base and height of a triangle then find the area.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cin >> a >> b;
return 0;
-> Keep in mind that the answer is "undefined" when it is being divided by 0
LESSON 2:
https://pastebin.com/zkqtDFYP
int main(){
if(num == 0) {
cout << "Yes"
}
else{
cout << "No"
}
LESSON 3:
count += 1;
}
LESSON 4:
-> 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
-> 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
- Problems:
- Usually take the worst case, to think of ways that would make it
harder.
---> Code:
int main()
{
int a = 1;
int b = 10000000;
---> Arrays:
- int num[10];
- 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];
}
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'
How to improve it ?
int freq[26];
for(int k=0; k<26; k++){ freq[k] = 0; }
string str = " ";
int main()
{
int freq[26];
string str;
cin >> str;
for(int k=0; k<26; k++){ freq[k] = 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 )
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; }
return 0;
}
---> Assignment:
- On Document
- Tuesday C++
- Finish Homework
- Check Over
- Take Notes
---> Discuss prime numbers, and discuss two dimensional arrays as well.
---> 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)
return 0;
}
- 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
#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:
-
#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
return 0; \\ Remember to always include this part of the code, no matter what I
think
}
---> 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;
---> 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.
- This example above is a very easy one, and the name of the function is the
most important.
---> Exercise:
- Make a findBigger nuber uncion
---> Exercise:
- Write a function findAverage which will return the average of 3 numbers:
}
return 0;
}
int main() {