I have some code that just does seem to work right. The code looks like this
Code:
#include <stdio.h>
#include <math.h>
int main() {
double a=1, b=1, *r;
int i=2, n=10;
r = malloc(n);
r[0] = 1;
while(i<n){
if( i%4 == 0){
a += 1;
b += 2;
}
else if( i%2 == 0){
a += 3;
b += 4;
}
r[i-1] = a;
r[i] = b;
i += 2;
}
for(i = 0; i<n; i++){
printf("r[%d] = %f \n",i, r[i]);
}
return 0;
}
It should output something that looks like this
Instead it outputs
Which is clearly not what I want.
Does any one know how I can make this code work? What is wrong with it? I suspect it has something to do with my pointers, but I'm not positive.