PROBLEM CLOSED: i copied and pasted (that is, exact same) the code from windows and used SSh to connect to a solaris server (at school), compiled, executed, and it gave the correct output. im just thankful i dont have to pay for this junk of an OS Windows XP (license from school--not pirated).
first, this isnt homework, im working on a problem from a math/programming site (projecteuler.net if i may post it). the code is very basic, but ive cut it down quite a bit. from what i have so far, it will seem pointless and probably confusing, but here i go. the limits of the for loops will seem confusing, please try and ignore them.
the inner for loop takes characters from the string at position k and converts it to an integer. it does this 5 times, multiplying the new digit each time (ie 1*digit1*digit2*...)
i have two print statements in the inner for loop to verify im reading the correct character at position k, and it is properly converted to an int. if you run exactly what i have (at least for me), it works for the first few iterations.. then on the last iteration it reads in a '6' and converts it to integer '61'. and i have absolutely no clue why. ive looked it over and over, and i dont understand why it works for seemingly every case but this exact 6!
following the code is output from running the program.
Code:
#include<stdio.h>
#include<stdlib.h>
int max(int a, int b)
{
if (a >= b)
return a;
else
return b;
}
int main()
{
char num[100] = {"73167176531330624919225119"}; // no, it isnt 100 characters but it shouldnt matter
int curMax = 0;
int i, k;
for (i = 0; i < 5; i++)
{
int temp = 1;
for (k = 0 ; k < 5; k++)
{
char x = num[i+k];
printf("x = '%c'\n", x);
int digit = 0;
digit = atoi(&x);
printf("digit= '%d'\n", digit);
temp *= digit;
}
printf("\n");
curMax = max(curMax, temp);
}
printf("%d\n", curMax);
}
NOTE: x = 'i' means the character value, digit='i' means the integer value.
Code:
x = '7'
digit= '7'
x = '3'
digit= '3'
x = '1'
digit= '1'
x = '6'
digit= '6'
x = '7'
digit= '7'
x = '3'
digit= '3'
x = '1'
digit= '1'
x = '6'
digit= '6'
x = '7'
digit= '7'
x = '1'
digit= '1'
x = '1'
digit= '1'
x = '6'
digit= '6'
x = '7'
digit= '7'
x = '1'
digit= '1'
x = '7'
digit= '7'
x = '6'
digit= '6'
x = '7'
digit= '7'
x = '1'
digit= '1'
x = '7'
digit= '7'
x = '6'
digit= '6'
x = '7'
digit= '7'
x = '1'
digit= '1'
x = '7'
digit= '7'
x = '6'
digit= '61' ************* WHY IS THIS 6 == 61!
x = '5'
digit= '5'
14945
im sure its hard to look past the seemingly confusing logic and pointless algorithm, but thanks for any input.
EDIT: it seems that it works when i change the number before the number in question (the 7 before the 6) to something else! this cant be a solution, because i cant alter the number (its a 1000-digit number). also, initially i copied and pasted the number, but same problem after actually typing in the first, say 20, digits. i thought that could be a problem with having non-printable characters, but its a plain text file anyways so theyre arent any.