Quantcast

Maximum PC

It is currently Sat Aug 23, 2014 1:29 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 46 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Help with C++ assignment!
PostPosted: Wed Jul 23, 2008 10:46 am 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Ok so I'm taking this C++ class and I need help on one of the assignments:

"Create a program that will accept numbers in decimal and display them in either binary, octal or hexadecimal. Use switch to select the option (binary, octal, or hex) and make use of modulus (%) and divide operators. A sample run of the program is shown:

Enter a number in decimal:
255

Convert the number from decimal into:
0 – Binary
1 – Octal
2 – Hexadecimal
2

The number 255 (decimal) is FF in hexadecimal.

Store the base to convert to chosen by the user as an integer and use it in a modulus / division algorithm similar to what we had in Ex_6. As the result of Ex_6 was reversed, your program will need the capability to reverse the order of the digits as part of the number conversion process. To do this, create a stack class based on the “last-in, first-outâ€


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 1:13 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
If you want help with schoolwork, you have to put in some effort. Ask specific questions. Try it on your own. Ask us when you get stuck.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 9:46 am 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
ok ok I am really close to finishing it! I'm done with the binary part and the octal buttt i can only cout the hexadecimal backwards... How would I flip it? I dont know how to do this with letters only numbers...


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 8:58 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
use a lifo stack ;)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 4:46 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
hbgrg wrote:
ok ok I am really close to finishing it! I'm done with the binary part and the octal buttt i can only cout the hexadecimal backwards... How would I flip it? I dont know how to do this with letters only numbers...


Post some code so we can see that you did some work on this. Maybe then someone will give you a tip or two.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 12:07 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Ok so dont make fun of me cuz Im a reallllly nooby programmer but heres two things i need help with heres what i have for the decimal to hex-oct-binary converter:

Code:
#include <iostream>
using namespace std;
void binary(int number)
{
   int remainder;

   if(number <= 1)
   {
      cout << number;
      return;
   }

   remainder = number%2;
   binary(number >> 1);   
   cout << remainder;
}

void hexadecimal(int number)
{
   int remainder;
   while (number >= 1)
   {
      remainder = number%16;
      if (remainder > 9)
      {
         if (remainder == 10)
            cout << "A";
         if (remainder == 11)
            cout << "B";
         if (remainder == 12)
            cout << "C";
         if (remainder == 13)
            cout << "D";
         if (remainder == 14)
            cout << "E";
         if (remainder == 15)
            cout << "F";
      }
      else
      {
         cout << remainder;
      }
      number = number/16;
   }
};
void octal(int number)
{
   int remainder;
   int i;
   int ten = 10;
   remainder = number % 8;
   number = number/8;
   i = remainder;
   while (number >= 1)
   {
      remainder = number%8;
      number = number/8;
      i += remainder * ten;
      ten = ten * 10;
   }
   cout << i << "\n";
};
int main ()
{
   int number;
   cout << "Enter a number in decimal: ";
   cin >> number;
   int spec;
   cout << "Specify whether you want to convert to Binary, Octal, or Hexadecimal (1-3): ";
   cin >> spec;
   if (spec == 1)
   {
      binary (number);
      cout << "\n";
   }
   if (spec == 2)
      octal (number);
   if (spec == 3)
      hexadecimal (number);
   return 0;
}



and this should be really simple but I dont get it... For this I have to use pointers :cry: (I hate pointers) and all I have to do is make a function that orders three floats from greatest to least... I can order them from greatest to least but I cant make it all a function for somereason and i dont know where to use pointers:

Code:
#include <iostream>
using namespace std;
void swap (int, int);
int main ()
{
   float a = 3.4;
   float b = 5.7;
   float c = 6.9;
   if (c > b)
      swap (c , b);
   if (c > a)
      swap (c , a);
   if (b > a)
      swap (b , a);
   cout << a << ", " << b << ", " << c << "\n";
}
void swap (int a, int b)
{
   int  temp;
   temp = a;
   a = b;
   b = temp;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 8:07 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Can you use the STL?

Either way, look at arrays or linked lists.

My only gripe with the second code block is that you're passing floats to int parameters ;)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 9:04 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
smartcat99s wrote:
Can you use the STL?

Either way, look at arrays or linked lists.

My only gripe with the second code block is that you're passing floats to int parameters ;)


idts on the STL thing and ya that mightve been a problem lol...


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 9:09 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
oh and now the sorting function doesnt even work... What am I doing wrong? Heres the code again slightly revised...

Code:
#include <iostream>
using namespace std;
void swap (float, float);
int main ()
{
   float a = 3.4;
   float b = 5.7;
   float c = 6.9;
   if (c > b)
      swap (c , b);
   if (c > a)
      swap (c , a);
   if (b > a)
      swap (b , a);
   cout << a << ", " << b << ", " << c << "\n";
}
void swap (float a, float b)
{
   float  temp;
   temp = a;
   a = b;
   b = temp;
}


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 10:16 pm 
Team Member Top 50
Team Member Top 50

Joined: Sat Jun 25, 2005 11:04 am
Posts: 1026
hbgrg wrote:
oh and now the sorting function doesnt even work... What am I doing wrong? Heres the code again slightly revised...

Code:
#include <iostream>
using namespace std;
void swap (float, float);
int main ()
{
   float a = 3.4;
   float b = 5.7;
   float c = 6.9;
   if (c > b)
      swap (c , b);
   if (c > a)
      swap (c , a);
   if (b > a)
      swap (b , a);
   cout << a << ", " << b << ", " << c << "\n";
}
void swap (float a, float b)
{
   float  temp;
   temp = a;
   a = b;
   b = temp;
}

You're not going to like this answer, but you need to use pointers in your swap function.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 4:17 am 
Monkey Federation (Top 10)*
Monkey Federation (Top 10)*
User avatar

Joined: Sun May 22, 2005 8:28 am
Posts: 3673
Location: The Blue Nowhere
For the sake of efficiency you should use a switch/case statement instead of all the if() statements.
Code:
      if (remainder > 9)
      {
         if (remainder == 10)
            cout << "A";
         if (remainder == 11)
            cout << "B";
         if (remainder == 12)
            cout << "C";
         if (remainder == 13)
            cout << "D";
         if (remainder == 14)
            cout << "E";
         if (remainder == 15)
            cout << "F";
      }


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 6:03 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
hbgrg wrote:
oh and now the sorting function doesnt even work... What am I doing wrong? Heres the code again slightly revised...

Code:
void swap (float a, float b)
{
   float  temp;
   temp = a;
   a = b;
   b = temp;
}


As someone mentioned, the problem is in here. You aren't using pointers for your function parameters. Before I get into pointers, I'll quickly explain why your function isn't working.

When you pass parameters to a function, you are actually creating a new variable within the scope of the function. A variable's scope is its lifespan. When you create a variable in a function, the variable exists only so long as the function does. The temp float that you've created is destroyed as soon as your program exits the function. What I think you've missed is the fact that float a and float b are also destroyed when the function exits.

Of course, you have another pair of float a and float b that are created in the main section of your program. The scope of these variables is the entire program. These are called global variables. Problem number one: never use the same variable names within functions that you use for global variables. Actually ... never reuse the same variable names. If you had different names for your function variables and your global variables, you would have caught this error much more easily.

So, void swap (float a, float b) creates two new floats and assigns the values of a and [/b] to them. These two new variables take precedence over the global variables a and b. The function swaps the assigned values from a to b and vice versa. The function ends, the variables are destroyed, your global variables are no longer masked by the function variables and TA-DA! ... nothing has changed. The variables that were swapped were created in the function, swapped in the function, and then destroyed in the function .. leaving you in the same place you were before you launched the function!

There are several ways to get around this. One is by using the 'return' statement in your variable.

Code:
float swap (float swapA, float swapB) {
[i]do stuff[/i]
return newFloat
}


Of course, you need to return a pair of values in a certain order. In this case, you can either create a new data type that consists of two ordered floats and use that as your return type, or you could use the existing Pair type in the STL.

This solution is a bit of a hack, though. Either way, you have to use a data type that holds a pair of floats and this is not terribly efficient. C++ offers a more elegant solution.

Rather than passing variables, you can pass a reference to a variable ... also known as a pointer. Pointers are scary, much maligned, and much easier to deal with than anyone really wants you to know. A pointer is essentially just an object that points to another object location in memory. It can point to anything, really, provided you know how to use them.

So, rather than comparing a pair of floats and swapping their names, you'll be comparing the contents of a pair of pointers and swapping their contents instead. The pointers are unchanged, but the contents of their memory locations are swapped.

Code:
void swap (float &swapA, float &swapB) {
int temp = swapA;
swapB = swapA;
swapA = temp;
}


Questions?


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 6:12 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
On another note, I think you need to spend more time thinking about the problem at hand before you start writing code. This is not uncommon .. I'd be shocked to find a new programmer who doesn't immediately start writing code. It is a habit you need to break ASAP, though, because it drastically slows down your production of good code.

For instance, you've been tasked with writing a simple program to convert integers between various bases. You've also been given a fairly specific method of doing so (using modulus and division .. probably because you've been provided an algorithm in class that uses those operations ;) ).

Your 'instinct' is to attack immediately and start on the bits that you know how to do, right? You may quickly recognise how to convert decimal to binary and octal, but hex is trickier because of those darn A-F characters. So you might start by writing the code for the bits you know and leave the hard part for later.

The inefficiency comes into play here. The algorithm for each of these cases is the same; the difference lies in the base value. You could rewrite your octal, binary, and hex functions such that they are a single conversion function that accepts a binary number and a new base and it spits out the number in the new base.

However! This isn't necessary. It just makes code reuse much easier. Rather than having several functions with the new base hardcoded into them, you'd have one function that you could reuse anytime you need to convert a number from binary to any other base.

Just something to think about .. for now, keep on truckin' with what you're doing.


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 10:31 am 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Thank you Jip! Your post really cleared up a lot for me and I have finished the swap easily =]... I am still having trouble with the hexadecimal converter but I'll finish it!


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 12:49 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
I'm happy to have been able to help. :)


Top
  Profile  
 
 Post subject:
PostPosted: Sun Jul 27, 2008 3:36 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Code:
void hexadecimal(int number)
{
   int remainder;
   int i[50];
   int x = 0;
   for (number; number > 0; x++)
   {
      remainder = number % 16;
      number = number/16;
      i[x] = remainder;
   }
   cout << i;
};


ok so for the hexadecimal I'm trying to store each individual result I get in an integer array... And then I want to examine each individual result to see if its over 9 and then display them... is this possible?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 6:10 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
Well, integer arrays won't store A-F, so that might be a problem.

Didn't the assignment specify a stack?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 3:42 pm 
Team Member Top 500
Team Member Top 500
User avatar

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
ya I'm not sure I understand the stack class idea... could u specify for me? Thanks again jip =] I get the concept but how do I make it?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 4:21 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
hbgrg wrote:
ya I'm not sure I understand the stack class idea... could u specify for me? Thanks again jip =] I get the concept but how do I make it?


I believe that's part of the assignment that you should figure out. Treat the stack class as a limited array (refactor the array into a class where you can only touch the last item)

For storing individual digits, I'd look at chars.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:48 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
hbgrg wrote:
ya I'm not sure I understand the stack class idea... could u specify for me? Thanks again jip =] I get the concept but how do I make it?


As mentioned, that is getting dangerously into 'doing your homework' territory. I'll explain a stack and its relationship to an array without code and hopefully you'll be able to implement the idea.

You're lucky .. you've caught me in mid-procrastination. :P

A stack is a simple data structure. When you add something to a stack, you push it on top of the stack. When you take something from the stack, you pop it off the top. A stack can only be accessed in these two ways: stack.pop() returns the last item placed on the stack, and stack.push(item) pushes 'item' onto the stack.

A stack's interface (those commands used to interact with the stack) are:
Code:
bool isEmpty():  returns a boolean indicating whether the stack is empty
object pop():  returns the object most recently pushed onto the stack
push(object):  pushes the object onto the top of the stack.


You'll note I wrote 'object' in the return and parameter types. This is because I am describing a general stack. You'll want to make yours hold only char values to avoid type-casting and checking, but you'll want to think about the implications of a stack that can hold ANY data type as opposed to a stack that can only hold one data type. How would you implement them?

Back to stacks.

You are asked to implement a stack using an array. How are the two similar? How are they different?

A stack is an array with certain limitations. You can only add an item to a stack by 'growing' the array by one and adding the item to the end of the array. In other words, each new item goes at the 'end' of the array. Similarly, you can only remove items from the end of the array.

Bearing in mind that array size must be declared when it is created, how would you implement an array as a stack?


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 46 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group