Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,165 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 2,526 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Code Question.

 
Reply to this topicStart new topic

Code Question.

chadihuoma
post 1 Jul, 2008 - 11:54 PM
Post #1


New D.I.C Head

*
Joined: 19 Jun, 2008
Posts: 1

#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?
User is offlineProfile CardPM

Go to the top of the page


no2pencil
post 2 Jul, 2008 - 12:03 AM
Post #2


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,388



Thanked 35 times

Dream Kudos: 2325

Expert In: Goofing Off

My Contributions


QUOTE(chadihuoma @ 2 Jul, 2008 - 02:54 AM) *

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?


Depends on how you want to govern the for loop.

If you want to check data[0], then yes you will need to start index at zero. Also checking while index is less than size means that you will not perform a check on the last array element that equals the max value of size, because it's checking while index is less than size. If you need to check that value as well you will want

cpp

for(index=0;index <= size; index++) {
...
}


I hope this helps...
User is offlineProfile CardPM

Go to the top of the page

captainhampton
post 2 Jul, 2008 - 05:56 AM
Post #3


D.I.C Addict

Group Icon
Joined: 17 Oct, 2007
Posts: 501



Thanked 2 times

Dream Kudos: 775
My Contributions


For your question 7 here is a good run down of the static keyword and the use and meaning of it.
http://msdn.microsoft.com/en-us/library/s1sb61xd(VS.80).aspx
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/6/08 11:21AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month