check variable contains a value that it's comparing with the contents in the list. I understand the description but not sure on how to do it. So we use nested for loops to do it?
Oooooo O'Reilly has a 50% off sale today to celebrate being against DRM. Quick, order your books!
EDIT: Addendum to the heads up! Effective Modern C++ is a book I keep hearing a lot of good things about and it's part of the sale.
Oooooo O'Reilly has a 50% off sale today to celebrate being against DRM. Quick, order your books!
EDIT: Addendum to the heads up! Effective Modern C++ is a book I keep hearing a lot of good things about and it's part of the sale.
Is this book geared towards devs that are already really familiar with the language up till '11/'14?
Is this book geared towards devs that are already really familiar with the language up till '11/'14?
Table of contents says "yes". At a glance, everything seems to be about the new stuff.
And besides, Meyers is a very incisive guy and the language is very fiddly. I think pretty much everyone who works with the language, no matter how experienced, can take away something from his classic Effective C++ books. You've already read them, I presume?
Can I PM someone my code, because I'm trying to print out a numbers that's in a list. But doesn't work. Similar problem with checking if a value exist. There's a while loop in ostream operator function and in check_list that checks if the list is not empty. It skips the code within a while loop, and goes to the return statement.
int recursiveSum(int num1, int num2)
{
if (num2 == 0)
return num1;
else
{
if (num2 < 0)
{
num2++;
num1--;
recursiveSum(num1,num2);
}
else
{
num2--;
num1++;
recursiveSum(num1, num2);
}
}
}
I think people are generally more receptive to commenting on code that is posted here, rather than answering questions through PMs. If you're worried that it's because of a homework assignment, then only paste in a few lines relevant to your question, and remove any extraneous stuff, and change names to foo and bar or something.
In any case, in this instance it sounds to me like your list is empty. You should inspect the code that you think is adding items to the list. You should also try using a debugger.
Sadly I think 99% of homework questions could be answered with "use a debugger", it amazes me that they don't teach basic debugging skills in the first couple weeks of a programming course. Not really your fault that you haven't been taught how to use a debugger, but really, any problem you could possibly encounter in your first year or so of taking programming courses can be solved very easily by "set breakpoint, run program, step, print value, step, print value, etc until something isn't what you expect"
However, adding a cout statement recursive call statements gives you extremely erroneous results. Can anybody explain?
Something like this: http://stackoverflow.com/questions/...function-without-returning-a-value-without-pr
It will ultimately depend on your architecture and compiler, but the code, as it is, is basically relying on undefined behavior to do its intended job.
I debugged the program, and your right. The list is empty though I called insertion function before attempting to print the list. I'm not sure why though.
Your code here:But that example shows that it works, not that you get the right results. Somehow, if you add 1 + 4, that function is managing to return 5, not 0.
else
{
num2--;
num1++;
recursiveSum(num1, num2);
}
else
{
num2--;
num1++;
return recursiveSum(num1, num2);
}
But how does it know to return that and not simply return zero? Also, why does it give a wrong result of you add a cout statement after that recursive call, but not other things like assignments or declarations?Your code here:
Code:else { num2--; num1++; recursiveSum(num1, num2); }
Actually operates like this when it's compiled and ran:
Which is how that function should be written and exhibits the correct behavior.Code:else { num2--; num1++; return recursiveSum(num1, num2); }
But how does it know to return that and not simply return zero? Also, why does it give a wrong result of you add a cout statement after that recursive call, but not other things like assignments or declarations?
int foo() {
return 7; // (1)
}
int main() {
int f = foo(); // (2)
cout << f;
}
mov eax, 7;
ret;
call <address of foo()>; // Call the function foo()
mov <address of f>, eax; // Mov the return value of foo() into the variable f
// Push arguments for the call to operator <<
push eax; // Normally this would load it from a register, but compiler is smart enough to know that even though the argument is the variable "f", the register 'eax" contains the same value at this particualr moment.
push cout;
call < address of basic_iostream::operator<<() > // call basic_ostream::operator <<(cout, f); // The first argument to a member function is the "this" pointer, which in this case is the instance 'cout'
if (num2 == 0)
return num1;
// Semi pseudocode to avoid unimportant details of assembly language
if (num2 == 0) {
__asm mov eax, num1; // Move the value of num1 into EAX register
__asm ret; // Exit the function.
}
int recursiveSum(int num1, int num2)
{
if (num2 == 0)
return num1;
else
{
if (num2 < 0)
{
num2++;
num1--;
recursiveSum(num1,num2);
}
else
{
num2--;
num1++;
recursiveSum(num1, num2);
}
}
#if defined(_MSC_VER) // Microsoft Visual C++
__asm mov eax, 1234567
#else // Everything else
__asm__("movl $1234567, %eax")
#endif
}
Can you post the line of code that does the insertion, followed by the line(s) that print?
void fooLists::fooInsert(int pos, const itemlists& new_item)throw(OutOfRangeException, listexception){
int new_size = getSize() + 1;
if((pos < 1) || (pos > new_size)){
throw OutOfRangeException("You can't insert an item when you're out of range!");
} else {
try{
Node *new_pointer = new Node;
size = new_size;
new_pointer->item = new_item;
if(pos == 1 ){ //Attach a new node at the beginning of a list.
new_pointer->next = head; //Insert a new node to the list.
head = new_pointer;
} else {
Node *previous = find(pos-1);
new_pointer->next = previous->next;
previous->next = new_pointer;
}
}
catch(bad_alloc e){
throw listexception("Failed to insert a new node!");
}
}
}
ostream &operator<<(ostream &output, const fooLists &list){
fooLists list1;
list1.head = list.head;
while(list1.head != NULL){
if(list1.head->next == NULL){
output << list1.head->item;
list1.head = list1.head->next;
} else{
output << list1.head->item << ", ";
list1.head = list1.head->next;
}
}
return output;
}
Code:snip
I see. Wow, that is a very in depth explanation.On x86 architectures, the return value is typically stored in the EAX register. That's just by convention, and how most compilers generate code. When you write this:
Code:int foo() { return 7; // (1) } int main() { int f = foo(); // (2) cout << f; }
The statement at (1) is executing these assembly instructions:
Code:mov eax, 7; ret;
And the statement at (2) is executing these instructions:
Code:call <address of foo()>; // Call the function foo() mov <address of f>, eax; // Mov the return value of foo() into the variable f // Push arguments for the call to operator << push eax; // Normally this would load it from a register, but compiler is smart enough to know that even though the argument is the variable "f", the register 'eax" contains the same value at this particualr moment. push cout; call < address of basic_iostream::operator<<() > // call basic_ostream::operator <<(cout, f); // The first argument to a member function is the "this" pointer, which in this case is the instance 'cout'
The important thing to note is that the main() and foo() both "agree" that the return value will be in eax, so they just know to look there.
Look at just this part fo your function:
Code:if (num2 == 0) return num1;
What this is really doing is saying this:
When you call recursiveSum, eventually it will boil down to those assembly instructions, at which point EAX contains the correct return value. All it does from that point is unwind out of recursive calls. No other instructions ever get executed, so nothing else has the potential to modify EAX and eventually you unwind all the way out back up to main. EAX still has the same value in it as it did when it mov'ed num1 into it, and so main just thinks that's what the return value was.Code:// Semi pseudocode to avoid unimportant details of assembly language if (num2 == 0) { __asm mov eax, num1; // Move the value of num1 into EAX register __asm ret; // Exit the function. }
Edit: And it gives a wrong value when you add a cout() call because cout returns a value, which modifies eax. Try this:
Code:int recursiveSum(int num1, int num2) { if (num2 == 0) return num1; else { if (num2 < 0) { num2++; num1--; recursiveSum(num1,num2); } else { num2--; num1++; recursiveSum(num1, num2); } } #if defined(_MSC_VER) // Microsoft Visual C++ __asm mov eax, 1234567 #else // Everything else __asm__("movl $1234567, %eax") #endif }
and see what happens
I might be overlooking something, but both of these functions look fine to me. Well, style wise they could be improved a bit, but behavior wise they look like they should do the right thing. What does the line look like that calls insert and operator<<?
It looks like the line runs normally, but for operator<< it skips the while statement. The compiler thinks it's empty. I'm not sure what to do at this point. I've debugged, and know the reason why it's not printing the list. But I can't retrace the problem since we're using pointers.
Can you post the lines of code that call insert and operator <<?
int fooLists::getSizeLists(){
while(head != NULL){
size++;
head = head->next;
}
return size;
}
I've got help, and he found my problem. The getSizeList function was the reason why the list prints nothing.
Code:int fooLists::getSizeLists(){ while(head != NULL){ size++; head = head->next; } return size; }
There's actually 2 problems with this function. Did you find both?
There's actually 2 problems with this function. Did you find both?
what's going to happen to size over multiple calls?I think I know one, it's the head = head->next statement
I'm guessing the second one is size?
what's going to happen to size over multiple calls?
When I called the function to print, it doubles the size;
We had one lecture in Intro for debugging and one lecture in OOP for errors, that's it I think. Actually most of the people new to programming I saw use debugging quite a bit, I never bothered.I've never gone through a single class that explains error checking or debugging processes. Blows me away that they don't have that as an entry level course. It should at least be integrated into a curriculum.
We had one lecture in Intro for debugging and one lecture in OOP for errors, that's it I think. Actually most of the people new to programming I saw use debugging quite a bit, I never bothered.
I'd recommend starting here, and getting a good look at the official docs, before looking at frameworks -- you won't know what's what otherwise: https://developers.facebook.com/docs/graph-apiWorking on a JSF-based web app for a university project. In addition to standard username/password authentication (which we already implemented), users should be able to sign in using Facebook or Twitter. It should be easy to extend it with additional providers.
Could someone recommend me a framework for this?
I've had a look at Spring Social, Scribe and SocialAuth.
Spring Social is of course the most powerful of those three, but jesus, it's incredibly complex, at least from what I've seen so far.
Scribe looks pretty simple to use, but apparently it only returns an access token after signing. SS, on the other hand, provides a uniform way (independent of the provider) to obtain personal data like name, email, username and so on.
SocialAuth appears to be kinda in the middle between the other two, in terms of complexity and features. But there's a suspicious lack of comments/impressions about it, and not many stars/watchers on GitHub either.
I'd really appreciate some help, because the way it looks, I'm probably gonna be the one responsible for implementing this.
I'd recommend starting here, and getting a good look at the official docs, before looking at frameworks -- you won't know what's what otherwise: https://developers.facebook.com/docs/graph-api
(1) You'll need an app https://developers.facebook.com/apps/
(2) Facebook users sign into the app, the access tokens are associated with it
(3) If you need info for a user, you can just request it as the Graph API dictates (provided it falls into the granted auth scope)
That is the rudimentary connection between your app and a Facebook user. The framework doesn't usually explain how this works. Read the API docs, at least the surface stuff. That'll help a LOT.
Thanks, but I know how OAuth works. I'm just not sure which framework is best suited for our application. As I mentioned in my previous post, it should be easy to add new providers, but at the very least we need to support Facebook and Twitter.
However, there's something I haven't figured out about OAuth: How exactly are the credentials of OAuth users usually persisted? Like, someone signs in to our app using a Twitter account. We obtain an access token and can use it to get their credentials (id, name, email) and create a User object for that. What now? To my knowledge, we should persist the user's id, not the access token. That way, if Twitter ever returns a different token when the user signs in again, we are still able to find their local account (the item from the User table). Is that how it's done?
public static int sumOfListFor(List<int> numbers)
{
int sum = 0;
foreach(int number in numbers)
{
sum += number;
}
return sum;
public static int sumOfListWhile(List<int> numbers)
{
int sum = 0;
int counter = 0;
while(counter < numbers.Count)
{
sum += numbers[counter];
counter++;
}
return sum;
}
public static int sumOfListRecursive(List<int> numbers)
{
if (numbers.Count == 0)
{
return 0;
}
else
{
int sum = 0;
sum += numbers[0];
numbers.RemoveAt(0);
return sum + sumOfListRecursive(numbers);
}
}
I saw an article "Five programming problems every Software Engineer should be able to solve in less than 1 hour". I wrote the following three functions for question 1: "Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion.".
How could I write these better, or optimize them better?
Code:public static int sumOfListFor(List<int> numbers) { int sum = 0; foreach(int number in numbers) { sum += number; } return sum; public static int sumOfListWhile(List<int> numbers) { int sum = 0; int counter = 0; while(counter < numbers.Count) { sum += numbers[counter]; counter++; } return sum; } public static int sumOfListRecursive(List<int> numbers) { if (numbers.Count == 0) { return 0; } else { int sum = 0; sum += numbers[0]; numbers.RemoveAt(0); return sum + sumOfListRecursive(numbers); } }
I need to decide on a thesis topic. >_>Anyone else have trouble coming up with personal projects?
Since I'm a student I can get Xamarin for free so I can create Android apps in C#. I think that'd be a cool summer project but I just can't come up with the ideas.
Write a program that outputs all possibilities to put + or - or nothing between the numbers 1, 2, ..., 9 (in this order) such that the result is always 100. For example: 1 + 2 + 34 5 + 67 8 + 9 = 100.
The guy writing comes off as an ass, but the problems are worth the read.
If you bother to read this blog at all (or any other blog about software development), you are probably good enough to solve these and 5 more problems within the hour. The people that think are above all these "nonsense" are usually the ones that can't code crap.
Welp, that was fun. I don't really like his tone and there's a lot of other sites for practicing these kind of arbitrary problems.reddit said:Well this asshole should stop calling himself a software engineer, since his solution for #4 is WRONG!
Welp, that was fun. I don't really like his tone and there's a lot of other sites for practicing these kind of arbitrary problems.