Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Why is my variable returning a large negative number when dividing?

gohanhango

New Coder
I'm tackling the credit problem from cs50 and I'm baffled by the for loop on one section because I'm dividing a long integer 4003600000000014 by 100 and it returns a large negative number -1685133312.

Here is the actual code:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long int number;
    long int temp;
    do
    {
        number = get_long("Number: ");
    } while (number < 0);

    temp = number;
    int counter = 1;
    for (int i = 10; i <= number; i = 10)
    {
        number /= i;
        counter += 1;
    }
    printf("%i\n", counter);

    int product = 0;
    int divisor = 100;
    int modulo = 0;
    //printf("%li\n", (temp % 100) / 10);
    for (int i = 0; i < counter / 2; i++)
    {
        modulo = temp / divisor;
        divisor *= 100;
        product += (2 * (modulo % 10));
    }
    printf("%i\n", product);
}
I'm tackling the credit problem from cs50 and I'm baffled by the for loop on one section because I'm dividing a long integer 4003600000000014 by 100 and it returns a large negative number -1685133312.

Here is the actual code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
long int number;
long int temp;
do
{
number = get_long("Number: ");
} while (number < 0);

temp = number;
int counter = 1;
for (int i = 10; i <= number; i = 10)
{
number /= i;
counter += 1;
}
printf("%i\n", counter);

int product = 0;
int divisor = 100;
int modulo = 0;
//printf("%li\n", (temp % 100) / 10);
for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}
printf("%i\n", product);
}

for (int i = 0; i < counter / 2; i++)
{
modulo = temp / divisor;
divisor *= 100;
product += (2 * (modulo % 10));
}

Here is the portion where the division occurs.
Code:
for (int i = 0; i < counter / 2; i++)
    {
        modulo = temp / divisor;
        divisor *= 100;
        product += (2 * (modulo % 10));
    }
What could be the problem?
 
Here are a few issues with your code where that negative number could be the result of undefined behavior.

get_long isn't defined and number isn't initialized to a number less than zero.

Some of the int declarations are too small while others can be smaller based on their maximum possible values in your code.

You're using at least C99 based on the variable declarations in code logic, so each variable should use the C99 unsigned long long data type to contain the largest non-negative integers, including 4003600000000014. unsigned long only guarantees a minimum of 4294967295.

i isn't used as an iterator in the first loop, so it uses the the integral constant 10.

The second division loop can also be optimized without the repeated multiplications to calculate the divisor.

Here's the fixed code with no undefined behavior and 4003600000000014 defined as a constant with the ULL suffix.

It outputs counter as 6 and product as 16.

C:
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    unsigned long long number = 4003600000000014ULL;
    unsigned long long temp = number;
    char counter = 1;

    while (number >= 10) {
        number /= 10;
        counter++;
    }

    printf("counter: %d\n", counter);

    int product = 0;
    char i = 0;
    counter /= 2;

    while (i < counter) {
        temp /= 100;
        product += (2 * (temp % 10));
        i++;
    }

    printf("product: %d\n", product);
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom