• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

firehawk12

Subete no aware
I'm late, but thanks for the explanations!

Thinking about it as a string object being formatted makes sense. It's just trying to figure out what format can do then.
 

Somnid

Member
Hints:

sizeof(foo) == 1. It doesn’t contains any function pointers.

The using statements are both correct and name actual types, but the declaration of wtf is illegal because you can’t have an instance of this type, even though it’s a legal type.

There’s also another thing I’m not saying it because it’s too stupid to believe, so I’ll see if anyone figures it out

Edit: actually I might be wrong, the declaration of wtf is legal,
But you can’t make a pointer of it, even though you can with int_func

So if it has size of 1 what's actually there in memory? Like my understanding of a "struct" is a block of bytes with some type information, so if you declare something a type of function, what does that mean if it's not a reference?
 
So if it has size of 1 what's actually there in memory? Like my understanding of a "struct" is a block of bytes with some type information, so if you declare something a type of function, what does that mean if it's not a reference?

It’s basically the same as an empty struct, because it’s not declaring a variable, it’s declaring a function. Size of an empty struct is defined to be 1, because you have to be able to take its address, and you wouldn’t want two different objects to have the same address
 

Somnid

Member
It’s basically the same as an empty struct, because it’s not declaring a variable, it’s declaring a function. Size of an empty struct is defined to be 1, because you have to be able to take its address, and you wouldn’t want two different objects to have the same address

So then would the compiler force me to implement int_func like it was part of a class?
 
Hey guys, I'm having some troubles with passing arguments from my main method to another so it can do calculations.

Code:
  public static void main(String[] args){
    double len, wid, priceGallon,paintReq;
    welcomeMsg();
    priceGallon = readPricePerGallon();
    System.out.println("The price per gallon is " + priceGallon);
    len = readLength();
    System.out.println("The length is " +len);
    wid = readWidth();
    System.out.println("The width is " + wid);
    double squareFeet = wid * len;
    System.out.println("The square feet is " + squareFeet);
    paintRequired(squareFeet);

I'm trying to take squareFeet and pass it into this method called paintRequired. But I keep getting errors.

Code:
 public static double paintRequired(){
    double paintGallons = 1;
    paintRequired = paintGallons * squareFeet;
    return paintRequired;

How can I get squareFeet passed into the new method I've created?

Also if anyone knows a resource I can learn about methods that'd be really helpful, I'm learning Java from the beginning and I've done well so far but I've just started methods and hit a wall:/
 
Hey guys, I'm having some troubles with passing arguments from my main method to another so it can do calculations.

Code:
  public static void main(String[] args){
    double len, wid, priceGallon,paintReq;
    welcomeMsg();
    priceGallon = readPricePerGallon();
    System.out.println("The price per gallon is " + priceGallon);
    len = readLength();
    System.out.println("The length is " +len);
    wid = readWidth();
    System.out.println("The width is " + wid);
    double squareFeet = wid * len;
    System.out.println("The square feet is " + squareFeet);
    paintRequired(squareFeet);

I'm trying to take squareFeet and pass it into this method called paintRequired. But I keep getting errors.

Code:
 public static double paintRequired(){
    double paintGallons = 1;
    paintRequired = paintGallons * squareFeet;
    return paintRequired;

How can I get squareFeet passed into the new method I've created?

Also if anyone knows a resource I can learn about methods that'd be really helpful, I'm learning Java from the beginning and I've done well so far but I've just started methods and hit a wall:/

public static double paintRequired(double squareFeet) { ... }

You need to include parameters in the parentheses.
 
Hey guys, I'm having some troubles with passing arguments from my main method to another so it can do calculations.

Code:
  public static void main(String[] args){
    double len, wid, priceGallon,paintReq;
    welcomeMsg();
    priceGallon = readPricePerGallon();
    System.out.println("The price per gallon is " + priceGallon);
    len = readLength();
    System.out.println("The length is " +len);
    wid = readWidth();
    System.out.println("The width is " + wid);
    double squareFeet = wid * len;
    System.out.println("The square feet is " + squareFeet);
    paintRequired(squareFeet);

I'm trying to take squareFeet and pass it into this method called paintRequired. But I keep getting errors.

Code:
 public static double paintRequired(){
    double paintGallons = 1;
    paintRequired = paintGallons * squareFeet;
    return paintRequired;

How can I get squareFeet passed into the new method I've created?

Also if anyone knows a resource I can learn about methods that'd be really helpful, I'm learning Java from the beginning and I've done well so far but I've just started methods and hit a wall:/

Your method signature is different. Currently your paintRequired Function is not talking in any parameters but you are passing it one anyway. Correct Way:


Code:
 public static double paintRequired(double val){
    double paintGallons = 1;
    paintRequired = paintGallons * squareFeet;
    return paintRequired;
 

Slavik81

Member
Yep. I said it was weird didn’t i? :)

This is not something you should ever do btw, it’s just one of the dark corners of c++
I think that oddity stems from C. It's a little known fact that you can forward-declare functions by typedefing their signature and then declaring them like variables. using is just acting like a fancy typedef.

Another fun one: you can swap your array index and your array variable. 5[arr] is every bit as valid as arr[5].
 
Your method signature is different.

You'll want to assign something to your passed parameter in the method body or your argument doesn't have much meaning.

public static double paintRequired(double squareFeet) { ... }

You need to include parameters in the parentheses.

Thanks guys. I'm starting to understand it a bit more but damn if these methods, arguments and parameters aren't confusing:/

I really wanna stick with it this time too, I gave up the last two times cause the roadblocks are annoying.
 

oxrock

Gravity is a myth, the Earth SUCKS!
I've been a tad bit worn out from working on my game lately so I decided to write a fractal tree script in python after seeing some cool pictures online. I also hardly ever use recursion so I thought it would help getting more practice in there as well. Anyhow, here's a picture made from an earlier version of the code:
b7uhHNY.png
Since then I've added a lot more randomness in the hopes of making the result look more natural.
updated example:

Here's a video using the updated code of a fractal tree taking shape: Fractal Tree Video. There's a link to the repository in the description if anyone's interested at taking a peek at the code.
 

Koren

Member
Great job...


Btw,in Python, it's really easy to develop a generic L-system engine, that makes this kind of procedural generation a breeze, and basically recursionless.

An by using generator functions, you can avoid memory costs.

If someone is interested, I'll post an example...
 

Slavik81

Member
The Algorithmic Beauty of Plants is a nice read. http://algorithmicbotany.org/papers/#abop

My supervisor wrote it 27 years ago. There has been a lot of progress improving plant modelling since then, but it's still a good introduction even today.

Would you believe there are researchers who spend a significant amount of time doing more or less the same sort of model-building you did? Sometimes we start with a biological processes and build matching rules to see if that process is the cause of the plant shape. Other times, we take guesses at the rules and hope it helps biologists narrow down candidates for the processes that inform the shape.

Of course, sometimes we just make things look pretty.
 

oxrock

Gravity is a myth, the Earth SUCKS!
The Algorithmic Beauty of Plants is a nice read. http://algorithmicbotany.org/papers/#abop

My supervisor wrote it 27 years ago. There has been a lot of progress improving plant modelling since then, but it's still a good introduction even today.

Would you believe there are researchers who spend a significant amount of time doing more or less the same sort of model-building you did? Sometimes we start with a biological processes and build matching rules to see if that process is the cause of the plant shape. Other times, we take guesses at the rules and hope it helps biologists narrow down candidates for the processes that inform the shape.

Of course, sometimes we just make things look pretty.

I've never studied this stuff and I kind of did it on a whim because the pictures looked cool. Thanks for the link to that book, it'd be nice to read more about this. Maybe I'll give it another go after I do.
 
Hey does anyone here have any experience working with VBA/Excel? I have to automate a lab outage schedule, I have completed the entire program except for one critical component :p

There's are two columns with the date and time format of yyyy-mm-dd h:mm I want to extract this value into vba and split the time and the day as a string.

How to go about this?
 

phisheep

NeoGAF's Chief Barrister
Btw,in Python, it's really easy to develop a generic L-system engine, that makes this kind of procedural generation a breeze, and basically recursionless.

An by using generator functions, you can avoid memory costs.

If someone is interested, I'll post an example...

Ooh, yes please Koren. I've got a fairly intricate thing on the go that could do with something exactly like this.
 

Koren

Member
Will do, just give me a bit of time to get a computer...

Hey does anyone here have any experience working with VBA/Excel? I have to automate a lab outage schedule, I have completed the entire program except for one critical component :p

There's are two columns with the date and time format of yyyy-mm-dd h:mm I want to extract this value into vba and split the time and the day as a string.

How to go about this?
IIRC, dates are basically stored as doubles, so you can just read the cell and use format to convert it to a string to you liking...

Like format(date, "YYYY-MM-DD") to get a string with the date only in this format.

I'll check if that doesn't work, that'd from memory.
 

Pokemaniac

Member
Hey does anyone here have any experience working with VBA/Excel? I have to automate a lab outage schedule, I have completed the entire program except for one critical component :p

There's are two columns with the date and time format of yyyy-mm-dd h:mm I want to extract this value into vba and split the time and the day as a string.

How to go about this?


Using VBA for pretty much anything is a mistake. If you must use Visual Basic, you really should at least use the full .NET version to write a standalone program.

VBA will inevitably become a maintainability nightmare if allowed to exist for long enough.
 

Koren

Member
Using VBA for pretty much anything is a mistake. If you must use Visual Basic, you really should at least use the full .NET version to write a standalone program.

VBA will inevitably become a maintainability nightmare if allowed to exist for long enough.
Well, to automatize a spreadsheet, I don't think it's that bad. I used to have very awful experiences (like when each language has his own, incompatible, version of Excel VB), but it's now pretty stable and usable.
 

Koren

Member
Ooh, yes please Koren. I've got a fairly intricate thing on the go that could do with something exactly like this.

So... about Python and L-systems (I hope it's short enough that people don't mind I post the pieces of code)

In case anyone is not familiar with thoses, L-systems are easy way to represent and construct fractals.

I'll take the Koch fractal (the snowflake) as an example...

You begin with a token or a sequence of tokens (e.g. "frrfrrf")

And you apply rules:
"f" is replaced by the sequence"flfrrflf"
"r" is replaced by "r"
"l" is replaced by "l"

Assuming you use a dict, it's easy to store rules:
Code:
Koch_rules = { "f" : "flfrrflf" }
(I won't store rules where the result is the same as the input)

You can write a generator function that take a set of rules and an iterable and behave as an iterable of tokens:
Code:
def Apply(rules, it) :
    for elem in it :
        if elem not in rules :
            yield elem
        else :
            for elem in rules[elem] :
                yield elem

To test:
Code:
"".join( Apply(Koch_rules, "rlrlf") )
will gives you the "rlrlflfrrflf" string

Now, to get a fractal, you have to apply the rules several times. You can use a loop, but it's better to chain iterables, since you won't have to store any string ever, the tokens will be processed automatically. You can do this insanely easily in Python:
Code:
def Fractal(it_rules, x) :
    for rules in it_rules :
        x = Apply(rules, x)
    return x

Functional programming gurus may prefer folding, using reduce from functools module (though not as Pythonic, probably):
Code:
def Fractal(it_rules, x) :
    return reduce(lambda seq, rules : Apply(rules, seq), it_rules, x)


With this function,
Code:
Apply( [Koch_rules]*6, "frrfrrf" )
will gives you the sequence of tokens that produce the 6th order Koch fractal. See that the it_rules are an iterable of rules, so you can easily define different rules for different recursion orders (for example, in plants, use different recursion rules for trunk/branches and leaves). I just applied 6 times the same Koch rules here.


Basically, you can see it as processing streams: a sequence of tokens enter a "box", and a different sequence exit the box. You just chained the boxes, put "frrfrrf" at one side, and get the stream of tokens at the other side. There's basically no cost in memory, because tokens are processed one at a time (each box will accept the next token only when it has finished with producing the resulting tokens of the previous accepted ones).

I find it really nice, and generator functions makes everything a breeze in Python, though it's hardly language-specific.


Then, to get a graphical output, you only need to process the tokens. A simple way in Python to process this kind of actions is to use the turtle module.

In the Koch fractal, "f" means "move forward", "r" means "turn right 60", "l" means "turn left 60".

So I define a set of drawing rules
Code:
Koch_draw = { "f" : (turtle.forward, [5]),
              "r" : (turtle.right, [60]),
              "l" : (turtle.left, [60]) }

And a function to apply them, given a sequence of tokens (Unknown tokens are ignored, but are useful to construct fractals... Also, there's two special tokens, here "a" and "p", that push and pop the position/heading of the turtle on a stack, that's especially useful with plants)
Code:
def Act(actions, it) :
    import turtle
    turtle.reset()
    turtle.speed(0)
    
    stack = []
    for elem in it :
        if elem == "a" : # push
            stack.append( (turtle.position(), turtle.heading()) )
        elif elem == "p" : # pop
            pos, hd = stack.pop()
            turtle.penup()
            turtle.setposition(pos)
            turtle.setheading(hd)
            turtle.pendown()
        elif elem in actions :
            f, vargs = actions[elem]
            f(*vargs)

Now, just use
Code:
Act(Koch_draw, Fractal( [Koch_rules]*4, "frrfrrf" ))
and you'll have a nice Koch fractal.


Some other examples:

A tree (I'll let you adapt it to have different sizes/color for lines):
Code:
Tree_rules = { "g" : "gf",
               "x" : "gfaaxupdrxupdlx" }
Tree_draw = { "f" : (turtle.forward, [5]),
              "r" : (turtle.right, [35]),
              "l" : (turtle.left, [35]) }

Act(Tree_draw, Fractal( [Tree_rules]*6, "x" ))

Harter-Heighway dragon:
Code:
Dragon_rules = { "x" : "xlyf",
                 "y" : "fxry" }
Dragon_draw = { "f" : (turtle.forward, [5]),
                "r" : (turtle.right, [90]),
                "l" : (turtle.left, [90]) }

Act(Dragon_draw, Fractal( [Dragon_rules]*10, "fx" ))

Gosper curve:
Code:
Gosper_rules = { "x" : "xlyfllyfrfxrrfxfxryfl",
                 "y" : "rfxlyfyfllyflfxrrfxry" }
Gosper_draw = { "f" : (turtle.forward, [5]),
                "r" : (turtle.right, [60]),
                "l" : (turtle.left, [60]) }
Act(Gosper_draw, Fractal( [Gosper_rules]*6, "xf" ))

Sierpinski curve:
Code:
Sierp_rules = { "x" : "yfrxfry",
                 "y" : "xflyflx" }
Sierp_draw = { "f" : (turtle.forward, [5]),
                "r" : (turtle.right, [60]),
                "l" : (turtle.left, [60]) }
Act(Sierp_draw, Fractal( [Sierp_rules]*6, "xf" ))

You'll find hundreds of rulesets on the Internet, and it's easy to design your owns (though the most interesting ones are not that easy to create)

Hope it'll help. Happy coding ;)
 

phisheep

NeoGAF's Chief Barrister
So... about Python and L-systems (I hope it's short enough that people don't mind I post the pieces of code)

...

Hope it'll help. Happy coding ;)

It certainly does Koren. That will significantly demystify the monstrosity that I have been working on. Thank you.
 

Pokemaniac

Member
Well, to automatize a spreadsheet, I don't think it's that bad. I used to have very awful experiences (like when each language has his own, incompatible, version of Excel VB), but it's now pretty stable and usable.

Well, it's not as bad as using VBA in Access, I guess (side note, if you are even considering using VBA in Access you are using the wrong tool for the job). It is not without risk though.

Generally speaking, though, using Microsoft Office macros in place of actual software tends to be an indicator of more general issues. Especially if it's happening in, for example a business. Even if it's not bad by itself, it's something to be wary of when it comes up.
 

Koren

Member
They're designed to improve functionality of a spreadsheet... It's quite good at this, and while using spreadsheets from untrustable sources is dangerous, I don't see the issue of creating you own.

If you use Excel because you think it's a replacement for an IDE to developp applications, you're doing it wrong, indeed.
 
Hey does anyone here have any experience working with VBA/Excel? I have to automate a lab outage schedule, I have completed the entire program except for one critical component :p

There's are two columns with the date and time format of yyyy-mm-dd h:mm I want to extract this value into vba and split the time and the day as a string.

How to go about this?

Check out the Excel Help Thread http://www.neogaf.com/forum/showthread.php?t=1420804
 

Pokemaniac

Member
They're designed to improve functionality of a spreadsheet... It's quite good at this, and while using spreadsheets from untrustable sources is dangerous, I don't see the issue of creating you own.

If you use Excel because you think it's a replacement for an IDE to developp applications, you're doing it wrong, indeed.

The problem is that a lot of people actually do the bolded (more often with Access than Excel, but it still happens sometimes with Excel).

I once had a summer job at a small business where they had developed an internal suite of "apps" that were just Access files on a shared drive. General impressions I've seen online suggest that this is not an isolated occurrence.
 

JesseZao

Member
Anybody been messing around with clojure.spec?

I've been wanting to start working on a functional language side project and it seems awesome.
 

Antagon

Member
Anybody been messing around with clojure.spec?

I've been wanting to start working on a functional language side project and it seems awesome.

Not personally, but I'm starting a new job next month where they use clojure for some plugins for Atlassian tools. Heard some pretty positive stuff from a guy on that project. Seems like as good a place to start as any. Not sure about the .spec part though.

A downside of using a functional language in the VM is that using a lot of immutable objects puts quite a bit of strain on the GC, but that's not something you'd have to worry about when doing a hobby project.
 

JesseZao

Member
A downside of using a functional language in the VM is that using a lot of immutable objects puts quite a bit of strain on the GC, but that's not something you'd have to worry about when doing a hobby project.

Clojure has persistent immutable data structures so it's better than pure immutable objects for that concern.
 
Not a question really but I'm learning C in college at the moment and I find myself really enjoying it.

Learning about pointers, memory addresses, how arrays work, stacks, queues, execution stack etc...

I just find it really fun, no idea why. I guess I feel like I'm getting a better grounding in understanding the fundamentals of computer science/programming.

I mean I'm a complete novice at C so I don't consider myself skilled or anything like that but I guess I really like understanding what is going on one step closer to the machine code so to speak.

I was always afraid of C/C++ because I heard horror stories about the difficulty of understanding pointers and memory management in general. To be honest it seems reasonably straight forward to me, at least for the basics of Pointers etc..

I have the Deitel & Deitel book on C which I find really good, although I've only really skimmed a few chapters so far.

I guess I'm just surprised that I enjoy C so much. :)
 

dabig2

Member
Not a question really but I'm learning C in college at the moment and I find myself really enjoying it.

Learning about pointers, memory addresses, how arrays work, stacks, queues, execution stack etc...

I just find it really fun, no idea why. I guess I feel like I'm getting a better grounding in understanding the fundamentals of computer science/programming.

I mean I'm a complete novice at C so I don't consider myself skilled or anything like that but I guess I really like understanding what is going on one step closer to the machine code so to speak.

I was always afraid of C/C++ because I heard horror stories about the difficulty of understanding pointers and memory management in general. To be honest it seems reasonably straight forward to me, at least for the basics of Pointers etc..

I have the Deitel & Deitel book on C which I find really good, although I've only really skimmed a few chapters so far.

I guess I'm just surprised that I enjoy C so much. :)

Yeah, I loved learning C myself. At the University of Illinois in freshman year when I went a billion years ago, my first programming class actually had us start learning assembly code (specifically LC-3) . Then in the latter half of the course after that foundation we moved onto C. I thought it was great, because it really makes you appreciate what's going on under the hood a lot more than if you were to start out with Python, Java, VB, etc.

I think a large reason why things like pointers and memory management are such an obstacle to new people learning C is because classes aren't taking the time to really delve into the hardware and explain things like registers, memory addresses, frame pointers, stack pointers, etc.

All that said, I haven't touched a C program since college :D But I still appreciate the knowledge.
 

Koren

Member
Hell, I feels stupid tonight...

For a LOT of time, I've written
Code:
def foo(args) :
   ...

foo = np.vectorize(foo)

And I haven't thought a single time to write instead
Code:
@np.vectorize
def foo(args) :
   ...

I fully know how decorators work, I use them a lot, and it took me years to think that vectorize can be used as a decorator. It's so stupidly obvious that it bothers me a lot.

It's only because I'm trying to think of a way I'll be able to explain vectorize to people that may want to use Python to draw data (such as 3D surfaces, implicit plots...) but do not have time/interest to fully learn the language.

Granted, there's plently of issues with using np.vectorize as a decorator, but in a lot of simple cases, it may be an easier solution...

think a large reason why things like pointers and memory management are such an obstacle to new people learning C is because classes aren't taking the time to really delve into the hardware and explain things like registers, memory addresses, frame pointers, stack pointers, etc.
I don't think you need stack pointers (and even less frame pointers) but I have trouble to understand difficulties people have with pointers (to me, the most difficult thing with C is the typing of functions, when you want to pass a function as parameter of another ^_^) but I agree, it's most probably a lack of basic explanation on how a computer memory work.

And while learning how a computer works may not be a necessity for beginners (you may begin with C, but you may as well begin with Python), I believe you need to learn about memory management and the way CPU work soon or late.

Somehow, I'd say the main difference between C and C++ is that C++ coders don't always know what's under the hood. Try to explain the need of copy constructor or move constructor to someone that only used C++ as a high level language (which is fine, but if you try to be efficient, well...)
 
The fact that this code in Java
Code:
public class Student {
private final String name;

public Student(final String name) {
 this.name = name;

is the same as
Code:
class Student(val name: String)

in Kotlin is amazing.
 

Somnid

Member
The fact that this code in Java
Code:
public class Student {
private final String name;

public Student(final String name) {
 this.name = name;

is the same as
Code:
class Student(val name: String)

in Kotlin is amazing.

I don't really care for this type of declaration in typescript because you need to look at the constructor to see what properties an object has and for data objects those can be large.

How does this work with dynamic dispatch?
 

Kansoku

Member
I don't really care for this type of declaration in typescript because you need to look at the constructor to see what properties an object has and for data objects those can be large.

How does this work with dynamic dispatch?

That's just the primary constructor. You have a bunch of options in Kotlin with regards to properties and constructors:

Normal class declaration with properties:
Code:
class Clazz {
    val a: Int = ...
    var b: String = ...
}

You then can define a primary constructor:
Code:
class Clazz (a: Int, b: String) {
   val a = a
   var b = b
   val c = b.toUpperCase()
}

But there's also a syntactic sugar for properties that you don't modify on construction:
Code:
class Clazz (val a: Int, var b: String){
   val c = b.toUpperCase()
}

If you need to do some other work aside from just assignment you put it in a init block:
Code:
class Clazz (val a: Int, var b: String){
   val c = b.toUpperCase()
   var d: String
   init {
      d = generateString(a)
   }
}

You can have multiple constructors:
Code:
class Clazz (val a: Int, var b: String){
   val c = b.toUpperCase()
   var d: String
   constructor (e: Int) {
      a = e
      d = generateString(a)
      ...
   }
}

But generally it's better to use default parameters on the primary for overloading:
Code:
class Clazz (val a: Int = 0, var b: String = "Hello World"){
   val c = b.toUpperCase()
}

There's also a bunch of other stuff like lazy init or delagating initiation. You can see more at:
https://kotlinlang.org/docs/reference/classes.html
https://kotlinlang.org/docs/reference/properties.html
 

Antagon

Member
The fact that this code in Java
Code:
public class Student {
private final String name;

public Student(final String name) {
 this.name = name;

is the same as
Code:
class Student(val name: String)

in Kotlin is amazing.

There's definitely a lot of cool things about Kotlin. I also like that almost everything is an expression.

My main problem with Kotlin as someone who has worked a lot on Java software is that while it offers full interoperability, the Java ecosystem often abuses some of the weak points in the language.

For example, not only can everything in Java be null, a lot of libraries even expect an empty constructor and then use reflection to set all the properties in the class (I'm looking at you, JPA). This means you have to use hacks in Kotlin to use this effectively. There's a compiler extension that helps with this (it generates an empty constructor for classes with specific annotations that can only be accessed using reflection), but it just doesn't feel all that great.
 
Your question could mean a couple of very different things. Can you elaborate or give a specific example of what you are trying to achieve?

I need to check if a number is between 2 points. So like is 3 between 2 and 6. I know it would look somewhat like [2,6] to set it up. I'm gathering input for these 3 numbers as well as input for whether they want to check if a number is inside or outside a range.
 
I need to check if a number is between 2 points. So like is 3 between 2 and 6. I know it would look somewhat like [2,6] to set it up. I'm gathering input for these 3 numbers as well as input for whether they want to check if a number is inside or outside a range.

Code:
bool between(int n, int min, int max) {
  return (n >= min && n <= max);
}

This is a function that returns true if the first argument is in the range determined by the second and third arguments. Is this what you mean?
 
Code:
bool between(int n, int min, int max) {
  return (n >= min && n <= max);
}

This is a function that returns true if the first argument is in the range determined by the second and third arguments. Is this what you mean?

Pretty much. I was expecting something a little more complicated but I think that works.
 
Code:
bool between(int n, int min, int max) {
  return (n >= min && n <= max);
}

This is a function that returns true if the first argument is in the range determined by the second and third arguments. Is this what you mean?

Pretty much. I was expecting something a little more complicated but I think that works. I got a little thrown off that my teacher listed an example range as [3,6].
 

Kylarean

Member
Pretty much. I was expecting something a little more complicated but I think that works. I got a little thrown off that my teacher listed an example range as [3,6].

[3,6] would mean the range is inclusive so 3, 4, 5, or 6 would be part of the range (like cpp_is_kings code does)

(3,6) would be exclusive so only 4 and 5 would be part of the range.

You could also have [3,6) and (3,6].
 
Top Bottom