Implement the stringifyArray(int[],int) used in the code below. #include <iostre
ID: 3542428 • Letter: I
Question
Implement the stringifyArray(int[],int) used in the code below.
#include <iostream>
#include <cstdlib>
using namespace std;
/* Prints the given integer array to the screen.
*
* Require: size >= 0 &&
* size == array[last+1];
* Ensure: Only array memory location are traversed &&
* output format: "{ x,y,z }"
*/
void stringifyArray(int array[], int size);
int main() {
int integers_one[3] = { 0,1,3 };
int integers_two[2][3] = {{ 0,1,2 },{2,3,4}};
cout << "Integer array: " << endl;
stringifyArray(integers_one, 3);
exit(EXIT_SUCCESS);
}
Explanation / Answer
void stringifyArray(int array[], int size)
{
if (size>=0 && size==sizeof(array)/sizeof(*array))
{
cout<<"{";
for(int i=0;i<size;i++)
{
cout<<array[i];
if (i!=size-1)
cout<<",";
}
cout<<"}"<<endl;
}
}