7 Replies - 322 Views - Last Post: 01 February 2012 - 08:00 PM Rate Topic: -----

#1 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 03:41 PM

Hello, first time posting. Im writing a program in C that calculates if you make a higher profit by taking a penny that is doubled each day for a month or $1 Million.
Now the penny a day results to $5,368,709.12. So i know I need to make a variable that will start at 1 and then double itself 30 times.
Im not entirely certain as to where to go from here. Heres what I have so far.

#include <stdio.h>

int main (void)
{
    int x = 30;
    for(x = 1; x = x*2;)
    {
        printf("$%d\n", x);
    }
    printf("\n");
    return 0;
};



This code does what I intend it to do in principle. However, it needs some spring cleaning. After some trial and error of getting it to double 1 30 times (Well, 32 times in my case, which is unintentional) Ive succeeded, in a way.
Im hoping someone will pitch me some pointers as to how to display the money amounts correctly. (Dollars and cents)

Ive attached a .jpg of the compiled results as to help those who are interested in understanding what Im talking about.

This is part of my code for a homework assignment, and I know you guys wont do it for me. I feel like I've put forth a reasonable amount of effort though to get a helping hand.

Thanks,
blu

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: A for loop starting at 1, doubling itself 30 times.

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 03:51 PM

You should probably learn your for loops and basic logic, see reference: Control Structures
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,109
  • Joined: 31-December 10

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 03:54 PM

The reason the last result in your output is negative is because the number you're trying to store is larger than an integer can hold. Try declaring your variable to be a long and you could also make it unsigned which will allow even more numbers. So something like this:
unsigned long x;

If that isn't enough space, you could try a long long.

It makes a big difference if a number is signed/unsigned. With signed numbers, the CPU needs to keep track of the negative sign, but with unsigned numbers it doesn't. To keep this simple, on my machine, a char is 1 byte(8 bits). If I declared a signed char, then the CPU needs to use one of those 8 bits to hold the negative sign. This decreases how many numbers can be held in the char. If I declared it unsigned, I actually increase how many numbers that can be held in the char. Let me know if you're still confused on this point.
Was This Post Helpful? 1
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 04:00 PM

First what is happening in your screenshot is what is called an overflow.

Standard variable types like int have a limited amount of memory available to them and as a result have a limited range. When after a calculation a variable exceeds that range, it will reset to the beginning. In your case, x will go from the largest possible value an integer can hold to the smallest possible value an integer can hold. The exact limits are platform specific.

You can first try a type instead of int that is larger and thus has a larger range. In this case you could try long or unsigned long (only positive values). If this is c++ you also try long long and unsigned long long. If this is still not large enough, post again.

Second your for loop is not quite right, the syntax of a for loop is for(initialisation; condition; modification). So initialisation is executed before the loop starts. The condition is checked every time the loop loops including the first time and the modification is executed after every iteration of the loop. So to do something 30 times you would do:

for (int i = 0; i < 30; i++) {
    ...
}



I will now go from 0 to and including 29, while the code within the loop is ran 30 times.

This post has been edited by Karel-Lodewijk: 01 February 2012 - 04:01 PM

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,772
  • Joined: 25-December 09

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 04:02 PM

First your for loop is incorrect:
for(x = 1; x = x*2;)


A for loop consists of three parts, the initialization, condition, and increase. You are only using the initialization and condition, which is not necessarily incorrect (it will compile), is incorrect in your program. How many days should this loop execute? It looks like 30 days to me, so your loop should look more like:
    int pennies = 1;
    for(int days = 1; days < 31; ++days)
    {
       printf("%d\n", pennies);
       pennies *= 2;  // Short cut for pennies = pennies * 2;
    }



Jim

This post has been edited by jimblumberg: 01 February 2012 - 04:03 PM

Was This Post Helpful? 3
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3846
  • View blog
  • Posts: 11,772
  • Joined: 25-December 09

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 04:11 PM

Also an int should hold this result without a problem. Switching to long or long long will not really help much. The loop is actually an endless loop, using assignment in the condition clause, the overflow is actually causing the program to stop.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 04:36 PM

View Postjimblumberg, on 01 February 2012 - 11:11 PM, said:

Also an int should hold this result without a problem. Switching to long or long long will not really help much. The loop is actually an endless loop, using assignment in the condition clause, the overflow is actually causing the program to stop.

Jim


Without a problem is a little bit of an over statement. For up to 31 days a 4 byte int will still hold the value, if your month is 32 days a 4 byte int will overflow if I'm not mistaken.

1 doubled every 32 days is 2^31 and a 4 byte signed 2s-complement int (your garden variety int) has a maximum positive value of (2^31)-1, so close :). Luckily there is no such travesty as a 32 day month.

EDIT: Originally started at 2 penny's, so you would get into trouble after 31 days, that +votes might have been premature :)

This post has been edited by Karel-Lodewijk: 01 February 2012 - 04:57 PM

Was This Post Helpful? 2
  • +
  • -

#8 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: A for loop starting at 1, doubling itself 30 times.

Posted 01 February 2012 - 08:00 PM

Some light has been shed on the fact that I did not understand the syntax of a for loop as well as the long(longlong) int's. The funny thing is I wasnt aware of it. Big thanks to you all for showing me how to apply this to my code. Reputation +'s for the lot of you.

blu
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1