Hi folks,

I'm a beginner student preparing for my final C++ exam by completing some practice problems, and this particular one has me stumped.

If someone could please explain (step-by-step) how to calculate the correct answer I would appreciate it (answer should be "9"). I've tried different calculations such as:

if (2 > 0)
la = 0 + 1 = 1
if (6 > 1)
la = 1 + 1 = 2
if (5 > 2)
la = 2 + 1 = 3
if (7 > 3)
la = 3 + 1 = 4
if (9 > 4)
la = 4 + 1 = 5

. . . but it's obvious I'm not doing it right.

Please guide me on how to get the right answer. Here's the problem:

Code:

int arr[5] = {2, 6, 5, 7, 9};
int la = arr[0];
for (int c = 1; c < 5; c = c + 1)
{
    if (arr[c] > la)
    {
        la = arr[c];
    }
}
cout << la << endl;
[/code]
The code is putting the first value of the array into a variable called la. Then, it's looping through the array, starting at the 2nd element (index 1) and is comparing each element to la. If the current element is greater than la, la is set to the value of the current element. At the end, it should print out 9 because it is the largest element.
Thank you, Souvik1997! So it seems there is no need to write out the loop to get the answer. You should just be able to look at the problem

Code:
if (arr[c] > la)

to determine that the 5th array (or 4th element/index), number 9, is the largest element and thus the answer. Or is that true? It really helps me to write things out, so how would you start the process?

I'm sorry... I realize I appear to be dumb as a brick! I have a 98% average at this point in class but every now and then something just doesn't compute. Sometimes I need things explained to me like a 5th grader before I can get it, and I guess this is one of those times. If you're not too fed up with me by this point and you don't mind, further explanation would be SO appreciated.

How would I hand write this problem to reach the conclusion that 9 is the answer?
I would just write a number to indicate which iteration you are on. Next to that white the comparison' and perhaps the value of la.
Well, you do actually need the whole program to see what it's doing. Without the loop, you won't know which elements of the array it's actually checking (e.g. is it checking every other element?). If I were to write it out, I would do so like this:

Code:

la is 2 at first.
if (6 > 2)
   la is 6
if (5 > 6)
   5 isn't greater than 6, so nothing happens
if (7 > 6)
   la is 7
if (9> 7)
  la is 9
print la
Thank you, thank you, thank you, Souvik1997! I have been trying to get this type of answer for 2 days on other Web sites but no one would help. What confused me most was the

Code:
for (int c = 1; c < 5; c = c + 1)

part of the problem, which distracted me from calculating the correct output of "la." Again, thank you for taking the time to answer! Very Happy
We hope you stick around Sad

Introduce Yourself and Share Projects. And stuff.
Thanks, comicIDIOT! Sure wish I had hooked up with you guys weeks ago. My C++ days are over by Monday afternoon (the day of the final exam). But perhaps I'll require future programming assistance in another language. If so, I'll be sure to visit again...

Take care!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement