LANGUAGE C THE SAMPLE CODE FOR THIS PROGRAM IS POSTED BELOW
LANGUAGE C++
THE SAMPLE CODE FOR THIS PROGRAM IS POSTED BELOW:
#include <iostream>
int main() {
return 0; }
Part II. Pointers and Arrays 4. Pointers and arrays: Now, starting from the (original) basic program a. create a pointer, initialize it to the beginning of the array data b. output the elements of data by indexing the array data[ ] using a loop (and the indexing operator L1) output the elements of data by indexing your pointer using a loop, with the pointer pointed at the beginning of the array and using [] c. d. output the elements of data by incrementing the pointer each time through the loop. your loop control could look like this for (i=0; 1 5; 1++) or like this (why does this work?): for (p=data; p data+3; p++) e. reset your pointer to point at the beginning of the array again, then output the elements of data by adding i, a loop counter, to the pointer and dereferencing the summed address. Use something like: cout *(p+i) (Be sure to point back at the beginning of the array before the loop!) Q: how is coutSolution
#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;
p = data; // sub-part a.
for(int l=0;l<i;l++) { //sub-part b.
cout << data[l] << \" \";
}
cout << endl;
for(int l=0; l<i; l++) { //sub-part c.
cout << p[l] << \" \";
}
cout << endl;
for(int l=0; l<i; l++) { //sub-part d.
cout << *(p+l) << \" \"; /*This works because of pointer arithmetic. When we
add any integer to a pointer, it moves forward to point to the next element, instead
of adding it simply to the address. */
}
cout << endl;
// 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; }

