Given the following piece of code double balance5 10000 20
Given the following piece of code double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; double *p; int I; p = balance; a) Write a statement to output each array element\'s value using pointer. b) Write a statement to output each array element\'s value using balance as address.
Solution
main.cpp
#include <iostream>
using namespace std;
int main () {
int i;
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
p = balance;
// output each array element\'s value
cout << \"a. Array values using pointer \" << endl;
for ( i = 0; i < 5; i++ ) {
cout << \"*(p + \" << i << \") : \";
cout << *(p + i) << endl;
}
cout << \"b. Array values using balance as address \" << endl;
for ( i = 0; i < 5; i++ ) {
cout << \"*(balance + \" << i << \") : \";
cout << *(balance + i) << endl;
}
return 0;
}
Output :
![Given the following piece of code double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; double *p; int I; p = balance; a) Write a statement to output each array Given the following piece of code double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; double *p; int I; p = balance; a) Write a statement to output each array](/WebImages/41/given-the-following-piece-of-code-double-balance5-10000-20-1128357-1761602205-0.webp)