| |

VerySource

 Forgot password?
 Register
Search
View: 1235|Reply: 9

How to make the max function in c best

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-28 11:30:01
| Show all posts |Read mode
How to write a max function with unlimited numbers? Please enlighten me
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-28 11:45:01
| Show all posts
I also want to know
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-28 12:00:01
| Show all posts
Use indefinite parameter functions...

max(...)
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-9-28 12:15:01
| Show all posts
The array should work.
Inside, bubble sorting, only one side will do!
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-9-30 16:00:01
| Show all posts
std::max_element
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-30 18:00:01
| Show all posts
Put all the numbers to be compared in an array, and use the array name as a parameter to compare.
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-9-30 22:00:01
| Show all posts
int mymax( int const *arg, int size)
{
int m = *arg;
      for( int i = 1; i <size; ++i)
{
    if (m <arg[i]) m = arg[i];
        
}
return m;

}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-1 09:30:01
| Show all posts
There is a limit to the number of arrays in C language, it is to be less than a set constant, the array setting may not be too large, and there may be no limit when reading files, or you can try the vector in C++
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-1 09:45:01
| Show all posts
How many do you want to use?
To compare the size, you don't have to use MAX.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-1 10:00:01
| Show all posts
You can convert the value into a string (you should be able to solve the string length.),
Use ASCII code to compare, in the case of the same number of bits, the larger the ASCII code, the smaller the value.
In the case of different digits, you can do the data alignment. Then compare the ASCII code, the larger the ASCII code, the smaller the value.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list