15,358,470 members
1.00/5 (1 vote)
See more:
I have no idea how to complete this problem from what I have so far. Maybe what I have so far is even wrong, so if someone can explain exactly how to code it and how the code works, that would be great!

What I have tried:

C
```int maxValue(int n, int x[n])
{
if (n< 0)
{
return -1;
}

int max = x[0];
int maxIndex = 0;

for (int i = 1;i < n; i++)
{
if (x[i] > max)
{
maxIndex = i;
max = x[i];
}
}
return maxIndex;
}```
Posted
Updated 5-Oct-21 2:04am
v2

## Solution 1

Your code should work - and a quick test says it does.
So if you wrote it, you don't need it explained, and you should have no problem in using that function since all it needs it calling - which is trivial.
`printf("%u\n", maxValue(3, arr));`
Will do it, provided there are three elements in the array.

If you didn't, then why are you pretending you did, and what is stopping you from understanding such trivial code yourself?
jeron1 4-Oct-21 16:19pm

"If you didn't"

Bingo! ;-)
OriginalGriff 4-Oct-21 16:27pm

One of my big "no-no"'s is being lied too by peopel who assume I'm too dumb to notice ... :sigh:
Member 15381863 4-Oct-21 21:07pm

The person teaching me the code doesn't teach it well, he tells me what should go where without explaining why and that's it. So I have no idea how half this stuff works in the end. While I try to do it, I don't even know if it's right or wrong either, or if it is complete unless my terminal shows me the right product, which in this instance it did not. Leading me to ask you guys for help, kindly.
OriginalGriff 5-Oct-21 3:42am

Do you have any idea how often we hear that? And how often it's the case that the OP hasn't listened, hasn't paid attention, has spent more time on FaceBook, Twatter, TikTok, WhatsApp than actually working in class?

That may not be the case with you, but I'll say the same thing to you that I do to all the others: if your teacher is bad, read a book. If there is a course book, read it. Do the exercises. If there isn't get one: Addison Wesley, Wrox, MS Press - they all do excellent books on many, many languages.

The "Bad teacher" excuse means "I didn't try to learn" in nearly every case, I'm afraid.
Member 15381863 4-Nov-21 21:38pm

This is a late response, I apologize for leaving you hanging. However, I would like to tell you that regardless the circumstance, I have tried to understand how to do specific things, and it just doesn't click. It is not the lack of trying to learn, it is that I have to go through so many sources to get to a simple concept, when in reality I can come to a website like this, and have code explained for me exactly as it is supposed to be used. I can take these explanations and go on to create my own code right after. I do not think it is as easy as trying to teach myself concepts through books and whatnot when there are other priorities taking place. Full time jobs and other courses still exist, and if I must go through 17 sources to learn one simple concept so that "I can try to learn", then I'm afraid yes "I didn't try to learn". Either way, I appreciate your response, and if I made you upset in trying to help me understand some concepts, then I sincerely apologize.

## Solution 2

Quote:
How do I make a function that finds the index of the largest value in an array?

your code look rather good to me.
Can you complete the code with what is necessary to run it with your array?
So we can reproduce the problem.

I would only replace
C++
`int maxValue(int n, int x[n]) {`

with
C++
`int maxValue(int n, int x[]) {`

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
Member 15381863 4-Oct-21 21:09pm

Thank you for that tip, it will be very useful moving forward!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 CPallini 90 Richard MacCutchan 65 OriginalGriff 60 Dave Kreskowiak 50 dan!sh 35
 OriginalGriff 560 CPallini 260 Richard MacCutchan 240 Chris Copeland 190 0x01AA 140

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900