ButteredToast
ButteredToast

Reputation: 1

Finding a max from a known list of variables WITHOUT using arrays

#include<iostream>
#include<string>
#include<ctime>
using namespace std;

int main()
{
int box1 = 30;
int box2 = 99;
int box3 = 140;
int box4 = 200;
int box5 = 220;
int box6 = 260;
int box7 = 300;
int box8 = 310;
int box9 = 500;
int box10 = 100;

return(0);
}

I've tried something along the lines of this:

string var;
int i = 1;

for(i=1;i<=10;i++) {
var = "box" + std::to_string(i);
cout << var << endl;
}

but it kept telling me that std::to_string was an ambiguous call to an overloaded function. Not sure where to go from here.

Upvotes: 0

Views: 95

Answers (5)

sehe
sehe

Reputation: 393134

Since we're deconstructing the question so delightfully:

Live On Coliru

#include <algorithm>
#include <iostream>

int main() {
    std::cout << std::max({ 30, 99, 140, 200, 220, 260, 300, 310, 500, 100});
}

Not using arrays, not even as part of the implementation of a vector or similar

Upvotes: 2

Jahid
Jahid

Reputation: 22438

If you just want to find max then you can do it like this:

Example code:

#include<iostream>
#include<string>
#include<ctime>
using namespace std;


void Max() {
}

int max_val=0;;

template<typename T , typename ... type_pack>
int Max(T x, const type_pack... rest) {

    max_val=(max_val>x)?max_val:x;
    Max(rest...);
    return max_val;
}

int main()
{   
int box1 = 30;
int box2 = 99;
int box3 = 140;
int box4 = 200;
int box5 = 220;
int box6 = 260;
int box7 = 300;
int box8 = 310;
int box9 = 500;
int box10 = 100;

std::cout<<Max(box1,box2,box3,box4,box5,box6,box7,box8,box9,box10);

return(0);
}

You can pass all the variables as argument to the function Max() (the number of argument is not fixed) and it will return the Max for you... without using array or any loop (It uses recursion).

Upvotes: 0

Neil Kirk
Neil Kirk

Reputation: 21773

This solution finds your maximum without using any arrays or data structures whatsoever.

int main()
{
    std::cout << 500;
}

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490198

Even if you didn't prohibit using an array, I'd probably prefer a vector to an array anyway.

#include<iostream>
#include <algorithm>

int main()
{
    std::vector<int> boxes{30, 99, 140, 200, 220, 260, 300, 310, 500, 100};
    std::cout << *std::max_element(boxes.begin(), boxes.end());
}

Seems straightforward enough.

Upvotes: 2

Qaz
Qaz

Reputation: 61920

Since you asked* (proof that it works, preprocessed (scroll down for output)):

#include <algorithm>
#include <iostream>

#include <boost/preprocessor.hpp>

#define VAR_N(z, n, nameBase) BOOST_PP_CAT(nameBase, BOOST_PP_INC(n))

int main() {
    int box1 = 30;
    int box2 = 99;
    int box3 = 140;
    int box4 = 200;
    int box5 = 220;
    int box6 = 260;
    int box7 = 300;
    int box8 = 310;
    int box9 = 500;
    int box10 = 100;

    std::cout << std::max({BOOST_PP_ENUM(10, VAR_N, box)}); //500
}

*Seriously, don't have ten variables whose names only differ by successive numbers. That's what arrays are for.

Upvotes: 6

Related Questions