GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jan-2006, 15:41
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Fresh Small C++ Program :P


Hello all, I just started taking C++ and its a bit different than my usual C programs I wrote

The simple question im working says to write a program that inputs a five digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each.

For example, lets say I entered 93032

it would look like this

9 3 0 3 2



My code is only this so far:

CPP / C++ / C Code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()

{
	int number = 0;

	cout << "Enter a five-digit number:  ";
	cin >> number;

	cout << number << endl;

	return 0;

}


its a bit strange why we were assigned this question, considering we never went over anything on it =p
  #2  
Old 25-Jan-2006, 17:50
weeb0 weeb0 is offline
New Member
 
Join Date: Jan 2006
Posts: 17
weeb0 is on a distinguished road

Re: Fresh Small C++ Program :P


I would begin using mask.

I mean you should do:

I assume int => 16 bits

CPP / C++ / C Code:
int mask = 0x000f
int individual[4];
for (int x=0;x<4;x++)
{
  individual[x] = number & mask;
  mask = mask << 4; 
}

each of the number is in the array.

Hope this help!
Last edited by LuciWiz : 26-Jan-2006 at 02:27. Reason: Please insert your C code between [c] & [/c] tags
  #3  
Old 25-Jan-2006, 22:10
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Fresh Small C++ Program :P


Quote:
Originally Posted by Sabin044
Hello all, I just started taking C++ and its a bit different than my usual C programs I wrote

The simple question im working says to write a program that inputs a five digit integer, separates the integer into its individual digits and prints the digits separated from one another by three spaces each.

For example, lets say I entered 93032

it would look like this

Code:
9 3 0 3 2


its a bit strange why we were assigned this question, considering we never went over anything on it =p

Probably to prove you understand the rudiments of math, like to get the last digit from an integer use the modulus operator:
CPP / C++ / C Code:
int value = 5397;
digit = value % 10;
digit now has the value 7. Lather

Then because you're done with that digit, you can remove it from the value:
CPP / C++ / C Code:
value = value / 10;
value now has 539. Rinse

Repeat

It also lets you use your knowledge of arrays, for loops, and cout. Each (except cout) you already know from C
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #4  
Old 26-Jan-2006, 09:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Fresh Small C++ Program :P


Quote:
Originally Posted by weeb0
I would begin using mask.

I mean you should do:

I assume int => 16 bits

1. Bad assumption. Although I know that some compilers have 16-bit ints, every single compiler that I have has 32-bit ints. The size of an int is not specified in any C or C++ standards document.

2. Nothing in the problem statement suggests or requires 16-bit ints. Why should the solution depend on 16-bit ints? (As a matter of fact, the example that the Original Poster used --- 93032 --- requires 17 bits in its binary representation.)

Quote:
Originally Posted by weeb0
CPP / C++ / C Code:
int mask = 0x000f
int individual[4];
for (int x=0;x<4;x++)
{
  individual[x] = number & mask;
  mask = mask << 4; 
}

each of the number is in the array.

Exactly what is in the array? That's a question that can be answered by actually running your code.

It can also be answered by looking at the code: Each time through the loop it leaves an individual hex digit of the number in place with the other hex digits zeroed out. Huh??? How does this help???

I pasted your code into the Original Poster's program (after supplying the missing semicolon on your mask declaration) and ran it with an input that can be represented as a 16-bit integer and printed out the elements of the array:
CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()

{
  int individual[4];
  int mask = 0x000f;
  int number = 0;

  cout << "Enter a five-digit number:  ";
  cin >> number;

  cout << "Number = " << number << endl << endl;

  for (int x = 0; x < 4; x++) {
    individual[x] = number & mask;
    mask = mask << 4; 
    cout << "individual[" << x << "] = " << individual[x] << endl;
  }

  return 0;
}

Here's the result:

Code:
Enter a five-digit number: 12345 Number = 12345 individual[0] = 9 individual[1] = 48 individual[2] = 0 individual[3] = 12288

Quote:
Originally Posted by weeb0

Hope this help!

I am sure that everyone appreciates all attempts to help, but if you really want to help, I respectfully suggest that you test your code and see what it gets and see if it could help the Original Poster solve the stated problem.

Regards,

Dave
Last edited by davekw7x : 26-Jan-2006 at 10:23.
  #5  
Old 26-Jan-2006, 11:31
weeb0 weeb0 is offline
New Member
 
Join Date: Jan 2006
Posts: 17
weeb0 is on a distinguished road
Thumbs down

Re: Fresh Small C++ Program :P


Dave:

You are the kind of people I don't like to work with. Are you able to help ? Are you able to give advice ? if you are only able to bash, go away. I only want to help, not give a full solution. This guy is doing homework, so he have to learn and try and solve the problems!

I think I gave a good hint. When I said I assume int -> 16 bits, was for the example purpose only. If your compiler use 32 bits, use the loop for 32 bits. Then, I agree I forgot to shift right, but I think we are all here to help and give advice.
  #6  
Old 26-Jan-2006, 12:37
Sabin044 Sabin044 is offline
Junior Member
 
Join Date: Oct 2005
Posts: 68
Sabin044 is on a distinguished road

Re: Fresh Small C++ Program :P


WaltP, I realized you do have to your integer division and modulus (%) from a friend today. The only problem is, my math skills are not so bright, I do not know the forumla for this either, but I think I may have a clue how.

I appreciate weeb0, waltp, and dave for your feedback. weeb0 I understood what you were saying too, and btw this is not homework this is simply a practice problem, but I do not know why it involves the math part hence this is my 1st program in C++.

I'm working on this now still, give me a minute =D

Sabin

P.S; Weebo, i dont think dave was trying to insult ya or anything or be negative towards ya for any reason =]
  #7  
Old 26-Jan-2006, 15:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Fresh Small C++ Program :P


Quote:
Originally Posted by weeb0
Dave:

Are you able to help ? Are you able to give advice ?
.
.
.
I only want to help, not give a full solution. This guy is doing homework, so he have to learn and try and solve the problems!

I think I gave a good hint.

You are correct in saying that my post didn't contribute any direct help to the Original Poster. Normally I don't jump into a thread unless I feel that I can offer something towards solving a problem. In this case, I think that Walt had given a pretty good hint for one approach to the problem.

My post was not intended to be a personal insult, but I meant to point out that the code that you posted wouldn't help to solve the problem of extracting decimal digits from an integer value. A lot of people follow the threads in this forum looking for general hints and techniques for solving various programming problems. In general, I can't see how masking and shifting is useful in obtaining decimal digits. (Binary, octal or hexadecimal digits, yes, but those weren't any part of the problem here.)

I respectfully suggest that people always test their code and look at the output and then decide whether it will actually help other people.

This forum is a wonderful place, and I enjoy seeing other people's points of view. I learn something every single time that I visit here. You are entitled to your opinion. I am entitled to mine.

Regards,

Dave
Last edited by davekw7x : 26-Jan-2006 at 16:18.
  #8  
Old 26-Jan-2006, 17:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Fresh Small C++ Program :P


Quote:
Originally Posted by weeb0
Dave:

You are the kind of people I don't like to work with. Are you able to help ? Are you able to give advice ? if you are only able to bash, go away. I only want to help, not give a full solution. This guy is doing homework, so he have to learn and try and solve the problems!
Your idea to 'point' the way instead of give the poster the answer is absolutely correct. He does have to learn to solve the problem himself. And that's what we try to do.

And Dave actually is one of the best people on this board to give help. Simply look back on many of his posts. I think you did him an injustice. He was simply stating that the values the original poster was using seem to be decimal, not hex. Your solution is very good for hex.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #9  
Old 26-Jan-2006, 18:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Fresh Small C++ Program :P


Quote:
Originally Posted by WaltP to weeb0
Your idea to 'point' the way instead of give the poster the answer is absolutely correct.
I agree; I couldn't have said it better than weeb0 did.


Quote:
Originally Posted by WaltP to weeb0 about davekw7x

And ...I think you did him an injustice.

To be fair: I think I could have phrased my criticism better. I didn't mean for it to be personal.

One of the things that I am constantly working on is: "How can I give more help to more people?" If any individual takes my statements as a personal put-down, then others may see it that way, too. This means that I haven't helped everyone that I could have, since the entire interchange will probably be discounted. (The other thing that happens is that the original purpose of the thread gets lost in the noise. I hope that we can get back on track now.)

When I try to help, sometimes I nail it; sometimes --- not so much.

Regards,

Dave

"The mighty words of the proud are paid in full by mighty blows of fate,
and at long last these blows will teach us wisdom."
--- Sophocles
Antigone


"Well, I can tell you that the part about the blows is true.
I'm still waiting for the 'wisdom' thing."
--- davekw7x
gidforums
  #10  
Old 26-Jan-2006, 20:09
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Fresh Small C++ Program :P


Just hover through the little green buttons(Right to his name) of Dave, and you'll find what Dave is worth of!

__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 13:52
Small help with this program please NigelZen C Programming Language 3 29-Aug-2005 00:17
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Having a small problem with this program Krc784 C++ Forum 1 04-Nov-2004 23:25

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.