Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C Noob needs help

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);



}


Name: Anonymous 2007-12-28 14:01

Fuck, nevermind I found it haha

Name: Anonymous 2007-12-28 14:09

It's hard to help
when the asker is a silly whelp.

Name: Anonymous 2007-12-28 14:14

We have to abort
when the array is too short

Name: Anonymous 2007-12-28 14:19

>>4
It's hard to abide by one theme
I know because I invented this meme.

Name: Anonymous 2007-12-28 14:19

>>4
We don't need it
when our code isn't shit.

int vari(int k, int x, int t)
{
    int num = 1;

    while(t--)
    {
        printf("%d\n", k * num);
        num *= x;
    }
}

Name: Anonymous 2007-12-28 14:20

>>5
It's hard to rhyme
WHEN I AM SHOVING A CUDDER DOWN YOUR THROAT, MOTHERFUCKER

Name: Anonymous 2007-12-28 15:02

>>6

OP here,
Holy cow, thats nice. Makes my code look overweight and unhealthy

Name: Anonymous 2007-12-28 15:15

>>7
It's hard to drive a car
when your other car is a cudder.

Name: Anonymous 2007-12-28 15:21

>>8
Your code is fat
but his code is phat

Name: Anonymous 2007-12-28 17:03


int yarr[4]; //0 1 2 3 4

0,1,2,3 faggot

Name: Anonymous 2007-12-28 19:26

>>11
See >>2-10

Name: Anonymous 2007-12-28 19:40

>>6
Nice try,
don't cry.

int vari(int k, int x, int t) {
 while(t--)
  printf("%d\n",k),k *= x;
}

Name: Anonymous 2007-12-28 19:45

>>13
Nothing was returned
A voidis yearned.

void vari(int k, int x, int t) {
 while(t--)
  printf("%d\n",k),k *= x;
}

Name: Anonymous 2007-12-28 19:59

>>14
Awesome

Name: Anonymous 2007-12-28 20:07

>>14
Don't waste vertical space;
with a one-liner, replace:

void v(k,x,t){while(t--)printf("%d\n",k),k*=x;}

Name: Anonymous 2007-12-29 0:08

>>16
Stop at zero,
Don't underflow.

void v(k,x,t){while(t-->0)printf("%d\n",k),k*=x;}

Name: Anonymous 2007-12-29 0:19

>>17
The off-by-one error
is my most feared terror

Name: Anonymous 2007-12-29 1:08

>>17
It does stop at 0 moron.

Name: Anonymous 2007-12-29 2:35

FROSTED BUTTS

Name: Anonymous 2007-12-29 4:23

>>19
Not t is below zero
moron; Become an hero.

Name: Anonymous 2007-12-29 4:24

>>21
You can't spell.
Go to hell.

Name: Anonymous 2009-03-18 3:04

The word pirahna, is all I can think of that rhymes with marijuana

Marijuana MUST be legalized.

Name: Anonymous 2009-08-16 23:17

Lain.

Don't change these.
Name: Email:
Entire Thread Thread List