Write a function that will return the max element in an array of 10 integers

Hi, can someone help me with this. This is what I have so far !
#include <iostream>
#include <cdtg>
using namespace std;

int max(array<int,10>Data) {

int i = 0;
int count = 9;

while (count > 0) {
if (i < i+1) {
i+1;
}
else {
i;
}
i++;
count = count -1;
cout << endl;
}

int main()
{
Data data;
setRandom(data);
cout << data << endl;
cout << "Maximum value = " << max(data) << endl;
return 0;
}
A for loop would be better.

You'll need to adapt it for your function and your data.

1
2
3
4
5
6
7
int highest = -1;

for (int i=0; i<10; i++)
{
   if (array[i] > highest)
      highest = array[i];
}
simple and good, but i would recommend a little change in your code iHutch105

1
2
3
4
5
6
7
int highest = array[0];

for (int i=1; i<10; i++)
{
   if (array[i] > highest)
      highest = array[i];
}


because the array could only contain negativ integers and that would cause -1 to be the "max" value or?

regards
Last edited on
It is very interesting what is it?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int max(array<int,10>Data) {
 
int i = 0;
 int count = 9;
 
while (count > 0) {
 if (i < i+1) {
 i+1;
 }
 else {
 i;
 }
 i++;
 count = count -1;
 cout << endl;
 }

i don't want to be rude, but this function simply does nothing at all.. except for the newlines
There is a general function in C++ that returns the maximum element of a sequence. It is named std::max_element and declared in <algorithm>
I do not understand this part ..
1
2
3
4
5
6
if (i < i+1) {
 i+1;
 }
 else {
 i;
 }


can some one explain this .
drugs, must be drugs.
Topic archived. No new replies allowed.