Wrong variable value assigned
Hello:
I am trying to make an application which takes input of sets of 2 numbers and does a mathematical equation with them and displays the answer for each set.
However; I have been testing and testing this code:
Code:
/* CSC1140@ 07611201 74kzYkXl 10523 */
#include <stdio.h>
int main()
{
int c,n,a;
while(c = getchar() != EOF)
{
int sum=0;
scanf("%d",&n);
scanf("%d",&a);
int i;
for(i=1; i<=n; i++)
{
int calc=0,x=0,final_a = a;
for(x=1;x<i;x++)
{
final_a = final_a * a;
}
calc = i * final_a;
sum = sum + calc;
}
printf("N:%d A:%d\n",n,a);
printf("%d\n",sum);
}
}
I am using the input as
The output displays the numbers as it is supposed to; but gives the wrong answers. So I debugged my code and printed out N and A for each set and found out that the "a" variable is not being correctly assigned.
The "a" value seems to be assigned 4 in the first set (a is supposed to be 3).
the n is correctly assigned.
Maybe it is a really simple mistake I just cant see... any help would be appreciated!