I need to make a program that lets the user input a bunch of numbers using an array that contains a function " ( int mymax ( int array[], int numofelements )
this fuction returns the max value of the array, i need help with inputting the arrays because there is no limit on what they can input and also the math on sorting the array, not sure if that goes in the mymax or main function, THANK YOU VERY MUCH
If you are unsure of the number of user entries, the array will need to be dynamic.
You could also just make the array very large and hope the user doesn't input more than the max.
I assume the second might be the answer for you if you have not learned about dynamic arrays.
pseudo-code:
1 2 3 4 5 6 7
user enters number
while (number != -1)
{
put number into array
increment element counter
read in from user again
}
this way you will end up with an array and a number of elements in that array, which you will then be able to pass to your max value function.
Note that your array will be very large, but if the user only enters some values, the rest of the array is unused and full of garbage. This is why you need to know how many elements were entered so you do not consider garbage values.
For your int mymax(int number[], int amount); you have the right idea, but remember that you want to read all the elements, not just 5.
Sorry I'm not very good at coding, my brain is starting to hurt from reading that, but i will try again, please give me some more help if you can thanks
the function should work for any number of elements, so the amount of elements you should search through is variable.
look at the function definition: int mymax(int number[], int amount);
you pass in two values. int number[] is your array with all of the values the user stored. int amount is the number of elements entered in that array. Even though the array could be massive, you only want to consider the number of elements inserted into the array. This is why the second parameter is neccesary. You don't need to create a new variable, you just need to use the value passed in.
Imagine you make an array of length 20 and the user inputs 13 numbers:
Notice you did not overfill your array but you should only consider 13 elements. When you loop through the array, you should loop 13 times (the number of elements counted) rather than the size of the array, because the values in the spots initialized could be anything!
You will probably want your array to be bigger than 20 though :)
--
Your array needs to be initialized to something large (larger than what you think the user will input).