Name: Anonymous 2007-12-28 13:38
Hi, i'm trying to make a program that does Direct Variation and shows the values of y from: y = kx as a result. So far, I get the desired result but only half way..
Two errors happen:
- Either I get a Seg. Fault
- The fifth value (when you put that you want to see the loop run 5 times) strays away from the variation and becomes a long shit number:
15
45
135
405
778121006
How can I fix this?
CODE:
#include <stdio.h>
//ykx.c - Direct Variation in C
int vari(int k, int x, int t)
{
int ie; // loop count
int yarr[4]; //0 1 2 3 4
int ans;
int num;
num = x; // give intial x value to num.
for (ie = 0; ie < t; ie++)
{
yarr[ie] = k * x; //multiply k and x + put in array
ans = x * num;
x = ans;
printf("%d\n", yarr[ie]);
}
}
main()
{
int k;
int x;
int t;
printf("y = kx\n");
printf("I want you to give me the value of K. Go:");
scanf("%d", &k);
printf("Great. Next, give me the value of X. Go:");
scanf("%d", &x);
printf("How many times do you want to see the values computed?:");
scanf("%d", &t);
printf("Here I go..\n");
vari(k,x,t);
}
Two errors happen:
- Either I get a Seg. Fault
- The fifth value (when you put that you want to see the loop run 5 times) strays away from the variation and becomes a long shit number:
15
45
135
405
778121006
How can I fix this?
CODE:
#include <stdio.h>
//ykx.c - Direct Variation in C
int vari(int k, int x, int t)
{
int ie; // loop count
int yarr[4]; //0 1 2 3 4
int ans;
int num;
num = x; // give intial x value to num.
for (ie = 0; ie < t; ie++)
{
yarr[ie] = k * x; //multiply k and x + put in array
ans = x * num;
x = ans;
printf("%d\n", yarr[ie]);
}
}
main()
{
int k;
int x;
int t;
printf("y = kx\n");
printf("I want you to give me the value of K. Go:");
scanf("%d", &k);
printf("Great. Next, give me the value of X. Go:");
scanf("%d", &x);
printf("How many times do you want to see the values computed?:");
scanf("%d", &t);
printf("Here I go..\n");
vari(k,x,t);
}