TIFF TetrisRandom Homebrew: TIFF Tetris
Tetris running on 2.0 - 2.8 firmwares
Friends: Coding 'n Cracking - Nymphaea - PS3 Forum - darkforestgroup - daxhordes.org - Tgames - coldbird - gopsp.it - pspstation.org - prometheus - hgoel.info - MakeSmartTV - ps vita

Beginners C++ muddle?

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device

Re: Beginners C++ muddle?

Postby xist » Fri Jul 15, 2011 10:08 am

Many thanks,but having slept on it i'm not so sure it's a good universal solution.

Whereas

Code: Select all
if (val1 > val2)
   {
      temp = val1;
      val1 = val2;
      val2 = temp;
   }


Works in all cases, by avoiding the use of a holding variable altogether i can't deal with 0 difference problems. In this case where i'm comparing if one is larger than the other and swapping it's not a problem as there's always a difference to initiate a swap, but if i was purely wanting to swap the integers it wouldn't work if the numbers were the same (as i'd get 0's).

Therefore, it's neat, but i'm not sure that it's the best answer. Although you guys are the ones to tell me if that's a good way to analyse the code and decide on it's worth (by judging it's universal usage potential)

Also, now i'm going to feel everything i write is awful because i don't know enough to judge it as i'm not thinking of a bigger picture, more does it run or do i get errors. It's sorely tempting to just post every exercise solution i feel unsure about (so yes in this case but not in that "is a number odd or even" as i'm happy with that) as i've no peer group or teacher to quiz for help.

It's very difficult to apply Occam's razor when you're working on your own work as a total beginner who cheers (metaphorically) every time code runs fine (and does what i wanted) without me having to fix anything. I don't know enough....and it's going to take some time to get my head in the right space for breaking down problems and fixing them neatly.
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby Strangelove » Fri Jul 15, 2011 11:10 am

xist wrote:Works in all cases, by avoiding the use of a holding variable altogether i can't deal with 0 difference problems. In this case where i'm comparing if one is larger than the other and swapping it's not a problem as there's always a difference to initiate a swap, but if i was purely wanting to swap the integers it wouldn't work if the numbers were the same (as i'd get 0's).


The number switching method you used works perfect. Feel free to double-check.
However, using a temporary integer to hold the value is usually faster, nevermind why.

It can be hard to find optimal methods for doing something, but don't worry about small details, it's the big picture that counts. Like in the case of bubble sorting 100 numbers, if you're lucky the method can skip 99 loops which is a lot of work eliminated. And yes, there are more efficient methods for sorting, but to begin with it's more important that find a simple method that works for all cases, and that has a clear and simple logic to it. Breaking up a task into a smaller set of tasks is the important thing to be learned now. Tricks can wait. Bubble sort reduces the problem of sorting a lot of numbers into sorting just two numbers at a time. the rest is basically just repetition.
"If you have specific questions ... don't hesitate to ask as the more generic the question is the more philosophic the answer will be" - PSPWizard
Strangelove
 
Posts: 388
Joined: Thu Nov 25, 2010 6:32 pm

Re: Beginners C++ muddle?

Postby m0skit0 » Fri Jul 15, 2011 11:33 am

It's good trying to find most optimal algorithm, but you don't need that right now, you're still learning.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4787
Joined: Mon Sep 27, 2010 6:01 pm

Re: Beginners C++ muddle?

Postby xist » Fri Jul 15, 2011 7:48 pm

Thank you both very much for all your input. I'm sure i'll be back before long! :D

After a long day i've picked up a spiral bound notepad and i'm going to jot down every bit of code i write so i can look back at it and go over things as i learn more. Additionally, since i've found myself double checking things with Codepad to help me narrow down errors, i've installed Bloodshed's Dev-C++ which i hope is a good alternative to Visual C++ Express.

Now i'm about to start on my fourth chapter (and bearing in mind the first was just a ramble about programming in the world around us) so hopefully i'll learn some more things to quiz you all about. The main hitch i see is that i'm going away in a few weeks for a fortnight and may not have a computer to work with so i may be be doing a lot of reading and little practice.

Anyway, thanks again, it's been most helpful.
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby m0skit0 » Fri Jul 15, 2011 8:20 pm

xist wrote:i've installed Bloodshed's Dev-C++ which i hope is a good alternative to Visual C++ Express.

Eclipse is better IMHO. Anyway, you should give Windows some holidays and try programming Linux. You'll understand why it's called the "joy" of programming.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4787
Joined: Mon Sep 27, 2010 6:01 pm

Re: Beginners C++ muddle?

Postby codestation » Fri Jul 15, 2011 8:34 pm

xist wrote: i've installed Bloodshed's Dev-C++ which i hope is a good alternative to Visual C++ Express.

Oh dear $DEITY, just drop that piece of ancient software and install something else like eclipse, code::blocks or at least wxDev-C++, Dev-C++ last release was 6 years ago.
Plugin list
Working on: Python (django, sqlalchemy, PyQt, etc) + Lua
Repositories: github, bitbucket, google code
Just feel the code..
opencma/vitamtp for Archlinux
User avatar
codestation
Big Beholder
 
Posts: 1277
Joined: Wed Jan 19, 2011 3:45 pm
Location: /dev/negi

Re: Beginners C++ muddle?

Postby asgard20032 » Fri Jul 15, 2011 9:31 pm

i would recommend codeblock for a starter in C or C++ or both, for its simplicity of installation(come bundled with a compiler) and its very easy to use, not a lot of button on screen, just a clean interface. Just install, and start right away... Eclipse is a little more complicated (but when i say a little, is really a LITTLE), more thing at screen, more option, but has the great advantage to be very ... i don't know how to say it, my english is not enough good, but i will try to explain... big community, a lot of plug in to customize it, add new language... you can completely (just a way to talk) transform eclipse with plugin, adding new programming language... i think the word in english to express that is modular
Image
asgard20032
 
Posts: 259
Joined: Thu Jan 20, 2011 1:16 pm
Location: Milky Way, Solar system, Earth, North America, Canada, Québec..... In front of my computer

Re: Beginners C++ muddle?

Postby m0skit0 » Fri Jul 15, 2011 9:43 pm

Eclipse needs no extra tuning if you get the one that comes with CDT plugin and you got compiler installed. It doesn't even install. I'm talking about Linux here, of course.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4787
Joined: Mon Sep 27, 2010 6:01 pm

Re: Beginners C++ muddle?

Postby xist » Fri Jul 15, 2011 9:50 pm

Thanks for the suggestions...will definitely look into them as it seemed odd that i had Visual Express installed but was relying more upon an online compiler for help.
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Previous

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests