Language C THE SAMPLE CODE FOR THIS QUESTION IS POSTED BELOW
Language C++
THE SAMPLE CODE FOR THIS QUESTION IS POSTED BELOW(basic program):
Using a pointer to access data in memory: 2. Starting from the basic program: a. assign p the address of i, and output the value where it points. b. move it to point to j and k and output those values. c. point it to the first element of the array data and output it d. point it to the 3rd element of data and output it. e. 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: a. initialize pc to the beginning of my cstring b. use it to capitalize the 1st and 5th characters of my cstring. (hint: toupper(c) returns the upper-case equivalent of c) c. NOTE: a. Either of the following will point to the 1st char: // using name of array 1, pc=my-cstring; ii. pc -&my; cstring[0]; I/or taking address of 1st element b. 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. d. (you\'ll obviously want to output those values after you change them...) e. 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(..)) f. NOTE: a. The following might be helpful. (hmmm. why does \"coutSolution
#include <iostream>
#include <ctype.h> // to acces s toupper function
#include<iomanip> // for setw function
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
// a. Assign p to the address of i and output the value p is pointing to.
p = &i;
cout << \"p is pointing to the value: \" << *p << endl; // *p will return the value (i) at the address that p is poitning.
// b. Reassign the pointer p to point j and k one by one.
p = &j;
cout << \"p is pointing to the value: \" << *p << endl; // *p will return the value (j) at the address that p is poitning.
p = &k;
cout << \"p is pointing to the value: \" << *p << endl; // *p will return the value (k) at the address that p is poitning.
// c. Assign the pointer to the array data[]. P will be poitining to the base address (the first element) of the array.
p = data;
cout << \"p is pointing to the value: \" << *p << endl; // *p will return the value (k) at the address that p is poitning.
// d. Incretement the pointer to point to the 3 rd element of the array that is data[2]
p = &data[2];
cout << \"p is pointing to the value: \" << *p << endl; // *p will return the value (k) at the address that p is poitning.
// e. update the values in i, data[0] and data[2] using pointer
p = &i;
*p = 8;
cout << \"i value is updated to: \" << *p << endl; // *p will return the value (i) at the address that p is poitning.
p = data;
*p = 45;
cout << \"data[0] value is updated to: \" << *p << endl; // *p will return the value (i) at the address that p is poitning.
p = &data[2];
*p = 80;
cout << \"data[2] value is updated to: \" << *p << endl; // *p will return the value (i) at the address that p is poitning.
// 3
// a. Point pc to my_cstring
pc = my_cstring;
cout << \"pc is poiting to first character: \" << *pc << \", in the string: \" << pc << endl;
// convert the my_cstring[0] to upper case
toupper(*pc);
cout << \"pc is poiting to first character: \" << *pc << \", in the string: \" << my_cstring << endl;
// convert the my_cstring[4] to upper case
pc = &my_cstring[4];
toupper(*pc);
cout << \"pc is poiting to first character: \" << *pc << \", in the string: \" << my_cstring << endl;
// e. using character pointer pc to print integer value of each character in the character array including the null chacter
pc = my_cstring;
while (*pc != \'\\0\')
{
cout << *pc <<\" \" << setw(3) << static_cast<int>(*pc) << endl;
pc++;
}
// now pc is poiting to the string end. print the intger value of null character
cout << *pc <<\" \" << setw(3) << static_cast<int>(*pc) << endl;
cout << \"done\"<<endl;
#ifdef WIN32
system(\"pause\");
#endif
return 0;
}

