Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

05-07-2015 , 10:46 PM
c++ recursion question:

say I have an array of integers that I want the product of. What I came up with worked, but it seemed really unintuitive and ugly.

Code:
int  recurProd(const int myArray[], int size)
{
	if (size == 1) return myArray[size - 1];
	return myArray[size - 1] * recurProd(myArray, size - 1);
}
any thoughts or critiques? Feels like I should be able to remove a lot of the minus ones, or at least two. Obviously the recursive call needs one.
Programming homework and newbie help thread Quote
05-07-2015 , 11:29 PM
I understand it better starting at the beginning:

Code:
//pseudocode template to help get it straight in my head
//prod x:xs = x * prod(xs)

int prod(int *xs, int size)
{
    if (size == 1){
        return xs[0];
    }
    return xs[0] * prod(xs + 1, size - 1);
}
edit:

To explain myself better, I want to take the first item of a list and multiply it by prod() of the rest of the list. I think to myself, "how do I get the rest of the list in C?" By incrementing the pointer.

Regarding the reply below mine, I don't know C++ and am answering in C because you code looks like C to me.

Last edited by Allen C; 05-07-2015 at 11:40 PM. Reason: more stuff
Programming homework and newbie help thread Quote
05-07-2015 , 11:34 PM
Quote:
Originally Posted by Anais
c++ recursion question:

say I have an array of integers that I want the product of. What I came up with worked, but it seemed really unintuitive and ugly.

Code:
int  recurProd(const int myArray[], int size)
{
	if (size == 1) return myArray[size - 1];
	return myArray[size - 1] * recurProd(myArray, size - 1);
}
any thoughts or critiques? Feels like I should be able to remove a lot of the minus ones, or at least two. Obviously the recursive call needs one.
Since we're talking C++, pretty sure this will work:

int prod = std::accumulate(myArrary, myArray + size, 1, std::multiplies<int>());
Programming homework and newbie help thread Quote
05-07-2015 , 11:44 PM
Quote:
Originally Posted by Anais
c++ recursion question:

say I have an array of integers that I want the product of. What I came up with worked, but it seemed really unintuitive and ugly.

Code:
int  recurProd(const int myArray[], int size)
{
if (size == 1) return myArray[size - 1];
return myArray[size - 1] * recurProd(myArray, size - 1);
}
any thoughts or critiques? Feels like I should be able to remove a lot of the minus ones, or at least two. Obviously the recursive call needs one.
Can you use a for/in or do/while loop instead of if and use the size of your array as the counter instead of a specified size?
Programming homework and newbie help thread Quote
05-08-2015 , 01:29 AM
Quote:
Originally Posted by Anais
my least favorite bits have definitely been the front end work. Can do it if it's necessary, but would rather be doing something else.
You're not the only one.

I don't have any real opinion on what languages to learn first, second, third, etc, but I enjoy C. In some ways, I wish I had started with C instead of Python.
Programming homework and newbie help thread Quote
05-08-2015 , 03:47 AM
Quote:
Originally Posted by Anais
c++ recursion question:

say I have an array of integers that I want the product of. What I came up with worked, but it seemed really unintuitive and ugly.

Code:
int  recurProd(const int myArray[], int size)
{
	int index = size-1;
	if (index == 0) return myArray[index];
	return myArray[index] * recurProd(myArray, size - 1);
}
any thoughts or critiques? Feels like I should be able to remove a lot of the minus ones, or at least two. Obviously the recursive call needs one.
See modifications. Feel free to use index as parameter in the recursion call, too. I just think leaving it makes it more clear what you're trying to do.

Alternatively, if you're after making it more brief, this would do the same:
Code:
int  recurProd(const int myArray[], int size)
{
	if (--size == 0) return myArray[size];
	return myArray[size] * recurProd(myArray, size);
}
Programming homework and newbie help thread Quote
05-08-2015 , 04:09 AM
Quote:
Originally Posted by kazana
Alternatively, if you're after making it more brief, this would do the same:
Code:
int  recurProd(const int myArray[], int size)
{
	if (--size == 0) return myArray[size];
	return myArray[size] * recurProd(myArray, size);
}
Actually, with a bit of thought a tad shorter
Code:
int  recurProd(const int myArray[], int size)
{
	return myArray[size-1] * ((size == 1) ? 1 : recurProd(myArray, size-1));
}
Programming homework and newbie help thread Quote
05-08-2015 , 04:38 AM
Quote:
Originally Posted by Anais
As one who is leaving c++ for Java, there's absolutely levels of control that I will miss. But I think I'd ultimately rather start my career out with a language that's a bit more OOP/beginner friendly and worry about learning the advanced stuff once I've got more experience.

I don't need to know how to tear down an engine and rebuild a transmission to be able to get a driver's license, after all.
Have you considered C#? I seem to be in similar boat as you and C# >>>> JAVA in coding enjoyment to me.
Programming homework and newbie help thread Quote
05-08-2015 , 08:01 AM
Quote:
Originally Posted by just_grindin
Can you use a for/in or do/while loop instead of if and use the size of your array as the counter instead of a specified size?
specifically supposed to avoid iteration for this

Quote:
Originally Posted by adios
Since we're talking C++, pretty sure this will work:

int prod = std::accumulate(myArrary, myArray + size, 1, std::multiplies<int>());
I think you've long since forgotten the point of school

Quote:
Originally Posted by Allen C
I understand it better starting at the beginning:

Code:
//pseudocode template to help get it straight in my head
//prod x:xs = x * prod(xs)

int prod(int *xs, int size)
{
    if (size == 1){
        return xs[0];
    }
    return xs[0] * prod(xs + 1, size - 1);
}
edit:

To explain myself better, I want to take the first item of a list and multiply it by prod() of the rest of the list. I think to myself, "how do I get the rest of the list in C?" By incrementing the pointer.

Regarding the reply below mine, I don't know C++ and am answering in C because you code looks like C to me.
I mean, the code is all pretty similar. But yeah, I think this is pretty much what I should have done.

Quote:
Originally Posted by kazana
See modifications. Feel free to use index as parameter in the recursion call, too. I just think leaving it makes it more clear what you're trying to do.

Alternatively, if you're after making it more brief, this would do the same:
Code:
int  recurProd(const int myArray[], int size)
{
	if (--size == 0) return myArray[size];
	return myArray[size] * recurProd(myArray, size);
}
this is pretty brilliant, i think. Decrementing in the if statement

Quote:
Originally Posted by kazana
Actually, with a bit of thought a tad shorter
Code:
int  recurProd(const int myArray[], int size)
{
	return myArray[size-1] * ((size == 1) ? 1 : recurProd(myArray, size-1));
}
along with the whole "std::cout", the "? x : somethingElse" is one of the things we've touched on like all of one time so I never got the hang of it. But I think I see what's going on here.

Quote:
Originally Posted by Absurdas
Have you considered C#? I seem to be in similar boat as you and C# >>>> JAVA in coding enjoyment to me.
I really wanted to go c#, but they only rarely offer c# classes at my school. Since I'm pretty committed to getting my second cat skin, err, sheep skin in a timely manner, waiting another semester or two isn't really an option.

Thank you to everyone for the awesome replies btw.
Programming homework and newbie help thread Quote
05-08-2015 , 05:30 PM
Quote:
Originally Posted by Anais
along with the whole "std::cout", the "? x : somethingElse" is one of the things we've touched on like all of one time so I never got the hang of it. But I think I see what's going on here.
If you aren't comfortable with that, yet, that's okay. You'll get around to using them one way or another.

Ternary operators are very useful for making very short, basic if/else decisions during variable assignment.
The only tricky part being that you do need to take operator precedence into consideration if there are other parts/computations during assignment - which is why I often put the entire ternary operator component in parenthesises.
Programming homework and newbie help thread Quote
05-10-2015 , 09:40 AM
Quote:
Originally Posted by kazana
If you aren't comfortable with that, yet, that's okay. You'll get around to using them one way or another.

Ternary operators are very useful for making very short, basic if/else decisions during variable assignment.
The only tricky part being that you do need to take operator precedence into consideration if there are other parts/computations during assignment - which is why I often put the entire ternary operator component in parenthesises.
I am not a big fan of ternary operators for the simple reason that it doesn't buy you much, certainly not an increase in speed and/or an effecient use of memory (who cares about insignificant allocations of automatic variables in non optimized compilations). Is debugging problems more difficult? Probably. Does it make code more readable? Doubtful in my view. Readability as well as company coding standards are the only legitimate reasons that exist in my view. I don't believe it contributes that much to readability, others do.

A long winded way of saying that using ternary operators is an issue of style and really nothing else.
Programming homework and newbie help thread Quote
05-10-2015 , 09:54 AM
Quote:
Originally Posted by Anais
specifically supposed to avoid iteration for this



I think you've long since forgotten the point of school
Yes I know, often it seems to be an unwavering process of "reinventing the wheel" which I find to be sad. I get that your priority needs to be you complying with what the teacher wants to get the piece of paper you need. I think you will enjoy working as a developer.
Programming homework and newbie help thread Quote
05-10-2015 , 05:14 PM
Quote:
Originally Posted by adios
I am not a big fan of ternary operators for the simple reason that it doesn't buy you much, certainly not an increase in speed and/or an effecient use of memory (who cares about insignificant allocations of automatic variables in non optimized compilations). Is debugging problems more difficult? Probably. Does it make code more readable? Doubtful in my view. Readability as well as company coding standards are the only legitimate reasons that exist in my view. I don't believe it contributes that much to readability, others do.

A long winded way of saying that using ternary operators is an issue of style and really nothing else.
Interesting. I put readability above all else unless the impact on performance is noticeable.

I personally find x = (b)? y : z; much easier on the eyes than pulling apart that type of assignment over several lines.
The example above, embedding it into a computation, is probably the furthest I'd take it and still find it easily readable.

Also, I have yet to run into an issue debugging statements with ternary operators. The two IDEs I have worked with extensively, Eclipse and VS, have not given me any problems stepping through what's happening there.
Programming homework and newbie help thread Quote
05-15-2015 , 11:54 AM
The problem with ternaries is they're often misused. In javascript you can do silly things like:

Code:
var foo = function () {..};
var bar = function () {..};
var baz = (function that returns a boolean);
and then later

Code:
c() ? a() : b();
which is obviously not at all how you should use them. You'll also see a lot of nested ternaries which make it very difficult to follow at least for me.

I still use them though for simple assignments below 40ish characters I'd say. They are very readable for me as JS doesn't have a lot of other things that would cause someone to confuse them with ternaries so to speak.
Programming homework and newbie help thread Quote
05-15-2015 , 06:04 PM
doing a c++ project with polymorphism and a bit perplexed

i've got three account types, base, derived, and one derived from the derived

wrote first two classes and decided to test, and thank goodness! For whatever reason, when I run the function to print everything in the object, a function that is const mind you, one of the string variables is somehow getting a renamed.

Spoiler:

base class
Code:
string account::getType() const
{
	return acctType;
}

const account & account::print(ostream & outs) const
{
	outs << "Account Number: " << getNum() << " Account Name: " << getName()
		<< " Account Type: " << getType() << endl;
	return *this;
}
derived class
Code:
int prefAccount::getGameTime() const
{
	return gameTime;
}

const prefAccount & prefAccount::print(ostream & outs) const
{
	account::print(outs);
	outs << "Game Time: " << getGameTime() << " days" << endl;
	return *this;
}
main is just:
Code:
	prefAccount a(1152, "Tetris", 30);
	cout << a.getType() << endl;
	a.print(cout);
	cout << a.getType() << endl;
	return;
for the sake of testing

output is:
Quote:
Preferred Account
Account Number: 1152 Account Name: Tetris Account Type: Account
Game Time: 30 days
Preferred Account
Press any key to continue . . .


so it sees the right type initially, but somehow gets changed during the print statement and then reverts. Which I doubt is true, even though it looks like it.

I'm guessing there's some sort of copy going on when I call the base print function, but I'm not sure how to get around that, as the type is hard coded per class. (Yes, I could get around this, I think, but I don't like how that would work.)

edit:

to clarify, i assume when "account:: print(outs);" is called, a new, temporary item of type account is created and it has the improper type due to it being hard-coded. Although, the copy constructor for account doesn't hard-code the type, so now I actually don't know. =*(

2nd edit:

found the culprit that's causing the issues in the base class:

Spoiler:
Code:
account::account(long num, string name)
{
	setNum(num).setName(name).setType("Account");
}


So this has to be getting called when I do "account:: print(outs);". Just not sure of a good way around this short of not hard-coding the type

Last edited by Low Key; 05-15-2015 at 06:25 PM. Reason: edit, space added to prevent smily
Programming homework and newbie help thread Quote
05-15-2015 , 06:40 PM
changed a few things, and it works, but i'm not entirely sure I like it

Programming homework and newbie help thread Quote
05-16-2015 , 12:22 AM
Could you post the class spec (.h file) for yor base class?
Programming homework and newbie help thread Quote
05-16-2015 , 08:38 AM
I think I got it sorted out. The main problem afaik was that I was trying to directly access polymorphic functions instead of calling them at runtime. Because as soon as I made an array of pointers to base pointers and started calling functions that way, everything worked like gangbusters.

Redesigned for a bit more flexibility and got rid of some redundant code too, so yay that!
Programming homework and newbie help thread Quote
05-16-2015 , 06:34 PM
Quote:
Originally Posted by Low Key
I think I got it sorted out. The main problem afaik was that I was trying to directly access polymorphic functions instead of calling them at runtime. Because as soon as I made an array of pointers to base pointers and started calling functions that way, everything worked like gangbusters.

Redesigned for a bit more flexibility and got rid of some redundant code too, so yay that!
Excellent!
Programming homework and newbie help thread Quote
05-22-2015 , 03:58 AM
Hey guys, very basic MySQL problem


I've custom-installed just the mySQL 5.6 server following instructions from here, and am trying to connect to the database using instructions here, however I cannot find any of the tools specified in steps 6,7 & 8. I have searched around including in

program data/MySQL/MySQL Server 5.6/

but there is no sign of tools here.

Please help, no answers from stackoverflow
Programming homework and newbie help thread Quote
05-23-2015 , 03:12 AM
Quote:
Originally Posted by POW
Hey guys, very basic MySQL problem


I've custom-installed just the mySQL 5.6 server following instructions from here, and am trying to connect to the database using instructions here, however I cannot find any of the tools specified in steps 6,7 & 8. I have searched around including in

program data/MySQL/MySQL Server 5.6/

but there is no sign of tools here.

Please help, no answers from stackoverflow
This won't be much help. I've actually muddled through installing a mySQL server. I got a book though. IIRC it was very close to what this link indicates:

Installing LAMP Linux, Apache, mySQL, and PHP

I was trying to interface some of the Boost Libraries to mySQL and that was a major PIA mainly because of the state of Boost (some libraries are just not kept up to date). Anyway I was able to run the tools indicated in the article I linked to with out too much difficulty.
Programming homework and newbie help thread Quote
05-26-2015 , 01:39 AM
Quote:
Originally Posted by POW
Hey guys, very basic MySQL problem


I've custom-installed just the mySQL 5.6 server following instructions from here, and am trying to connect to the database using instructions here, however I cannot find any of the tools specified in steps 6,7 & 8. I have searched around including in

program data/MySQL/MySQL Server 5.6/

but there is no sign of tools here.

Please help, no answers from stackoverflow
What's in the bin/ directory? According to this page, there should definitely be one in the Windows install.

If your installation doesn't have a bin directory that contains all the expected executables, you may have excluded them during your Custom install. But I suspect that's not really possible.

This is my bin directory on mac (45 items). I imagine Windows might be slightly different but probably not much:
Code:
$ ls /usr/local/mysql/bin/ | wc -l
      45
$ ls /usr/local/mysql/bin/
innochecksum			mysql_client_test_embedded	mysql_setpermission		mysqlbug			mysqlimport
msql2mysql			mysql_config			mysql_tzinfo_to_sql		mysqlcheck			mysqlshow
my_print_defaults		mysql_config_editor		mysql_upgrade			mysqld				mysqlslap
myisam_ftdump			mysql_convert_table_format	mysql_waitpid			mysqld-debug			mysqltest
myisamchk			mysql_embedded			mysql_zap			mysqld_multi			mysqltest_embedded
myisamlog			mysql_find_rows			mysqlaccess			mysqld_safe			perror
myisampack			mysql_fix_extensions		mysqlaccess.conf		mysqldump			replace
mysql				mysql_plugin			mysqladmin			mysqldumpslow			resolve_stack_dump
mysql_client_test		mysql_secure_installation	mysqlbinlog			mysqlhotcopy			resolveip
Programming homework and newbie help thread Quote
05-27-2015 , 06:19 AM
How do i install pokereval in C? I installed it with cygwin using ./configure and make commands. Now what do i do? I need to use the #includes in the C program, what do i write? And then if it works suceffully where can i get the functions available?
Programming homework and newbie help thread Quote
05-27-2015 , 06:33 AM
AOk so i was messing around with cygwin trying to install the library(not really knowing what i was doing) and now when i use devc++ it won't print me anything. I try
Code:
printf("Hello World\n");
All it prints is a 'D'. I tried reinstalling and the problem remains, what can i do to recover this?
Programming homework and newbie help thread Quote
05-27-2015 , 07:27 AM
This is really annoying. I don't know what i did. All i did was mess around with cygwin i did the ./configure and make commands and later i did some with gcc dont remember how it was.

Now DevC++ is just plain dumb, it doesnt do what i tell him.
Programming homework and newbie help thread Quote

      
m