2

I'm trying to find a solution(fix errors) in my programme which must count the binomial theorem from definition. Firstly I created the definition of "factorial" - "silnia".

1) The algorithm determines the value of SN1 (n,k) of the definition. (newton function)

2) The algorithm determines the value of SN3 (n,k) recursively by the formula. (newton_rek function).

INPUT: File name: In0101.txt

OUTPUT: File name: Out0101.txt In this file I want to save the values ​​calculated from the formulas.

EXAMPLE: In0101.txt

8 2// n k 

Out0101.txt

n=8 k=2
SN1 = 28; count= 14

And there is an error I can't fix. Does anybody can help me with this ?

MY CODE:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

long silnia(int a)
{
    long s;
    if (a == 0 || a == 1)
    {
        return 1;
    }
    else
    {
        s = 1;
        for (int i = 1; i <= a; i++)
        {
            s *= i;
        }
    }
    return s;
}

long newton(int n, int k)
{
    return silnia(n)/(silnia(k)*silnia(n-k));
}

unsigned long int newton_rek(long int n ,long int k)
{
    if ( n == k || k == 0 )
    {
        return 1;
    }

    if (k > n)
    {
        return 0;
    }

    else return newton_rek(n-1,k-1) + newton_rek(n-1,k);
}

int main()
{
    int n = 0;
    int k = 0;
    long funkcja1 = 0;
    long funkcja2 = 0;

    FILE *f = fopen("In0101.txt", "r+");    
    if (f == NULL)
    {
        printf("Nie udalo sie otworzyc pliku In0101.txt\n");
        return 1;
    }
    fread(n, sizeof(long), 1 , f);
    fread(k, sizeof(long), 1 , f);
    fclose(f);

    FILE *ff = fopen("Out0101.txt", "w+");    

    if (ff == NULL)
    {
        printf("Nie udalo sie otworzyc pliku Out0101.txt\n");
        return 1;
    }

    funkcja1 = newton(n,k);
    funkcja2 = newton_rek(n,k);
    fwrite(funkcja1, sizeof(long), 1 , ff);
    fwrite(funkcja2, sizeof(long), 1 , ff);
    fclose(f);

    return 0;
}
paddy
  • 60,864
  • 6
  • 61
  • 103
  • 2
    What is the error? Also, I suggest you re-think your indentation for `silinia`. It's really hard to read. – Blender Oct 29 '12 at 22:36
  • http://pastebin.com/w7vvqCQh Maybe it's better to read code. – Maciej Januszewski Oct 29 '12 at 22:38
  • I ran this code through auto-layout in Visual Studio to fix the indentation. – paddy Oct 29 '12 at 22:42
  • 1
    The naive implementation will fail because the factorial will very quickly overflow a `long`. You need to do something more clever. (Hint: Change the order of operations so divisions cancel multiplications.) – Raymond Chen Oct 29 '12 at 22:46
  • It would help if you briefly described the binomial theorem for those of us who don't know it by heart. I notice that your `newton_rek` function does not use the `newton` function. Should it? What is your expected output? What is your actual output? – paddy Oct 29 '12 at 22:47
  • @Paddy: This is where I write it. – Maciej Januszewski Oct 29 '12 at 22:47
  • To be honest, I don't know if I understood the theorem from definition. I used "wikipedia" to write math formulas. I thought there must be a definition of factorial function and using it write the rest of task. – Maciej Januszewski Oct 29 '12 at 22:50
  • Take a look at Pascal's triangle for another way to compute binomial coefficients. – A. Webb Oct 29 '12 at 22:55
  • Seem to be ok(compilation status). But right now, there is a problem in "silnia" function... http://ideone.com/qOal4y – Maciej Januszewski Oct 30 '12 at 00:06

1 Answers1

4

Your calculations both generate Pascal's triangle. I have done a short test: http://ideone.com/jHA8EJ

I think your problem is that you are not outputting correctly. You did not state the problem you were having in your question, so people suspected it was algorithmic due to your lack of description.

I believe the problem is actually here:

fwrite(funkcja1, sizeof(long), 1 , ff);
fwrite(funkcja2, sizeof(long), 1 , ff);

There's two things wrong:

  1. You are not taking the address of the variables you are writing. That's likely to cause a crash (which perhaps you might have mentioned);
  2. You are trying to write them as binary, but you seem to be expecting text.

You should replace those calls with something like this:

fprintf( ff, "%d %d\n", funkcja1, funkcja2 );

As Daniel Fischer pointed out:

The address thing also applies to reading the input file (and also the byte/text representation thing), furthermore fread gets the wrong size parameter.

That is:

fread(n, sizeof(long), 1 , f);
fread(k, sizeof(long), 1 , f);

Same two principles apply. You are reading binary values and you are doing it incorrectly. Instead, read text:

int nvals = fscanf( f, "%d%d", &n, &k );

You should test that nvals is 2, indicating that both values were read successfully.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • 1
    The address thing also applies to reading the input file (and also the byte/text representation thing), furthermore `fread` gets the wrong size parameter. – Daniel Fischer Oct 29 '12 at 23:18
  • Oh thanks, I didn't even look at that. I'll add that to my answer. – paddy Oct 29 '12 at 23:20
  • @paddy: I think it shouldn't print the results on screen, just to put them to the file called Out0101.txt Or maybe I'm wrong. "Ansi C" isn't my best language, I used to write in Java, but our dr requires to have it done in Ansi C. And trying your idea: http://ideone.com/kQbpj6 – Maciej Januszewski Oct 29 '12 at 23:23
  • @MaciejJanuszewski My test program has nothing to do with your required output. It is simply checking that your two algorithms function as expected. My answer is pointing you in the direction of fixing the "error" that you are probably getting. You fix your code accordingly and modify to get the output in the format you require. – paddy Oct 29 '12 at 23:25
  • I think you are supposed to use `"%ld"` for `long`.. My bad. I don't usually do much C. Your `fread` calls are still wrong. You must read my answer, please. – paddy Oct 29 '12 at 23:27
  • I seem to be a donkey. I haven't slept for 2 days trying to implement other algorithms, and this task makes me angry. I can't focus. I want to be hones...I'm waiting for a hero, to give me a ready code :( – Maciej Januszewski Oct 29 '12 at 23:41
  • Seem to be ok(compilation status). But right now, there is a problem in "silnia" function... – Maciej Januszewski Oct 29 '12 at 23:51
  • Yeah, if you notice I corrected the error in the version that I posted. You cannot declare `i` inside the `for` loop like that. It's not ANSI C. – paddy Oct 30 '12 at 00:51
  • I've changed it. Right now it looks like this: http://pastebin.com/4ZcJjACY Does anyone would try to compile it creating in his project file "In0101.txt" and putting some values ? I hope it finally works. Seems to work in my compiler... – Maciej Januszewski Oct 30 '12 at 00:58