Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,704 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,491 people online right now. Registration is fast and FREE... Join Now!




stack numbers

 
Reply to this topicStart new topic

stack numbers, of course homework

duygugvn
post 21 Nov, 2005 - 03:49 AM
Post #1


New D.I.C Head

*
Joined: 31 Oct, 2005
Posts: 36


My Contributions


I chose stack numbers between0 and 255. there are rulesto check if it is stack or not.these are...
1-the bits must start with 1 and end with 0
2-number of 1s must equal numbner of 0s
3-start from left to right to sum the values of these bits,count 1 for 1s and count -1 for 0s....
I COULD NOT apply

CODE

#include <stdio.h>
#define BITS 7

int main() {

int binNum[7];
int number;

int i;
 

printf("enter a decimal number: ");
scanf( "%d", &number);
if(number>=0 && number<=255)
{

for ( i = 0; i <= BITS; i++ ) {
binNum[i] = number % 2;
number = number / 2;
}
for ( i = BITS; i>= 0; i-- ) {
printf("%d", binNum[i]);
}
}
return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 21 Nov, 2005 - 08:13 AM
Post #2


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,125



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


Could you clarify the problem statement? I'm having trouble seeing what the program requires.
User is online!Profile CardPM

Go to the top of the page

duygugvn
post 21 Nov, 2005 - 08:30 AM
Post #3


New D.I.C Head

*
Joined: 31 Oct, 2005
Posts: 36


My Contributions


I have to determine the stack number between 0 and 255.
stack numbers has three rules as I mentioned before post.but I only conver decimal to binary form and determine the boundery 0-255
I could not apply rules for stack numbers.
User is offlineProfile CardPM

Go to the top of the page

Nova Dragoon
post 21 Nov, 2005 - 09:13 AM
Post #4


The Innocent Shall Suffer, Big Time

Group Icon
Joined: 16 Aug, 2001
Posts: 6,125



Thanked 4 times

Dream Kudos: 515

Expert In: Python, Linux

My Contributions


Ok from what I could make out of the stack number rules:

You have converted the number into a binary number in an array.

For rule #1: check if the array starts with a 1
if( array[0]==1 )

Then use a for loop and a sum variable, iterate through the array, add 1 to sum if "1" subtract 1 from sum if "0"
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 09:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month