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

C help

Name: Anonymous 2013-03-16 21:08

How would I calculate an average recursively in C? Let's say I have an array with 8 numbers. I will calculate the average of the first two, then third and fourth, ... Then I will do it again with the 4 numbers I have and repeat until I only have 2 numbers where I just add them and divide by two. But how do I implement this?

posted this on /g/, thought i'd also try the /g/ textboard as well. Please help!

Name: Anonymous 2013-03-16 21:30

erm... sounds like you know what to do, just not how.. Practice!
(hint: use two loops, and nest one in the other..)

Name: Anonymous 2013-03-16 21:32

>>1
/g/ textboard
6/10

Name: Anonymous 2013-03-16 22:25

/g/ textboard
Godfuckingdamn. IHNBT, because he's an actual retard.

Name: sage 2013-03-16 22:30

>>1
How would I calculate an average recursively in C?
You wouldn't. C doesn't guarantee that tail calls will be eliminated, so every recursive function is a stack overflow waiting to happen.

Name: Anonymous 2013-03-16 22:40

>>5 lmao u so silly...
this is why university hex debts are considered harmful

Name: Anonymous 2013-03-16 22:45

/tech/ is over yonder ;) *points in some direction*

Name: Anonymous 2013-03-16 22:52

/prog/ Challenge numero uno

write a fibs function that does not call a fibs function..

You now understand recursion, and it's relationship to Machine State Implementation

Name: Anonymous 2013-03-16 23:22

>>8
brainfuck does not have recursion (or functions for that matter) yet it is capable of computing fibs. ergo your wrong bitch.

Name: Anonymous 2013-03-16 23:26

>>4
Do you seriously think that that is something that someone would really say? It's a god damn troll you moron.

Name: Anonymous 2013-03-16 23:33

and fibs is a recursive function none-the-less..

Name: Anonymous 2013-03-16 23:35

is there a C/C++ compiler for brainfuck?

Name: Anonymous 2013-03-16 23:45

eh, you mean like a bf -> C converter/translator..? i dunno, probably

Name: Anonymous 2013-03-16 23:56

Show me a version of fibs that can calculate fib(n) without first calculating fib(n-1) for any n>5... and i will show you that it does ;)

Name: Anonymous 2013-03-17 0:40

Name: Anonymous 2013-03-17 0:42

>>15
...actually, on that note: http://oeis.org/A000045/graph

See the second graph? You can compute the Fibonacci sequence directly with a bit of jiggling and an exp() function.

Name: Anonymous 2013-03-17 0:46

>>14
10 minutes in Racket.
(define (integrate func m) (cond [(> m 0) (+ (func m) (sum func (- m (/ 0.1 1))))] [else 0]))
(define (factorial x)(* (integrate (lambda (t) (/ (expt t x) (expt 2.71828182846 t))) 100.00) (/ 1 10)))

Name: Anonymous 2013-03-17 0:47

This is a fun problem. Imagine you have an array, A = [x1, x2, ... xn], and you have calculated the average of the first n-1 terms, an-1. How do you calculate the average of all terms, using only n, xn, and an-1?

Name: Anonymous 2013-03-17 0:49

>>17
factorial
Shit! I meant fibs! Now I look stupid instead of playfully stupid. ;-,-)<==,|,^,|,==>(-,-;

Name: Anonymous 2013-03-17 0:56

[a_{n-1}/(n-1) + x_n]/n

Name: Anonymous 2013-03-17 0:57

whoops
[a_{n-1}*(n-1) + x_n]/n

Name: Anonymous 2013-03-17 1:12

>>21
Youweresupposedtolet>>1-sandoit.

Name: Anonymous 2013-03-17 1:35

fold/reduce

imperative plebs be jelly

Name: Anonymous 2013-03-17 2:59

>>2
he said recursively so his teacher probably forbade loops
anyway the task is retardedly simple
>>8
lolwut, fibbonachi is easy to do with an ordinary while loop

Name: Anonymous 2013-03-17 3:04

>>24
back to /g/

Name: Anonymous 2013-03-17 3:14

>>25
so anybody who think you are stupid belongs to /g/?

Name: Anonymous 2013-03-17 3:23

>>26
back to /g/

Name: Anonymous 2013-03-17 4:21

double average(double *numbers, size_t n)
{
    size_t left, right;

    switch (n) {
    case 0: return 0.0/0.0;
    case 1: return numbers[0];
    case 2: return (numbers[0] + numbers[1])/2.0;
    default:
        right = n/2;
        left = n - right;
        return (average(numbers, left) + average(numbers + left, right))/2.0;
    }
}

Name: Anonymous 2013-03-17 6:15

Now do it ``in Shitta''

Name: Anonymous 2013-03-17 7:57

import Control.Arrow
import Data.List (genericLength)

avg :: Fractional a => [a] -> a
avg = uncurry (/) . (sum &&& genericLength)

Name: Anonymous 2013-03-17 8:06

import meme.arrow

Name: Anonymous 2013-03-17 9:59

[code]
include <recursive_average>
include <stdio>

int main(const int in_average_of_thi

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