There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Completely Lost in C++...


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
24-Jan-2005, 05:37 PM #1
Completely Lost in C++...
Hello everyone.

I just begun a C++ course in the beginning of this month. I was doing quite well at it until just now. I just wihs that the professor woudl clarify things a little more than he does.

I have an assignment to write a program (using no functions other than main(), the stdlib.h header file, for() and while() loops, switch() statements, and if() statements) that will take in a number from the user and return its binary and hexadecimal values. I am not sure how to go about this at all. I am just a bit lost because I am not sure of what algorithm to use in the creation of this program.

How could I go about this with the stipulations I was given?
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
billgrande's Avatar
Junior Member with 5 posts.
 
Join Date: Dec 2004
Experience: Advanced
24-Jan-2005, 07:40 PM #2
Make sure you think about the program, try thinking it out using psuedocode or something first. How would you do this in real life?

Here is something to get you started:

n n / 2 n % 2
35 17 1
17 8 1
8 4 0
4 2 0
2 1 0
1 0 1

35 = 100011
billgrande's Avatar
Junior Member with 5 posts.
 
Join Date: Dec 2004
Experience: Advanced
24-Jan-2005, 07:44 PM #3
Make sure you think about the program, try thinking it out using psuedocode or something first. How would you do this in real life?

Here is something to get you started:

This shows converting n to binary
n
35
17
8
4
2
1

n/2
17
8
4
2
1
0

n%2
1
1
0
0
0
1


35 = 100011
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
24-Jan-2005, 08:31 PM #4
I understand how to get the binary values...it's just implementing it into C++ code. I know that things take me awhile to comprehend because I try to think in a human-based state of mind when trying to program rather than in the mindset of a computer...I have no problem with conversion myself...I just need to try to teach myself what algorithms are necessary in the C++ code and how to form them. That is what I seem to get confused with.

Thanks for your input. I do see how that kind of implementation could be placed into source code, but when I run through it on paper I notice that you have to read the binary number from bottom to top. Will C++ also output the digits as such (right to left rather than left to right)?

Thanks for your help.
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
24-Jan-2005, 10:13 PM #5
Here si the code that I came up with for this project...but I am not sure that it is accruate because I was told that you have to read binary from right to left...but this prints from left to right...but it works at least...We also have to put spaces between each byte...I hope that I have this even somewhat close to what it has to be.

/*Robert Cole
CIS 226-01
lab12.cpp*/

#include <iostream.h>
#include <stdlib.h>


unsigned int main()
{
int number, power = 2, powerctr = 1, x;

cout << "Enter a positive number: ";
cin >> number;
cout << endl;

while(number < 0)
{
cout << "Your number is not a positive number." << endl;
cout << "Enter a positive number: ";
cin >> number;
cout << endl;
}//End while(number)

for(number; powerctr <= 32; powerctr++)
{
if(number % 2 == 0)
cout << "0";
else
cout << "1";
;

x = powerctr;
if(x == 8 || x == 16 || x == 24)
cout << " ";

number = number / 2;
}
cout << endl;



return 0;
}
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
25-Jan-2005, 02:36 PM #6
To print out the hex equivalent of an int using only <cstdio> (as you said you could only use stdio, loops etc.)

Code:
#include <cstdio>

int main() {
    const int i = 215;
    printf("%X", i);
}
and here's a ->binary way that I happened to find that I converted to only use <cstdio>. I didn't thoroughly test it though.

You shouldn't use this method unless you can explain why it works and this is only shown as an example to give you an idea, but if your instructor really meant that the only header file you can include is cstdio, you'll need to get rid of cin, cout and iostream and use strictly stuff defined in cstdio.

You'll have to figure out how to add the spaces.

Code:
#include <cstdio>

int main() {
     printf("Enter an integer: ");
     int ch;
     scanf("%i",&ch);
     for(int i = 0x80; i; i = i >> 1) {
          printf("%c",( (ch & i) ? '1' : '0') );
     }
}
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run

Last edited by Shadow2531 : 25-Jan-2005 03:45 PM.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
25-Jan-2005, 03:55 PM #7
Here's a little code cleanup for you.

Code:
#include <iostream>

using namespace std;

int main() {
    cout << "Enter a positive number: ";
    int n;
    cin >> n;
    cout << endl;

    while (n < 0) {
        cout << "Your number is not a positive number." << endl;
        cout << "Enter a positive number: ";
        cin >> n;
        cout << endl;
    }
    
    for (int pc = 1, x ; pc < 33; ++pc) {
        if (n % 2 == 0) {
            cout << "0";
        } else {
            cout << "1";
        }
        x = pc;
        if (x == 8 || x == 16 || x == 24) {
            cout << " ";
        }
        n /= 2;
    }
    
    cout << endl;
}
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
25-Jan-2005, 04:21 PM #8
Thanks for the help on that. I talked to the professor today, and he said that the binary output must read from right to left...and that the way of division is not what he wants. He wants us to use multiplication to get the output...

After running the program the way I coded it, the output would look like this:

**************************************************
Enter a positive number: 4

00100000 00000000 00000000 0000
**************************************************

The professor wants the output displayed this way:

**************************************************
Enter a positive number: 4

00000000 00000000 00000000 00000100
**************************************************
I am just not sure how to do that...but the code that all of you have posted has helped me with the way I was doing it, anyhow. Thanks for the advice so far..
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
25-Jan-2005, 05:44 PM #9
Did you tell him that division IS multiplication?

Anyway the way I have done conversions to binary was by using bitset and stringstream, which works great, but you won't be allowed to do that.

I'll give you a hint though. The ways I've seen it done, count backwards in the loop.

Kind of like, for (int i = 33; i > 0 ; --i) type of thing.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
25-Jan-2005, 06:47 PM #10
To flip it around, you could do this:

Code:
#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    int b[32];
    for (size_t i = 0; i < 32; ++i) {
       b[31 - i] =  n % 2 ;
       n /= 2;
    }
    for (size_t i = 0; i < 32; ++i ) {
        cout << b[i];
    }
}
and with spaces (this method actually holds the spaces in the array. You could make it easier by just printing out the spaces in the last loop)

Code:
#include <iostream>

using namespace std;

int main() {
    int n;
    cin >> n;
    char b[35];
    for (size_t i = 0; i < 35; ++i) {
        if ( 34 - i == 8 || 34 - i == 17 || 34 - i == 26) {
            b[34 - i] = ' ';
        } else {
            if (n % 2 != 0) {
                b[34 - i] = '1';
            } else {
                b[34 - i] = '0' ;
            }
            n /= 2;
        }
    }
    for (size_t i = 0; i < 35; ++i ) {
        cout << b[i];
    }
}
I'm not sure what multiplication method he wants you to use though.

Last edited by Shadow2531 : 25-Jan-2005 09:06 PM.
IMM's Avatar
IMM IMM is offline
Distinguished Member with 3,157 posts.
 
Join Date: Feb 2002
27-Jan-2005, 08:58 AM #11
Did you mean stdlib.h or stdio.h ?

Here's some stuff with a multiply involved - but I'm not sure I know what is wanted
Code:
/* for an unsigned int called number which is 32 bits wide*/
#include <stdio.h>

void main()
{
  unsigned int number = 33;  // this is the one you get as input from the console
  unsigned int i, j, bitsel;
  char binstr[32];
  
  for (i=0;i<32;i++) binstr[i] = '0';
  for (j=0, bitsel=1; j<32; j++){
    if (number & bitsel)
      binstr[31-j] = '1';
    bitsel = bitsel * 2;     // at least it's a multiply
    // bitsel = bitsel << 1; // I like this better
  }
  for (i=0; i < 4; i++) {
    for (j=0; j<8; j++){
      printf("%c", binstr[j + i*8]);
    }
    printf(" ");
  }
  // print it as a hex number
  printf("\n%#.8x",number);
}
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
27-Jan-2005, 01:22 PM #12
Thanks IMM. It's a good chance that's what the instructor wants.

Here's the same thing using less c-like methods, but the credit still goes to you.

Code:
#include <cstdio>

int main() {
    char binstr[32];
    for (size_t i = 0; i < 32 ; ++i ) binstr[i] = '0';

    const size_t number = 33;
    for (size_t i = 0, bitsel = 1; i < 32; ++i ) {
        if (number & bitsel) {
            binstr[ 31 - i ] = '1';
        }
        bitsel *= 2; 
    }
    for (size_t i = 0; i < 4; ++i) {
        for (size_t j = 0; j < 8; ++j ) {
            printf("%c", binstr[ j + i * 8 ] );
        }
        printf(" ");
    }
    printf("\n%#.8x",number);
}
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:33 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.