• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

swap ints

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this problem im supposed to switch the values that are entered and print them out like that. For example, if 3 8 is typed then the output should be 8 3. Im specifically supposed to use void SwapValues(int* userVal1, int* userVal2)
and this is how I attempted to do it. My code doesn't print output and im not sure why or how to fix it. Can someone help me? Also before I had printf("%d %d", userVal2, userVal1 ); and it suggested to replace ls with d but im not sure what that is.

 
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hanna - I'm not sure what the problem is. You don't have to actually swap anything to change the order in the output - just print the values out in a different order.

I think the swap function should just be swapping the variables, and the output should be separate ie. something like (in psuedo-code):
The swapping bit of your function looks fine. The error message you are seeing is because the printf 'format string', the %ls, doesn't match the type of values being output. I suggest reading up on printf() - it's a function you will be using a lot. Probably lots of websites will tell you about it but here's one I would use:
https://linux.die.net/man/3/printf
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I would suggest adding the newline character \n at the end of your printf(). Sometimes you won't get anything output without one of those. Eg.
 
Saloon Keeper
Posts: 6443
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


No temporary variable needed :-)
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're missing a subtraction there Tim...
 
Tim Moores
Saloon Keeper
Posts: 6443
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think you're missing a subtraction there Tim...


Thanks, I edited the post accordingly.
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and of course the actual code will need to dereference the pointers.
 
Tim Moores
Saloon Keeper
Posts: 6443
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not if used in the main method. Since pointers are specifically required, this tongue-in-cheek approach would (hopefully obviously) not be the best approach.
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try the classic party trick:-I don't know whether it still works in C; it certainly doesn't work in Java®. As JM says, your swap() function looks correct.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and as he says, dereference the pointers (not in the swap() function with the contents operator (*):-
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:


That's what's causing the errors Hanna is seeing (I believe) - the %1s should be %d. (%s is for string; %d is for int, the type being output.)
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and at the risk of stating the obvious, C doesn't see a %s and convert an int into a string for you; you either do that yourself, or use %d to get printf() to do it for you.
 
Saloon Keeper
Posts: 12008
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I don't know whether it still works in C


It never did work for some implementations of C. That's because evaluation order wasn't clearly defined.

Party tricks are just that: party tricks. Don't use them in real code.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:. . . %1s should be %d. . . .

Not only that, but I misread it as %ls (ell not one) and thought that was a special form of %l for long ints.
 
Hanna Roberts
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how is it that I am supposed to fix it? Before I had it as %d and it gave me an error about mismatched int and int*. Im confused on how to fix that
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have
then this should compile ok:
val is an int, and valPtr is a pointer to an int.

Does that help?
 
Hanna Roberts
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes that helped thank you. But now when it outputs it doesn't do a switch, it just outputs it the way it was entered. Am I doing something wrong with the swap.

 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's just because of the order you are printing them out - you are effectively swapping them twice, in the printf() as well as in the function. When you print them out you should be doing userVal1 then userVal2.
 
Hanna Roberts
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh that's right, I forgot to switch them from when I was just printing the swap. Thanks!
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should you really read the numbers with scanf() in the swap() function? Shouldn't you do that reading in main()? What you are doing is changing the contents of those pointers before the swap.
 
John Matthews
Ranch Hand
Posts: 175
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed - I sort of implied that in my pseudo-code (2nd post in thread), but forgot to start with the input bit. Swap should just swap, although I could understand doing a printf() inside for debugging.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Not if used in the main method. Since pointers are specifically required, this tongue-in-cheek approach would (hopefully obviously) not be the best approach.



Correct.  Besides introducing problems associated with integer overflows, it is harder to write (unsurprising you didn't get it correct the first time), significantly harder to understand and the benefits of not using a temporary third variable is far outweighed by performing all that arithmetic.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic