Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 2: Write some code using pointers Here’s the basic program for next few par

ID: 3813947 • Letter: P

Question

Part 2: Write some code using pointers Here’s the basic program for next few parts of this lab: #include <iostream> using namespace std; int main() { int i = 5, j=51, k=62; int data[5] = {10, 20, 30, 40, 50}; char my_cstring[8] = "the fox"; int *p = NULL; char *pc = NULL; // do something useful with the arrays and the pointers // . . . // and put some clean-up code to delete any // dynamically allocated variables cout << "done"<<endl; #ifdef WIN32 system("pause"); #endif return 0; }    A side note on: #ifdef ... #endif: These lines create a “conditional compile”. Visual Studio defines a precompiler constant called WIN32, while other compilers, like the Gnu C++ compiler, do not. Thus, the line system(“pause”); is compiled in Visual Studio, but left out of a gnu compile. This line lets you run with the debugger in VS and pause your program before exiting, so the window doesn’t disappear before you can read the output. Using a pointer to access data in memory: 2. Starting from the basic program: assign p the address of i, and output the value where it points. move it to point to j and k and output those values. point it to the first element of the array data and output it point it to the 3rd element of data and output it. using your pointer variable, change the values of the i and the 1st and 3rd elements of data. Compare your program with your partners’, and make sure you all are understanding how things work. 3. Starting from the previous (part 2) program: initialize pc to the beginning of my_cstring. use it to capitalize the 1st and 5th characters of my_cstring. (hint: toupper(c) returns the upper-case equivalent of c) NOTE: Either of the following will point to the 1st char: i. pc = my_cstring; // using name of array ii. pc = &my_cstring[0]; // or taking address of 1st element You could use either of the following to point to the 5th element: i. pc = my_cstring+4; // 4 elements over from beginning of array ii. pc = &my_cstring[4]; // or taking address of 5th element.
(you'll obviously want to output those values after you change them...) use your char pointer to output the integer value of each element of my_cstring (including the 8th element, my_cstring[7]). (hint: you could copy each element to an integer and output it, or use static_cast<int>(...) ). NOTE: The following might be helpful. (hmmm. why does "cout << *pc" print 1 char, but “cout << pc” more than 1 char?): i. pc = my_cstring; cout << pc << endl; cout <<*pc << " "<< pc[0] << " " <<setw(3)<<static_cast<int>(*pc)<<endl; Name your program basic_pointers.cpp Part 2: Write some code using pointers Here’s the basic program for next few parts of this lab: #include <iostream> using namespace std; int main() { int i = 5, j=51, k=62; int data[5] = {10, 20, 30, 40, 50}; char my_cstring[8] = "the fox"; int *p = NULL; char *pc = NULL; // do something useful with the arrays and the pointers // . . . // and put some clean-up code to delete any // dynamically allocated variables cout << "done"<<endl; #ifdef WIN32 system("pause"); #endif return 0; }    A side note on: #ifdef ... #endif: These lines create a “conditional compile”. Visual Studio defines a precompiler constant called WIN32, while other compilers, like the Gnu C++ compiler, do not. Thus, the line system(“pause”); is compiled in Visual Studio, but left out of a gnu compile. This line lets you run with the debugger in VS and pause your program before exiting, so the window doesn’t disappear before you can read the output. Using a pointer to access data in memory: 2. Starting from the basic program: assign p the address of i, and output the value where it points. move it to point to j and k and output those values. point it to the first element of the array data and output it point it to the 3rd element of data and output it. using your pointer variable, change the values of the i and the 1st and 3rd elements of data. Compare your program with your partners’, and make sure you all are understanding how things work. 3. Starting from the previous (part 2) program: initialize pc to the beginning of my_cstring. use it to capitalize the 1st and 5th characters of my_cstring. (hint: toupper(c) returns the upper-case equivalent of c) NOTE: Either of the following will point to the 1st char: i. pc = my_cstring; // using name of array ii. pc = &my_cstring[0]; // or taking address of 1st element You could use either of the following to point to the 5th element: i. pc = my_cstring+4; // 4 elements over from beginning of array ii. pc = &my_cstring[4]; // or taking address of 5th element.
(you'll obviously want to output those values after you change them...) use your char pointer to output the integer value of each element of my_cstring (including the 8th element, my_cstring[7]). (hint: you could copy each element to an integer and output it, or use static_cast<int>(...) ). NOTE: The following might be helpful. (hmmm. why does "cout << *pc" print 1 char, but “cout << pc” more than 1 char?): i. pc = my_cstring; cout << pc << endl; cout <<*pc << " "<< pc[0] << " " <<setw(3)<<static_cast<int>(*pc)<<endl; Name your program basic_pointers.cpp Part 2: Write some code using pointers Here’s the basic program for next few parts of this lab: #include <iostream> using namespace std; int main() { int i = 5, j=51, k=62; int data[5] = {10, 20, 30, 40, 50}; char my_cstring[8] = "the fox"; int *p = NULL; char *pc = NULL; // do something useful with the arrays and the pointers // . . . // and put some clean-up code to delete any // dynamically allocated variables cout << "done"<<endl; #ifdef WIN32 system("pause"); #endif return 0; }    A side note on: #ifdef ... #endif: These lines create a “conditional compile”. Visual Studio defines a precompiler constant called WIN32, while other compilers, like the Gnu C++ compiler, do not. Thus, the line system(“pause”); is compiled in Visual Studio, but left out of a gnu compile. This line lets you run with the debugger in VS and pause your program before exiting, so the window doesn’t disappear before you can read the output. Using a pointer to access data in memory: 2. Starting from the basic program: assign p the address of i, and output the value where it points. move it to point to j and k and output those values. point it to the first element of the array data and output it point it to the 3rd element of data and output it. using your pointer variable, change the values of the i and the 1st and 3rd elements of data. Compare your program with your partners’, and make sure you all are understanding how things work. 3. Starting from the previous (part 2) program: initialize pc to the beginning of my_cstring. use it to capitalize the 1st and 5th characters of my_cstring. (hint: toupper(c) returns the upper-case equivalent of c) NOTE: Either of the following will point to the 1st char: i. pc = my_cstring; // using name of array ii. pc = &my_cstring[0]; // or taking address of 1st element You could use either of the following to point to the 5th element: i. pc = my_cstring+4; // 4 elements over from beginning of array ii. pc = &my_cstring[4]; // or taking address of 5th element.
(you'll obviously want to output those values after you change them...) use your char pointer to output the integer value of each element of my_cstring (including the 8th element, my_cstring[7]). (hint: you could copy each element to an integer and output it, or use static_cast<int>(...) ). NOTE: The following might be helpful. (hmmm. why does "cout << *pc" print 1 char, but “cout << pc” more than 1 char?): i. pc = my_cstring; cout << pc << endl; cout <<*pc << " "<< pc[0] << " " <<setw(3)<<static_cast<int>(*pc)<<endl; Name your program basic_pointers.cpp

Explanation / Answer

#define ARR_COLUMNS 10 #define ARR_ROWS 10 float* arr = calloc (ARR_COLUMNS * ARR_ROWS, sizeof(float)); int get(float* arr, int x, int y) { if (x<0 || x>= ARR_COLUMNS) return 0; if (y<0 || y>= ARR_ROWS) return 0; return arr[ARR_COLUMNS*y+x]; } void set (int* arr, int x, int y, float val) { if (x<0 || x>= ARR_COLUMNS) return; if (y<0 || y>= ARR_ROWS) return; arr[ARR_COLUMNS*y+x] = val; }

int key = 1 ;


cout << "Press 0 then Enter to clean up the memory" << endl;
cin >> key ;

if(key == 0)
{
      for(int i = 0 ; i < size ; i++)
      {
         delete[] MainArray[i];
         MainArray[i] = NULL;
      }

      delete[] MainArray;
      MainArray = NULL;
      cout << "Memory cleared" << endl;

}


system("PAUSE");

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote