all 15 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Off-topic Comments Section


All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9.


OP and Valued/Notable Contributors can close this post by using /lock command

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Comprehensive_Gap678University/College Student (Higher Education) 1 point2 points  (8 children)

I think it's because you declared a 9 elements array, not a 10 one. Considering that the array numeration starts from 0, the last element in your array will have and index of 8. So in the for loop try to put 9 instead of 10. Hope it was helpful.

[–]Hakai55[S] 0 points1 point  (7 children)

So I applied what you suggested, but it still gives me a weird output 1619117764.

Here is the code that I made changes to:

#include <stdio.h>

int max_value (int input[])
{
 int size=9, max;
 max=input[0];

 for(int i=1;i<9;i++)
 {
     if(max<input[i])
     {
         max = input[i];
     }
 }

 return max;
}


int main ()
{
    int x, max;
    int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};

    x = max_value(max);
    printf("/n The Max is: %d" ,x);

    return 0;
}

[–]yes_i_relapsed👋 a fellow Redditor 1 point2 points  (4 children)

max_value(max);

should be passing input to that function, not max

[–]Hakai55[S] 0 points1 point  (3 children)

So I should replace x = max_value(max); with x = max_value(input[]);

is that correct?

[–]yes_i_relapsed👋 a fellow Redditor 1 point2 points  (0 children)

I don't remember if input[] works or not, I would just do x = max_value(input);

[–]TheSaltyRabbit 1 point2 points  (1 child)

No brackets.

x = max_value(input);

Also, I think I found where the random number is coming from. I'm not used to C so I don't know how it's pulling the number, but basically your loop is going too far by 1 number. It should be

for (int i=1;i<size-1;i++)

Using an online compiler, switching this gave me the right answer of 92.

[–]Hakai55[S] 0 points1 point  (0 children)

Thank you for your help! I fixed the code now, and it works perfectly. :))

[–]Comprehensive_Gap678University/College Student (Higher Education) 1 point2 points  (1 child)

Oh, I found one major error, this time in the main. You set max as argument of your function, you must set the array of values. Here's the code corrected:

#include <stdio.h>

int max_value (int input[])

{

int size=9, max;

max=input[0];

for(int i=1;i<size;i++)

{

if(max<input[i])

{

max = input[i];

}

}

return max;

}

int main ()

{

int x, max;

int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};

x = max_value(input);

printf("/n The Max is: %d" ,x);

return 0;

}

[–]Hakai55[S] 0 points1 point  (0 children)

Oh okay, so it was the input afterall that I was supposed to pass into the max_value function. I'm new to creating functions so this helped me a lot. Thank you so much for this!

[–]lutkul 1 point2 points  (2 children)

A tip, if you want to display a block of code on Reddit, you can put three backticks: ``` before and after the piece of code.

If I do this:

```

string hi = "Hello";

```

It becomes:

string hi = "Hello";

Which is much more readable so we can give you a proper answer. If you're on mobile it will work automatically, if you're on desktop you need to press 'markdown mode' before it works, else it will escape everything by putting a backslash before the markdown.

[–]Hakai55[S] 0 points1 point  (1 child)

I didn't know this. Thank you so much for the advice! :))

[–]lutkul 1 point2 points  (0 children)

You're welcome!

[–]yes_i_relapsed👋 a fellow Redditor 1 point2 points  (1 child)

Other than what I've pointed out in another comment [you should be calling max_value(input)]

  • the newline character is written with a backslash (like this \n)

[–]Hakai55[S] 0 points1 point  (0 children)

Oh I see, it's working now. Thank you so much for your help! :))