Please debug the error!

This is a discussion on Please debug the error! within the C Programming forums, part of the General Programming Boards category; Hi friends, i have written a C-prog,for implementing the equation in the attached file.. plz see the attachment! assume tau(0)=0 ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Please debug the error!

    Hi friends, i have written a C-prog,for implementing the equation in the attached file.. plz see the attachment!



    assume tau(0)=0
    tau(1)=0.1
    T=0.1
    X(tau(0))=1 as we know X = e^-t
    X(tau(1))= X(0.1)= e^-0.1

    n=2,3...20.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
    float x[20]={0},tau[20]={0},A,B,C,D,e=2.718;
    int n,T=0.1;
    tau[0]=0;
    tau[1]=0.1;
    x[0]=pow(e,0);
    x[1]=pow(e,-0.1);
    clrscr();
    for(n=1;n<=20;n++)
    {
    x[tau[n]]=pow(e,-tau[n]);
    x[tau[n-1]]=pow(e,-tau[n-1]);
    A=x[tau[n]]-x[tau[n-1]];
    B=tau[n]-tau[n-1];
    C=A/B;
    if(C<0)
    C=-C;
    D=C+1;
    tau[n+1]=tau[n]+T/D;
    printf("tau[n+1]=%f\n",tau[n+1]);
    }
    getch();
    }
    This prog is giving some error,if anyone debugs it plz let me know

    thnx in advance!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    Code:
    for(n=1;n<=20;n++)
    {
    x[tau[n]]=pow(e,-tau[n]);
    x[tau[n-1]]=pow(e,-tau[n-1]);
    A=x[tau[n]]-x[tau[n-1]];
    B=tau[n]-tau[n-1];
    C=A/B;
    if(C<0)
    C=-C;
    D=C+1;
    tau[n+1]=tau[n]+T/D;
    printf("tau[n+1]=%f\n",tau[n+1]);
    }
    in the last two iterations you are trying to access tau[20] and tau[21]....
    i think it should be n<19

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to indent your code to make it more readable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by stewie griffin View Post
    Code:
    for(n=1;n<=20;n++)
    {
    x[tau[n]]=pow(e,-tau[n]);
    x[tau[n-1]]=pow(e,-tau[n-1]);
    A=x[tau[n]]-x[tau[n-1]];
    B=tau[n]-tau[n-1];
    C=A/B;
    if(C<0)
    C=-C;
    D=C+1;
    tau[n+1]=tau[n]+T/D;
    printf("tau[n+1]=%f\n",tau[n+1]);
    }
    in the last two iterations you are trying to access tau[20] and tau[21]....
    i think it should be n<19
    stewie is correct. The maximum element for tau is 19, so 20 and 21 are out-of-bounds.
    And then indent your code from this unreadable mess!

    And know that main returns int, not void!
    Last edited by Elysia; 01-14-2009 at 03:09 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Smile

    Hi friends, thanks for ur replies

    i'll try the changes you have suggested!
    was in a hurry ,so couldn't indent the prog ..

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Indentation is not second-hand. It is first priority and is done as you write the code, not afterwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    ok , i'll practice indenting as and when i write the program.. thanks for your suggestion!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21