#including<iostream>
using namespace std;
int array[10]={3,5,2,7,8,4,6,7,4,11};
int findMax()
{
int length=array.length();
int max;
int count;
for(int i=0;i<length;i++)
{
count+=1
if(array[i]>max)
max=array[i];
}
return max;
}
int main()
{
findMax(array[10]);
}
a) create a function to find the position of the max value in the array
b) in your main function create and initialize an array, then call this findMax function to display the position of the maximum value in the array and display this value based on the position information.
I am lost and i cant figure out what i should be doing if anyone would help.
First of all would appreciate if you posted your code in [CODE] tags. Would be alot easier to read.
Here's a working code, hopefully this is what you needed help with.
Read the comments in the code for explanations.
Code:
int findMax() {
int length = (sizeof(array) / sizeof(int)); // Initialized a variable to get the size of the array[]
int max = 0; // Initilized max, if you use an uninitialized variable you'll get an error
for (int i = 0; i < length; i++) {
if (array[i] > max) { // Checked if the current array number is bigger than 0
max = array[i]; // If True, change value of max to the current number in array
}
}
return max; // Return the biggest number in array[]
}
Hopefully this made it more clear, if there were something you didn't understand. Feel free to ask more.
Bai
Originally Posted by ERGaergaergaergaergearg
First of all would appreciate if you posted your code in [CODE] tags. Would be alot easier to read.
Here's a working code, hopefully this is what you needed help with.
Read the comments in the code for explanations.
Code:
int findMax() {
int length = (sizeof(array) / sizeof(int)); // Initialized a variable to get the size of the array[]
int max = 0; // Initilized max, if you use an uninitialized variable you'll get an error
for (int i = 0; i < length; i++) {
if (array[i] > max) { // Checked if the current array number is bigger than 0
max = array[i]; // If True, change value of max to the current number in array
}
}
return max; // Return the biggest number in array[]
}
Hopefully this made it more clear, if there were something you didn't understand. Feel free to ask more.
Bai
i'm still a little confused i can understand for the most part how to do the max function, but i keep messing with it because i need to have a separate function called main and i need to call the function in order to solve the max problem for the array in the main functions.
Originally Posted by disturbed14x
i'm still a little confused i can understand for the most part how to do the max function, but i keep messing with it because i need to have a separate function called main and i need to call the function in order to solve the max problem for the array in the main functions.
Well, if that's the case. You need to pass the array[] and length variable from main() to the function by parameter.
Code:
#include <iostream>>
using namespace std;
int findMax(int array[], int length);
int main() {
int array[10] = { 3, 5, 2, 7, 8, 4, 6, 7, 4, 11 };
int length = (sizeof(array) / sizeof(int)); // Getting the size of the array
int maxNum = findMax(array, length); // Since the function returns a variable, you need something to hold the variable
// Passed the array[] and length variable as argument to the function
cout << maxNum << endl; // Display the max value returned
return 0;
}
int findMax(int array[], int length) { // Pass an array and a length variable as parameter
int max = 0;
for (int i = 0; i < length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
Bai
No response from OP since a week ago so I assume it's solved.