Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 12, 2007, 06:40 PM   #1
chris200x9
macrumors 6502a
 
Join Date: Jun 2006
add up all numbers?

the output of this is just th same number, why wouldnt it be the total of all the numbers that make it up?


Code:
#include <iostream>
using namespace std;

int main () {
int num;

  cout << "input a number" << endl;
  cin >> num;
  cout << endl;
  while (num < 0 )
  {
  num1= num;
  num = num + num;
  
  num--;
  }
  cout << num;
    return 0;
}
chris200x9 is offline   0 Reply With Quote
Old Nov 12, 2007, 07:07 PM   #2
CaptainZap
macrumors regular
 
Join Date: Jan 2007
Code:
#include <iostream>
using namespace std;

int main () {
int num;

  cout << "input a number" << endl;
  cin >> num;
  cout << endl;
  while (num < 0 )
  {
  num1= num;
  num = num + num;
  
  num--;
  }
  cout << num;
    return 0;
}
I would check out your variable names in your while loop. Your num1 is doing nothing.
__________________
CaptainZap is offline   0 Reply With Quote
Old Nov 12, 2007, 07:46 PM   #3
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
If you consider this logic,

Code:
  while (num < 0 )
  {
  num1= num;
  num = num + num;
  
  num--;
  }
Let's run an example number through. Let's pick 4.

while (4 < 0) { .... }

Humm. What's wrong with that picture?

Todd
toddburch is offline   0 Reply With Quote
Old Nov 12, 2007, 08:21 PM   #4
chris200x9
Thread Starter
macrumors 6502a
 
Join Date: Jun 2006
Quote:
Originally Posted by toddburch View Post
If you consider this logic,

Code:
  while (num < 0 )
  {
  num1= num;
  num = num + num;
  
  num--;
  }
Let's run an example number through. Let's pick 4.

while (4 < 0) { .... }

Humm. What's wrong with that picture?

Todd
omfg I feel more retarded than those people in that one current event article on here

I dont get why it just outputs the negative limit of integers

Last edited by chris200x9; Nov 12, 2007 at 08:27 PM.
chris200x9 is offline   0 Reply With Quote
Old Nov 13, 2007, 02:18 AM   #5
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by chris200x9 View Post
omfg I feel more retarded than those people in that one current event article on here

I dont get why it just outputs the negative limit of integers
Probably because it loops throught that many times, decrementing num by 1 each time until it reaches the limit. At that point I'm not sure what happens. I suppose it either wraps around, or zeroes out all the bits thereby making num >= 0 and terminating the loop.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Nov 13, 2007, 07:07 AM   #6
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
I don't know what you did to change the logic since my last post, but if you just flipped the relational operator to this:
Code:
while (num > 0 )
  {
  num1= num;
  num = num + num;
  
  num--;
  }
then that might not be getting you to where you want to go either.

Let's look at the logic again. We'll pick 4 again.
Code:
while (4 > 0 )
  {
  //num1= num;   not sure what this line is supposed to do???
  num = 4 + 4 ;   // double num 
  
  num--;      // subtract one - it's now 7 
  }
Every iteration will double the number and then subtract 1. When num, a 4 byte integer, reaches one more than X'7FFFFFFF' (2gb), it will go negative, since the sign bit will be on, and the WHILE loop will quit.

What is your loop supposed to be doing?

Todd
toddburch is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Anyway to add a shortcut to a numbers file MrMister111 iOS 7 4 Feb 6, 2014 11:48 AM
Numbers - Add 'em. Math Game SteveZz iPhone and iPod touch Apps 0 Jan 27, 2013 02:21 PM
Backup Numbers don't add up??? Crispykickz iPhone Tips, Help and Troubleshooting 1 Sep 26, 2012 05:30 PM
iPhone: how add 2 phone numbers to imessage Meric Jailbreaks and iOS Hacks 1 Sep 2, 2012 10:07 AM

Forum Jump

All times are GMT -5. The time now is 02:13 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC