Random Homebrew: Pen's Enhanced Picture Viewer
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

Beginners C++ muddle?

Postby xist » Thu Jul 14, 2011 10:24 am

Ok....i'm painfully new to C++, and learning whenever i get some spare time, so imagine i'm in week one to make things simple. It's painfully slow due to having to work out kinks on my own so if i have stylistic faults it's because i've not learnt something from my book yet.

And I'm using Microsoft Visual C++ Express....

Now, I've been tasked in the exercise i'm doing to ask for three integers and then read them out in size order.

I'm not supposed to know multi-purpose if/else statements yet (as in if and else's with multiple statements thus requiring { } bracketing, but i'm using them anyway because i thought they'd reduce the size of my code. (if there's a way to solve this without them and without it being larger/messier please tell me.)

So i'm supposed to know if statements, single line option if/else statements and assignments.

This is what i wrote (it compiles ok and works fine in windows, giving good results....i think). The comments were there for me to rationalise it when i got errors (0's popping out).

How bad is it, what could i have done better and what bad habits do i already have? (remember i've not been introduced to much yet in my book and i'm trying not to run before i can walk).

Code: Select all
int main()
{
   
   cout <<"Please enter three whole number values: \n";
   int val1;
   int val2;
   int val3;
   
   cin >> val1 >> val2 >> val3;
   
   int starter = 0;
   int middle = 0;
   int final = 0;
   int unknown_small = 0;
   
   if (val1 > val2) // if 1 is bigger than 2
   {
      middle = val1; // middle is assigned value of val1
      starter = val2; // and starter is assigned value of val2
   }
   else
   {
      middle = val2; // middle is assigned value of val2
      starter = val1; // and starter is assigned value of val1
   }               // Both starter and middle now non-zero value
   
   if (val3 > middle) // compare value assigned to middle to third number
   {   
      final = val3; // if third number is bigger final is assigned value of val3
      unknown_small = middle; //need to eliminate zero value
   }   
   else
   {
      unknown_small = val3;  // new integer unknown_small is given value of val3 if val3 not biggest
      final = middle; // and since we have the biggest number it's assigned final
   }

   if (unknown_small > starter) // third value(unknown_small) smaller than middle - smaller than first value?
        middle = unknown_small; //if not middle value becomes unknown_small
   else
   {
      middle = starter;
      starter = unknown_small;
   }
   
   cout <<starter <<" , " <<middle <<" , " <<final <<'\n';
}
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby m0skit0 » Thu Jul 14, 2011 11:23 am

Actually, that's not really C++, but rather C plus cin and cout streamings. I've always been against C++ for learning programming because you don't actually learn C++, but C with some C++ elements, making it a confusing mix IMHO.

As a great rule of thumb to remember, always use brackets {} even when not required. That will save you future headaches.

xist wrote:I'm using Microsoft Visual C++ Express....

Argh... :|

So the question, resuming, is receiving 3 numbers and ordering them, am I right? We definitely don't know how much of C++ features you're allowed to use. Are you allowed to use arrays?
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Beginners C++ muddle?

Postby xist » Thu Jul 14, 2011 11:28 am

I don't even know what an array is so i'm guessing no.

Edit- and on checking what an array is, no, definitely not if i understand what i'm reading. I have definitely not been introduced to that. I haven't even been introduced to curly bracked if/else statements so even my use of multiple actions in them is kind of cheating (and yes i feel bad for doing it).

Edit (2) - i'm very worried i'm making this far too complex for the task. The next question says do it with three words instead which i accomplished just by switching int for string. However, what worries me is that the question following that just asks me to write a program to determine if a value is odd or even, which i achieved just by -

Code: Select all
int main()
{
   
   cout <<"Please enter a whole number: ";
   int unknown;
   cin >> unknown;
   
   if (unknown%2 == 1)
      cout <<"The value " <<unknown <<" is an odd number\n";
   else
      cout <<"The value " <<unknown <<" is an even number\n";
   }


Which says to me i'm being stupid about ordering 3 integers.
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby Strangelove » Thu Jul 14, 2011 3:29 pm

@xist: are you allowed to use loops yet? doesn't matter much since there's only 3 numbers though.

one thing that hits be when looking at the code is how many extra variables you use to solve the problem. imagine if you had 100 numbers to sort, that method would use a lot of extra memory.

the logic you use to sort wouldn't work particularly well sorting 100 numbers either. to me it looks like you tried to solve the whole problem at once, without breaking up the task into smaller operations. (this is programming in a nutshell)

one method to sort a list of numbers that is often used for its simplicity is "bubble sort". it basically goes like:

1. compare the first and the next number, if the second is smaller then switch the numbers.
2. compare the second and the next number, if the third is smaller then switch them.

3. repeat this for every number in the list, then start over, beginning with the first number.
4. if you've gone through the entire list without switching a single number you're done.

for 3 numbers you can sort them using this method with as little as 4 comparisons. and since there's only 3 numbers there's no need to keep track of whether switches have occurred, it's just as fast to just do them.

example:

#0: 7 5 3 (original)
#1: 5 7 3 (compare 1&2 switch)
#2: 5 3 7 (compare 2&3 switch)
#3: 3 5 7 (compare 1&2 switch)
#4: 3 5 7 (compare 2&3 no switch)

example #2:

#0: 2 8 4 (original)
#1: 2 8 4 (compare 1&2 no switch)
#2: 2 4 8 (compare 2&3 switch)
#3: 2 4 8 (compare 1&2 no switch)
#4: 2 4 8 (compare 2&3 no switch)
"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 xist » Thu Jul 14, 2011 3:36 pm

I totally understand where you're coming from, but alas i've not learnt the switch function either (or loops). The most complex things i've used are the if/else options and as far as examples go the book has used "while" once. I'm guessing that these aren't helpful.

To give you an idea how basic i am the terms that have been listed as learning points to end this chapter are:

assignment
cin
concatenation
conversion
declaration
decrement
definition
increment
initialization
name
narrowing
object
operation
operator
type
type safety
value
variable
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby m0skit0 » Thu Jul 14, 2011 3:45 pm

There's no switch function (well, at least with the meaning exposed by Strangelove). He just gave you an algorithm, it's up to you to code it as you feel it should be.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Re: Beginners C++ muddle?

Postby Strangelove » Thu Jul 14, 2011 4:56 pm

int fox, rabbit1, rabbit2;

// flipping two numbers (rabbit1 and rabbit2).
// no rabbits were harmed.
fox=rabbit1;
rabbit1=rabbit2;
rabbit2=fox;

---
think of two rabbits running from a fox. if they both tried to switch sides at the same time they would crash (and be eaten by the hungry fox).
so one has to get behind the other, then the second rabbit can trade places, and then the first one can jump ahead to where his companion was standing.

lame example perhaps, but these are pedantic rabbits. :P
"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 xist » Thu Jul 14, 2011 7:42 pm

OK, i follow that. So if the first number is smaller than the second re-assign the values, and then compare the second number (new or unaltered) with the third number and if it's lower reassign the values.

Does that mean that there does exist a loop function (which i haven't learnt yet) which repeats these processes ? So that in this case i'd just have to run extra comparisons which later, once i know a bit more i could eliminate to slim my code down?

I'll see if i can trim this down and repost what i end up with.

Time passes.....
Edit - Right ....i don't see how i can switch the values neatly and eloquently...i tried something like

Code: Select all
if (val1 > val2) val1 = val2 && val2 = val1


but that gave me an lvalue error.

So i'm left with this which seems clumsy....but i think works.

Edit (2) - re-read Fox and Rabbits. Seems that in the example i missed that Fox was a temporary holding integer. However, that example uses three lines and i've used three lines. Is this solution good as i'm not using another variable?

Code: Select all
   
int main()
{       
       cout <<"Please enter three whole number values: \n";
       int val1;
       int val2;
       int val3;
       
       cin >> val1 >> val2 >> val3;
                   
       if (val1 > val2)
   {
      val1 = val1 + val2;
      val2 = val1 - val2;
      val1 = val1 - val2;
   }
   
   if (val2 > val3)
   {
      val2 = val2 + val3;
      val3 = val2 - val3;
      val2 = val2 - val3;
   }       

   if (val1 > val2)
   {
      val1 = val1 + val2;
      val2 = val1 - val2;
      val1 = val1 - val2;
   }             
       
       cout <<val1 <<" , " <<val2 <<" , " <<val3 <<'\n';
}



Also, once you've given me pointers on the above ( :D ) if i had to do this for 100 numbers is there some way of automating it to run cyclically or do i have to use three lines for every contiguous integer comparison?
xist
 
Posts: 42
Joined: Thu May 19, 2011 9:19 am

Re: Beginners C++ muddle?

Postby Strangelove » Thu Jul 14, 2011 9:35 pm

Edit (2) - re-read Fox and Rabbits. Seems that in the example i missed that Fox was a temporary holding integer. However, that example uses three lines and i've used three lines. Is this solution good as i'm not using another variable?

Code: Select all
   
int main()
{       
       cout <<"Please enter three whole number values: \n";
       int val1;
       int val2;
       int val3;
       
       cin >> val1 >> val2 >> val3;
                   
       if (val1 > val2)
   {
      val1 = val1 + val2;
      val2 = val1 - val2;
      val1 = val1 - val2;
   }
   
   if (val2 > val3)
   {
      val2 = val2 + val3;
      val3 = val2 - val3;
      val2 = val2 - val3;
   }       

   if (val1 > val2)
   {
      val1 = val1 + val2;
      val2 = val1 - val2;
      val1 = val1 - val2;
   }             
       
       cout <<val1 <<" , " <<val2 <<" , " <<val3 <<'\n';
}



Nice. That's a clever trick. Using a difference encoding as temporary storage. :) Did you come up with that yourself?

Also it seems tree comparisons will do too, I was a little quick to assume 4 was needed. :oops:
Usually with bubble sort you do n(n-1) comparisons for a list of n elements as a worst case, but we used a slightly altered version above.

xist wrote:Does that mean that there does exist a loop function (which i haven't learnt yet) which repeats these processes ? So that in this case i'd just have to run extra comparisons which later, once i know a bit more i could eliminate to slim my code down?

Also, once you've given me pointers on the above ( :D ) if i had to do this for 100 numbers is there some way of automating it to run cyclically or do i have to use three lines for every contiguous integer comparison?


the code looks fine. i don't think you could have solved that task any better than you did there.

you'll probably get to learn loops very soon. you have for instance one loop you typically use when you repeat something a given number of times (for loop), and another type of loop you use when you don't know beforehand how many times you will be repeating a task (while loop). also tasks you keep doing can be put into a function, so instead of writing three lines to swap two numbers you can basically just tell the compiler to run the number swapping function.

in case of the bubble sort, in the best case you have an already sorted list and so you wouldn't want to run through it 100 times if it could be helped. In this case you use a while loop, run through the list one time and see that no switching took place and then exit the loop since you're done.
"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 7:11 am

Nice xist :mrgreen:

Just as a side note on what Strangelove already said: when using loops, you usually use arrays instead of separate variables since they're easier to use data structures for such purposes. I'm pretty sure you'll be looking on loops and arrays pretty soon ;)
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
User avatar
m0skit0
Guru
 
Posts: 4783
Joined: Mon Sep 27, 2010 6:01 pm

Next

Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest