# Thread: finding highest number entered

1. ## finding highest number entered

Isn't there an easier way to write this program?

Code:
```#include <iostream>
using namespace std;

int main()
{
int num1;
int num2;
int num3;
int num4;

cout << "Enter four numbers" << endl;
cin >> num1;
cin >> num2;
cin >> num3;
cin >> num4;

if (num1 > num2 && num1 > num3 && num1 > num4)
cout << "The highest number entered was the first one" << endl;

else if (num2 > num1 && num2 > num3 && num2 > num4)
cout << "The highest number entered was the second one" << endl;

else if (num3 > num1 && num3 > num2 && num3 > num4)
cout << "The highest number entered was the third one" << endl;

else
cout << "The highest number entered was the fourth one" << endl;

return 0;

}```

2. Here's a thought:
Code:
```    char *ordinal[] = {"first","second","third","fourth"};

int num[4];
cin >> num[0] >> num[1] >> num[2] >> num[3];

int maxi = findmax(num,4);

cout << "The highest number entered was the " << ordinal[maxi] << " one" << endl;```
You just have to implement findmax().

gg

3. Originally posted by Codeplug
Here's a thought:
Code:
```    char *ordinal[] = {"first","second","third","fourth"};

int num[4];
cin >> num[0] >> num[1] >> num[2] >> num[3];

int maxi = findmax(num,4);

cout << "The highest number entered was the " << ordinal[maxi] << " one" << endl;```
You just have to implement findmax().

gg
Can you explain findmax and *ordinal[] for me please?

I'm a little fimiliar with arrays, but pointers...

4. You will have to learn pointers, but for now, maybe this will be easier to understand (have you worked with c++ strings yet?):

Code:
```#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,max,maxpos,num[4];
string ordinal[4] = {"first", "second", "third", "fourth"};

for(i=0;i<4;i++)
{
cout << "Enter a number > ";
cin >> num[i];
}
max=num[0];
maxpos=0;
for(i=1;i<4;i++)
if(num[i]>max)
{   max=num[i];
maxpos=i;
}

cout << "The highest number entered was the " << ordinal[maxpos] << " one." << endl;

return 0;
}```

5. Can anyone explain findmax and *ordinal[] for me?

6. I guess you haven't learned much about functions yet either...

int findmax(num,4) would be a function (written by you) that would receive two parameters: "num", an array of "somethings", presumably integers, and "n", or in this case "4", which is how many elements are in the array. This function would return an integer giving the array index of the highest num.

char *ordinal[] is an array of pointers to c-style character strings, and Codeplug's example was initializing the array with pointers to the four string literals "first","second","third","fourth".

Like I said, you do have to learn pointers if you want to program. Functions too.

7. Greetings.

Here's a sample template for finding MAX:

Code:
```

// calculate the zero-based offset of 'index'

template <typename type>
unsigned mem_pos(type * first, type * index) {
return (unsigned)((unsigned)index -(unsigned)first)/ sizeof(type);
}

// return a pointer to the MAX element

template <typename type>
type *
max(type data[], type * end) {
type * index, max = *data;
for(index = data; data != end; ++data)
if(*data > max)
index = data;
return index;
}

template <typename type>
type
max_value(type data[], type * end) {
return * max<type>(data, end);
}

// return the actual zero-based index of the MAX element

template <typename type>
unsigned
max_index(type data[], type * end) {
return mem_pos<type>(data, max<type>(data, end));
}

int main()
{
double mem[100], * input = &mem[-1];

puts("Enter up to 100 doubles, '0' to exit.");

do{
cin >> *(++input);
}while(*input != 0 && input != mem+100);

unsigned index = max_index(mem, input);

cout << endl << mem_pos(mem, input)
<< " numbers entered." << endl
<< "Maximum :  " << mem[index] << endl
<< "Position : " << index << endl;

return getch();
}```

8. Here's another one:
Code:
```void find_max_confusion_for_newbie_by_using_templates(){
...
}```
gg

9. Templates are merely advanced substitution macros that are very easy to use.

The central point was making it clear that the address of the maximum value was the most important data to collect because from it we can :
2) retrieve the maximum value.
3) calculate the index of max.

My second point was that it is actually desirable to template algorithms, since we can build catalogues of code for later use.

10. In all seriousness, Volk, if you learn and understand Sebastiani's post, you will have indeed learned a lot.

I suggest you kick off your learning by understanding pointers and functions first.

gg

11. Last time I replied to this question I got bashed for giving away the answer. It was the exact same question....

:-D!

12. ## The idea...

... is to store the input digits in an array. U can do it without pointers and such. But if you don't know the number of the inputs, then create a dynamic array. See the tutorial N14 I think on this page to create such array.
But the simpliest method is using static array -
Code:
`int arr[500];`