help with program please

Posts 1–5 of 5 · Page 1 of 1
help with program please
Code:
#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
Quote Originally Posted by ERGaergaergaergaergearg View Post
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.
Quote Originally Posted by disturbed14x View Post
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.
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?