Results 1 to 5 of 5

Thread: C program is unable to give correct output

  1. #1
    Join Date
    Jan 2010
    Posts
    43

    C program is unable to give correct output

    I have written a small program in C but I don't really understand why my program does not work. Here is my code:

    Code:
    #include <stdio.h>
    #include <math.h>
    void main(void)
    {
    FILE *outFile;
    float p[1000];
    float lambda = 1.0e-3;
    float i;
    int j;
    int m = 2;
    outFile=fopen("myfile.txt","w");
    for (i=0.1; i < 1.0; i+=0.1)
    {
    for (j=0; j<=1000; j++)
    {
    p[0]=1.0e-5;
    p[j+1]=p[j] + lambda * pow((p[j]),(m/2)) * pow(i,m);
    fprintf(outFile, "\n%lf\n",i,j,p[j+1]);
    }
    }
    fclose(outFile);
    }
    But the generated output is not satisfactory. What and where is the problem? For example i need to print out the value of i, j and p[j+1]. and the iterations seem wrong. It suppose to come out like this:

    (example answer only)

    i j p[j+1]

    0.1 1 1.0002e-5
    0.1 2 1.0003e-4
    .
    .
    0.2 1 1.0005e-4
    .
    .
    .
    1 1000 1.011e-2

    One more thing: is it possible for me to draw this number into line chart/graph with the help of above program?

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: C program is unable to give correct output

    There are few bugs in your code, some of which include:

    1. Your inner FOR loop is buggy since it is resulting into stack corruption:
    Code:
    for (j=0; j<=1000; j++)
    {
    p[0]=1.0e-5;
    p[j+1]=p[j] + lambda * pow((p[j]),(m/2)) * pow(i,m);
    fprintf(outFile, "\n%lf\n",i,j,p[j+1]);
    }
    2. The below line of code simply prints/saves the variable i onto your outFile.

    Code:
    fprintf(outFile, "\n%lf\n",i,j,p[j+1]);

  3. #3
    Join Date
    Jan 2010
    Posts
    43

    Re: C program is unable to give correct output

    If that's the equation that I need to use without changing it, is there any other way to do that?

    Code:
    p[j+1]=p[j] + lambda * pow((p[j]),(m/2)) * pow(i,m);
    Thanks for your reply.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: C program is unable to give correct output

    What I'm saying is that array "p" is having 1000 elements. These elements are indexed from 0 to 999. If your FOR loop goes up to this j<=1000 value then at some point you will reach p[999+1]=p[999] and then p[1000+1]=p[1000] which implies array out of bounds. Atmost your loop will be able to go to and stay withing the bounds of the array is 998. So at most you will have p[998+1]=p[998] and the loop should then stop. Do you understand what I mean?

  5. #5
    Join Date
    Nov 2008
    Posts
    1,054

    Re: C program is unable to give correct output

    In other words you don't need to have so big array on the stack. You could even store those values to heap. I mean you can have something like this:

    Code:
    float *temp = new float[1000];

Similar Threads

  1. How to get a Java Program output in Tabular format
    By Falgu in forum Software Development
    Replies: 3
    Last Post: 18-07-2012, 04:15 PM
  2. apple tv 2 unable to give output of 5.1 dolby audio
    By Dulani in forum Monitor & Video Cards
    Replies: 8
    Last Post: 10-01-2012, 07:07 AM
  3. Windows batch file: set output of program to a variable?
    By supernoob in forum Windows Software
    Replies: 5
    Last Post: 17-10-2010, 05:58 AM
  4. How to find the correct program for unknown file ?
    By SoftwareGuy in forum Tips & Tweaks
    Replies: 0
    Last Post: 02-04-2009, 05:38 PM
  5. Replies: 0
    Last Post: 20-03-2009, 05:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,433,668.86328 seconds with 17 queries