/* * w1_c2_lecture.cpp * * Created on: Mar 4, 2019 * Author: johnsontimoj */ /////////////////////////////////// // // Week1 - Class2 Lecture examples // ///////////////////////////////////// #include using namespace std; #include "w1_c2_lecture.h" int main(void){ /////////////// // Simple IO ////////////// // simple output cout << "Hello World\n"; ///////////////////////////////// // simple input int val; cout << "Please enter a value: "; cin >> val; cout << "You entered the value: " << val << endl; ///////////////////////////////// // gated input val = 0; cout << "Please enter the value -1" << endl; while(val != -1){ cin >> val; } // end while cout << "Finally !\n"; ////////////////////////////////// // multiple input int val1; int val2; int val3; cout << "please enter 3 values: "; cin >> val1 >> val2 >> val3; cout <<"You entered: " << val1 << val2 << val3 << "\n"; ////////////// // pointer review ////////////// // basic pointer use int val4; read_val(&val4); print_val(val4); ///////////////////////////////////// // alternate pointer use int val5; int * val5_ptr; val5_ptr = &val5; read_val(val5_ptr); print_val(val5); //////////////// // Dynamic memory /////////////// // Show the difference in memory locations int val6; int val7; int * val8_ptr = new int; int * val9_ptr = new int; read_val(&val6); read_val(&val7); read_val(val8_ptr); read_val(val9_ptr); print_val(val6); print_val(val7); print_val(*val8_ptr); print_val(*val9_ptr); delete val8_ptr; delete val9_ptr; ////////////////////////////////// // Show the use of new with multiple number of elements int * valA_ptr = new int[4]; read_val(valA_ptr); read_val(valA_ptr + 1); read_val(&valA_ptr[2]); read_val(valA_ptr + 3); print_val(*valA_ptr); print_val(valA_ptr[1]); print_val(*(valA_ptr + 2)); print_val(valA_ptr[3]); delete[] valA_ptr; ////////////////////////////////// // Show the use of new with intermediate delete int * valB = new int; read_val(valB); delete valB; int * valC = new int; read_val(valC); delete valC; ////////////////////////////////// // // Plus File stream example from notes // return 0; } // end main // // Function passing back the value read "by pointer" // void read_val(int * value){ cout << "Please enter a value: "; cin >> *value; // print out the actual value passed to the function // since a pointer is passed - this will be the address // of the variable referenced by the pointer cout << "The value passed to the read_val function was: " << value << endl; return; } // end read_val // // Function printing a value "passed by value" // void print_val(int value){ cout << "You entered: " << value << endl; return; } // end print_val