Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-dot authored May 24, 2021
1 parent d4c5d2b commit b3aac2a
Show file tree
Hide file tree
Showing 100 changed files with 3,704 additions and 0 deletions.
53 changes: 53 additions & 0 deletions p11/DynamicMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by Carlos Arbizu on 12/19/20.
//

// to allocate a variable to the heap use the new keyword.
#include <iostream>

int main(){
int *int_ptr {nullptr};
int_ptr = new int; // stores variable in the heap
std::cout << int_ptr <<" is the value of the address of the pointer." << std::endl;
*int_ptr = 5; //dereference the pointer to get to the variable value and set it to a new value
// in this example the only way to access the variable is through the pointer since the int variable has no name.
// it is important that you delallocate the storage to free up space in the memory as such:
delete int_ptr;
//************************************************************************
int *size_ptr {nullptr};
int size {};
std::cout <<"Input the size that you want the array to be: ";
std::cin >>size;
size_ptr = new int{size};
std::cout << size_ptr <<std::endl;
//Now points to an array made in the heap
//If we lose the ability to acess this pointer there is no way to remove this array
// This is called a memory leak
delete size_ptr;
//If we have a pointer leading to an array allocated in the heap such as this we must use the delete keyword
// to free up space.
int array[4] {1,2,3,4};
int *ptr_arry {array};

std::cout << array << " is the address of the first element." << std::endl;
std::cout << array + 1 << " is the address of the second element." << std::endl;
std::cout << array + 2 << " is the address of the third element." << std::endl;
std::cout << ptr_arry << " is the address of the first element using a pointer. " << std::endl;
std::cout << ptr_arry + 1 << " is the address of the second element using a pointer." << std::endl;
std::cout << ptr_arry +2 << " is the address of the third element using a pointer." << std::endl;

std::cout << *array<< " is the first element." << std::endl;
std::cout << *array + 1<< " is the second element." << std::endl;
std::cout << *array + 2 << " is the third element." << std::endl;
std::cout << *ptr_arry << " is the first element using a pointer and offset notation" << std::endl;
std::cout << *ptr_arry + 1 << " is the second element using a pointer and offset notation" << std::endl;
std::cout << *ptr_arry + 2 << " is the third element using a pointer and offset notation" << std::endl;

std::cout << ptr_arry[0] << " is the first element using a pointer and subscript notation." << std::endl;
std::cout << ptr_arry[1] << " is the second element using a pointer and subscript notation." << std::endl;
std::cout << ptr_arry[2] << " is the third element using a pointer and subscript notation." << std::endl;

std::cout << sizeof(int) << " is the amount of bytes of an int." << std::endl;

return 0;
}
21 changes: 21 additions & 0 deletions p11/PassingPointerFunc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by Carlos Arbizu on 12/22/20.
//

//passing pointer through functions can be done through c++
#include <iostream>
void doubleptr(int *ptr);
int main(){
int value {3};
//here we can pass the variable value through this function using the variable's address or a pointer pointing to the variable itself
// NOTE: the variable changes remain after the function is called since the function is changing the value
// at the address which is very different from pass by value functions and is the same as pass by reference functions
doubleptr(&value);
std::cout << value << " is the value of the variable";


return 0;
}
void doubleptr(int *ptr){
*ptr *= 2;
}
82 changes: 82 additions & 0 deletions p11/Pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// Created by Carlos Arbizu on 12/9/20.
//

// To create a pointer use an astrik (*) on the data type or variable name
// Pointers point to the address of a specific function or variable
// This can allow objects or functions to access information outside of their scope
// IT IS VERY IMPORTANT to remember that pointers unintiliazed will point anywhere
// that means it can access any type of variable or function without you realizing it
// this it is important to initialize it to the nullptr
// as such
// int *int_ptr {nullptr}; (before we use)
// pointers can use the const keyword
// using the const keyword as seen below makes THE DATA that the pointer points to constant not the pointer itself
// const int *int_ptr {highscore}; //here we can still change the pointer but not the data pointed to
// int *const int_ptr // HERE WE CANNOT CHANGE THE POINTER however we can the data pointed to
// const int *const int_ptr // Here we cannot change the pointer nor the data pointed to by the pointer
// ****************************************************************************************************
// To create a function that returns a pointer see below
// NEVER RETURN A POINTER TO A LOCAL VARIABLE!!!
// Since the local variable is contained within the function as soon as the function ends and the stack is
// popped the local variable is deleted
// BELOW ARE SOME EXAMPLES OF WHAT NOT TO DO!!

// int *DONTDOTHIS(){
// int size {};
// return &size;
// }

// int *ORTHIS(){
// int size {};
// int *ptr{&size};
// return ptr;
// }

#include <iostream>
using std::cout;
using std::endl;
using std::string;

int *createArray(size_t size, int initial_value = 5){
int *arryptr{nullptr};
arryptr = new int[size];

for(size_t i {0}; i < size; i++){
arryptr[i] = initial_value;
}
return arryptr;
}
void displayArray(const int *const arryptr,size_t size){
// POINTER ADDRESS IS CONST AND DATA IS CONST NONE CAN BE CHANGED
for(size_t i {0}; i < size; i++){
cout << arryptr[i] << endl;
}
}
int main()
{

int num {10};
int *p;

string Array[] {"Larry","Moe","Curly"};
cout << "Garbage output since p was not intialized " << p << endl;
cout << "Value of the address of p " << &p << endl;
cout << "Size of p (also the amount of bytes an address can hold) " << sizeof(p) << endl;
cout << "It is important to note that the size does NOT change for the datatype the datatype only indicates"
" what values(double int etc.) that pointer points to." << endl;

p = nullptr;
cout << "Now the value of p since it was set to nullptr " << p << endl;
//to set a pointer to a varialble it must be in the form of int *ptr = &variableName
p = &num;
cout << "The address where the num variable is stored is " << p << endl;
int *arryptr = createArray(5,9);
std::cout << "**************" << endl;
displayArray(arryptr,5);


return 0;


}
34 changes: 34 additions & 0 deletions p11/PointerAritmetic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Carlos Arbizu on 12/22/20.
//

#include <iostream>

int main(){
//pointers can be added or subtracted by n
//adding or subtracting a pointer by n is the same as adding by n and
// multiplying n by sizeof(DATATYPE) the datatype you are using
// adding and subtracting pointers returns the difference
//we can compare pointers to see if they point to the same LOCATION
// not the same data stored if you want to see if the data is the same
// DEREFERENCE the pointer
int scores[5] {100,300,200,800,-1};
int *ptr_int {scores};
int n {0};
int *ptr_int2 {scores};
//accessing elements in array using pointers
while(*ptr_int!= -1){
std::cout << *ptr_int << std::endl;
n = ptr_int - ptr_int2;
std::cout << n * sizeof(int) << " is the difference between the two pointers' address (in bytes)." << std::endl;
ptr_int++; //increment the pointer address by 4 (value is specific to this system)
}

//The pointer can be dereferenced and incremented all in one line using the ++ operator
ptr_int = scores;
while(*ptr_int != -1){
std::cout << *ptr_int++ << std::endl;
}

return 0;
}
45 changes: 45 additions & 0 deletions p11/References.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Created by Carlos Arbizu on 12/26/20.
//
#include <iostream>

using std::cout;
using std::endl;
using std::string;

int main(){
string Array[]{"Curly","Moe","Larry"};
//This for loop will not change the variable since it is a copy of the
// stringptr variables in Array
for(string str : Array){
str = "funny";
}
//To overcome this we use the actual variable value instead of the copies
// behind the scenes it's a pointer that has been abstracted
for(string &str : Array){
str = "funny";
//now we change the value
}
for(string const &str : Array){
cout << str << endl;
}

int num {100};
int &ref {num};
//here ref acts like a pointer to number
// it will automatically be dereferenced when used
// and will automatically change the value of num and be changed when the value of num is updated.
cout << num << " is the value of num" << endl;
cout << ref << " is the value of ref" << endl;
num = 200;

cout << num << " is the value of num after num was updated" << endl;
cout << ref << " is the value of ref after the value of num was updated" << endl;
ref = 300;

cout << num << " is the value of num after ref was updated" << endl;
cout << ref << " is the value of ref after the value of ref was updated" << endl;


return 0;
}
Binary file added p11/challenge4
Binary file not shown.
46 changes: 46 additions & 0 deletions p11/challenge4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by Carlos Arbizu on 12/26/20.
//


#include <iostream>
using std::cout;
using std::endl;
//FOR CONST INT *CONST POINTERS IT IS NOT NECESSARY TO SPECIFY CONST INT *CONST IN THE FUNCTION
//PROTOTYPE
int *applyAll(const int *arryptr1,size_t size1,const int *arryptr2,size_t size2);
void print(const int *arryptr,size_t size);

int main(){
int array1[3] {3,5,6};
size_t size = 3;
int array2[3] {1,2,3};

int *arryptrHeap {applyAll(array1,size,array2,size)};
print(arryptrHeap,size * size);

return 0;
}
void print(const int *const arryptr, size_t size){
cout << "[ ";
for(size_t i {0}; i < size; i++){
cout << arryptr[i] << " ";
}
cout << "]";
}
int *applyAll(const int *const arryptr1,size_t size1, const int *const arryptr2,size_t size2){
//the size of the new array
size_t newSize {size2 * size1};
int *newArryptr {new int[newSize]};
// index of the new array that increases every loop instance
size_t counter {0};
//loop through all the elments of one array multiplying it by all the elements of the other array
//
for(size_t i {0}; i < size1; i++ ){
for(size_t j {0}; j < size2; j++) {
newArryptr[counter] = arryptr1[i] * arryptr2[j];
counter++;
}
}
return newArryptr;
}
31 changes: 31 additions & 0 deletions p12/Account.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by Carlos Arbizu on 12/30/20.
//
//If the symbol _ACCOUNT_H has been defined (by the preprocessor) then the symbol does not need to be
// defined again and the program can run as usual
#ifndef CPP_ACCOUNT_H
#define CPP_ACCOUNT_H
#include <string>


class Account{
private:
double balance;
std::string name;

public:
//Methods declared out of class
void set_balance(double balance);
double get_balance();
//Methods declared inline
//Not recommended best practice is to define functions and classes in the .h file
//and to declare them in a separate .cpp file
void set_name(std::string name){
this->name = name;
}
std::string get_name(){
return name;
}

};
#endif //CPP_ACCOUNT_H
14 changes: 14 additions & 0 deletions p12/AccountPoly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by Carlos Arbizu on 12/30/20.
//

//Recreation of the code in ClassMethods.cpp using header files for specification and separate cpp
// for implementation

#include "Account.h"
void AccountPoly::set_balance(double balance){
this->balance = balance;
};
double AccountPoly::get_balance(){
return balance;
};
46 changes: 46 additions & 0 deletions p12/BisectionalSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by Carlos Arbizu on 12/30/20.
//
#include <iostream>
#include <cstdlib>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

int main(){

double input;

cout << "Input the number you want to take the square root of: "; cin >> input; cout << endl;

double middlenum = {input/2};
const double error = {.000000000001};
double max = input;

while(abs(middlenum * middlenum - input) >= error )
{
if(middlenum * middlenum > input){
max = middlenum;
middlenum = middlenum/2;
}
else {
middlenum = (max + middlenum)/2;
}
}
cout << middlenum << endl;

//for answers just a bit above or below due to error
if(int(middlenum) * int(middlenum) == input){
middlenum = int(middlenum);
}
else if((int(middlenum) + 1) * (int(middlenum) + 1) == input){
middlenum = int(middlenum) + 1;
}

cout << std::setprecision(11);
cout << "The result is: " << middlenum;


return 0;
}
Loading

0 comments on commit b3aac2a

Please sign in to comment.