c - Why isn't it adding my numbers correctly -
i'm trying add 100 numbers in array code: numbers 17 117 should add lot more.
#include <stdio.h> #include <stdlib.h> int main(){ int* number = malloc(101*sizeof(int)); int num = 0; while (num != 101){ number[num] = 17 + num; num = num + 1; }; num = 0; while(num != 101){ printf(" %d \n", number[num]); num = num + 1; } num = 0; int sum = 0; while (num != 101){ number[num] = 17; sum = sum + number[num]; num = num + 1; } printf(" %d \n ", sum ); return 0; }
the bottom print statement gives me value of 1717
while (num != 101){ number[num] = 17; sum = sum + number[num];
remove second line has been resetting 17
Comments
Post a Comment