#7.
cpp
/*Write a for loop that scans through a dynamically allocated int array,
pointed to by a variable called data, keeping track of the greatest
value in a static int variable max. The array contains 100 values.
At the end of the loop, the array should be deleted.*/
int* data;
data = new int[100];
static int max = 0;
int index = 0;
for (index = 0; index < 100; index++)
{
if(data[index] > max)
{
max = data[index];
}
}
delete[] data;
QUESTION #7: What does the static int max mean? What is it saying about memory allocation? Is it saying that there is always space reserved for max, it doesn't have to reserve it everytime program runs. Also, it this space reserved at compile time or execution time?
#8 p. 840
Wrap the loop written in Exercise 7 in an int-returning function
called Greatest that takes the array and its size as parameters and
returns the value in max after the array is deleted. Be sure to change
the loop to work with the given array size rather than the constant 100.
cpp
int Greatest(/*inout*/ int data[], /*in*/ int size)
{
static max = data[0];
for (int index = 1; index < size; index++)
{
if (data[index] > max)
{
max = data[index];
}
delete[] data;
return max;
}
}
QUESTION #8: I'm reviewing these notes from exercises done in class. My question is on the loop, where "int index = 1). Is that supposed to be a zero there(index = 0)? If not, why are we starting the loop from the second element?