### week 4

parent 630a5b5b
 pointer_arithmetics
 g++ -Werror -Wpedantic -g -o pointer_arithmetics pointer_arithmetics.cpp \ No newline at end of file
 # Group 1 In this project, you will learn about pointer arithmetics and use it for an example. Please follow the steps below and create a few slides summarizing your findings and discussion. Note: please compile code using: g++ -Werror -Wpedantic -g -o pointer_arithmetics pointer_arithmetics.cpp 1. read file pointer_arithmetrics.cpp, compile it, and run it. Describe what you observed and explain why this occurs. 2. extend the file to declare, initialize two arrays and compute their element-wise multiplication. Your code should satisfy the following conditions: - size of arrays is read from terminal input (so that it can be varied) - the two arrays A and B need to be initialized with "something" of your choice - use a for loop to compute the vector multiplication $ C = A \cdot B $ - all allocated memory needs to be freed before the end of your program. 3. extend your code to compute the vector multiplication a second time. This time, use pointer arithmetics. *i.e.* use pointers p_A, p_B, and p_C to point to the arrays and modify them. Tip: you will need to use ++p_A etc. 4. finally, measure the time for both vector multiplication computations and print it to the console. The pointer_arithmetics.cpp contains comments that provide the necessary tools to measure the time. Your slides should summarize all of your steps above. \ No newline at end of file
 #include // HELP: to time function execution // #include // to time function execution // auto t1 = std::chrono::high_resolution_clock::now(); // get current time // auto dt = std::chrono::duration_cast(t2 - t1).count() // time passed between t1 and t2 int main(int argc, char* argv[]) { // RUN this, observe, and explain int a = { 10, 22, 333 }; int* p_a = a; std::cout << "a = " << a << "\n"; std::cout << "a = " << a << "\n"; std::cout << "a = " << a << "\n"; std::cout << "a = " << a << "\n"; std::cout << "p_a = " << p_a << "\n"; std::cout << "*p_a = " << *p_a << "\n"; std::cout << "\ncall ++p_a\n"; ++p_a; std::cout << "p_a = " << p_a << "\n"; std::cout << "*p_a = " << *p_a << "\n"; std::cout << "\ncall ++p_a\n"; ++p_a; std::cout << "p_a = " << p_a << "\n"; std::cout << "*p_a = " << *p_a << "\n"; std::cout << "\ncall ++p_a\n"; ++p_a; std::cout << "p_a = " << p_a << "\n"; std::cout << "*p_a = " << *p_a << "\n"; std::cout << "---------------------\n"; // write additional code for element-wise vector multiplication return 0; }
 const_pointer
 g++ -Werror -Wpedantic -g -o const_pointer const_pointer.cpp \ No newline at end of file
 # Group 2 In this project, you will learn about constant pointers. Please follow the steps below and create a few slides summarizing your findings and discussion. Note: please compile code using: g++ -Werror -Wpedantic -g -o const_pointer const_pointer.cpp You can find useful information at: [http://www.cplusplus.com/doc/tutorial/pointers/](http://www.cplusplus.com/doc/tutorial/pointers/) 1. read file const_pointer.cpp and compile it. If compilation fails, find lines that cause compilation error and comment them out using the // command. Once, it compiles, run the program. Describe what you observed and explain why this occurred. 2. extend the file to compute element-wise vector multiplication $ C = A \cdot B $. Your code should satisfy the following conditions: - use a for loop to compute the vector multiplication $ C = A \cdot B $ - in section 1: define and initialize pointers to A, B, and C, named p_A, p_B, and p_C. - in section 2: compute dot-product using only p_A, p_B, and p_C. - the pointers p_A, p_B, and p_C should be const appropriatly for this task. Meaning that one should not be able to change the values of A and B, and that the pointer p_C should not be allowed to change. 3. uncomment the lines in section 3: Your code should not compile anymore. In fact, your code should only compile if all of these lines are commented. Your slides should summarize all of your steps above. \ No newline at end of file
 #include int main(int argc, char* argv[]) { // RUN this, observe, and explain int a = { 10, 22, 333, 4, 55, 666 }; int b = { -10, -20, -30, -40, -50, -60 }; // define various pointers // spacing should not be like this // we use it exceptionally to highlight the differences int * p_a = a; const int * cp_a = a; int const * cp2_a = a; int * const pc_a = a; const int * const cpc_a = a; // read array through pointers: std::cout << " p_a = " << p_a << "\n"; std::cout << " cp_a = " << cp_a << "\n"; std::cout << "cp2_a = " << cp2_a << "\n"; std::cout << " pc_a = " << pc_a << "\n"; std::cout << "cpc_a = " << cpc_a << "\n"; // modify array through pointers: // comment out those that do not work p_a = 1; cp_a = 2; cp2_a = 3; pc_a = 4; cpc_a = 5; // change pointer to point to b array p_a = b; cp_a = b; cp2_a = b; pc_a = b; cpc_a = b; // -------------------------------------------------------- // write code for element-wise Vector-product C = A \cdot B // -------------------------------------------------------- int A = { 10, 22, 333, 4, 55, 666 }; int B = { -10, -20, -30, -40, -50, -60 }; int C; // 1: define pointers: p_A, p_B, and p_C // YOUR CODE // 2: code vector multiplication using ONLY pointers defined in 1 // condition: A and B should not be allowed to modified, and // pointer to C cannot be modified // YOUR CODE // 3: uncomment the following lines to check if you used the correct const //p_A = 100; // should fail //p_B = 200; // should fail //p_A = nullptr; // should fail //p_B = nullptr; // should fail //p_C = nullptr; // should fail return 0; }
 pointers2
 g++ -Werror -Wpedantic -g -o pointers2 pointers2.cpp \ No newline at end of file
 # Group 3 In this project, you will learn about pointers to pointers and deleting dynamically allocated memory. Please follow the steps below and create a few slides summarizing your findings and discussion. Note: please compile code using: g++ -Werror -Wpedantic -g -o pointers2 pointers2.cpp 1. read file pointers2.cpp, compile it and run it. Describe what you observed and explain why this occurred. 2. extend the file to compute the dot-product $ c = A \cdot B $ where A and B are dynamically allocated based on a user input. Run the code. Your code should satisfy the following conditions: - read an interger as input from terminal - allocate array A and B of length as provided by terminal input - initiate A and B to something, e.g., 2,4,6,... or else - use a for loop to compute the dot-product $ c = A \cdot B $ - free allocated memory 3. modify your code such that the **allocation**, initiation and computation of dot-product is within a for loop going for 1 billion rounds. Make sure that the read-input-from-terminal is before the loop and the free allocated memory after the loop. However, the allocation needs to be inside the for loop. Run the code. What do you observe and why. Suggestion: In a second terminal, type top and observe what happens while you are running your program. Your slides should summarize all of your steps above. \ No newline at end of file
 #include int main(int argc, char* argv[]) { double m1 = {2, 4, 6, 8}; double m2 = {30, 50}; std::cout << "m1 = " << m1 << "\n"; std::cout << "m2 = " << m2 << "\n"; double* M; M = m1; M = m2; std::cout << "M = " << M << "\n"; std::cout << "M = " << M << "\n"; std::cout << "M = " << M << "\n"; for (int i=0; i<2; ++i) { for (int j=0; j<4; ++j) { std::cout << "M[" << i << "][" << j << "] = " << M[i][j] << "\n"; } } // write your code below here return 0; }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!