Reputation: 294
int main(){
int N, i, j=0;
float MA, MB, asum=0, bsum=0, y;
printf("\number of pairs: "); scanf("%d", &N);
int a[N+1], b[N+1], c[N+1];
for(i=1; i<N+1; i++){
printf("\na%d",i); printf("=");
scanf("%f", &a[i]);
printf("b%d",i); printf("=");
scanf("%f", &b[i]);
printf("\n aSUM= %.6f \n",asum);
asum+=a[i];
printf("\n aSUM= %.6f \n",asum);
}
The idea of this code is simple. User inputs int or float values, then they get summed and outputted as a float value. However I'am getting astronomical values straight away. Fe. if it tries to make addition of 0 and 7, it outputs a value of 1088421888.000000. What the heck is going on?? :D
Upvotes: 0
Views: 107
Reputation: 1
You should enable all warnings with a modern C compiler (like GCC 4.8).
int a[N+1], b[N+1], c[N+1];
//...later
scanf("%f", &a[i]);
This cannot work: %f
is for scanf(3) a control format requiring a float
pointer, and &a[i]
is a pointer to an int. (and GCC would have warned about that).
And you have other errors too. Please enable all warnings -e.g. compile with gcc -Wall
BTW, it is much better to put \n
at the end of your printf
format control string (not at the beginning!).
Upvotes: 5
Reputation: 106102
You are using wrong specifier for int
. It will invoke undefined behavior. You will get anything. You are lucky that you are not getting the desired result!
Upvotes: 2
Reputation: 1
sscanf doesn't know about the type of their pointer parameters, so you read in floats in integer pointers. Thus, integer pointers were as float interpreted.
You need to scanf into a temporary float variable, and then convert this to integers. So:
float theFloat;
sscanf("%f", &theFloat);
N[a]=theFloat;
Upvotes: 2