first number in array incorrect...

This is a discussion on first number in array incorrect... within the C Programming forums, part of the General Programming Boards category; Can anyone tell me if there is anything wrong with this notation? I have array[200]; then this Code: for(i=0; i<100; ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    32

    first number in array incorrect...

    Can anyone tell me if there is anything wrong with this notation?
    I have array[200];
    then this

    Code:
    for(i=0; i<100; i++){ 
    delta_x[i]=array[i]; 
    } 
    for(i=100; i<200; i++){ 
    delta_y[i-100]=array[i]; 
    }
    In my program everything is okay except that delta_y[0] is not right - it's not in array[200] at all, so it messes everything up by that amount.
    This seems right to me, but I thought maybe there was a problem, or does anyone know why this could happen?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That assignment statement sets delta_y[0] to array[100], so everything is okay there. Are you sure that the error is here and not some later statement?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    I'm not sure of anything, actually, this is really puzzling me. I have the same notation for delta_x[100] as I do for delta_y[100] and delta_x is fine all the way.
    I printed out array[200] to make sure the mistake's not there, and it's not. I also printed out delta_y[100] by itself, and the first number is wrong.
    I don't think there's anything going on between array and delta_y besides the loop I already posted...

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Rather than having two loops, that essentially do the same thing, try this:

    Code:
    for(i=0; i<100; i++){ 
    delta_x[i] = array[i];
    delta_y[i] = array[i+100]; 
    }
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    lol good call. My whole program's kinda like that. I did it in pieces when I was tired... :P But I just condensed my other loops, too. Unfortunately, it didn't change anything. Still same problem...

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    How did you declare delta_x and delta_y?
    Can you post the smallest program that reproduces this problem?
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    They're declared as double delta_x[100], delta_y[100].
    I'm messing with the first part of the program now. Once I get it simplified I'll post some more.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    All right, I think the problem might actually be here

    Code:
         for (i=0; i<100; i++){
             x[i+1]=x[i]+delta_x[i];
             y[i+1]=y[i]+delta_y[i];
             A[i]=bluemoon(x[i], y[i]);
             }
    When I comment this out and print delta_y[100] by itself, the first number is correct.
    But how does this change delta_y?
    bluemoon is my function. :P

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    I think you are still better off showing the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    All right:

    Code:
    #include <stdio.h>
    
    double bluemoon (double x, double y)
    {
           double function;
           double expfunction;
           
           function=-1*x*x;-(1/2)*y*y;
           expfunction=exp(function);
           
           return expfunction;
    }
         
    main ()
    {
         double array[200];
         
         int i;
         double asta;
           
         for (i = 0; i < 200; i++){
             asta=i;
             array[i]=asta;
             }
             
         double delta_x[100], delta_y[100];
         double x[100], y[100], A[100];
                  
         for(i=0; i<100; i++){ 
                  delta_x[i] = array[i];
                  delta_y[i] = array[i+100]; 
                  }
         
         for (i=0; i<100; i++){
             x[i+1]=x[i]+delta_x[i];
             y[i+1]=y[i]+delta_y[i];
             A[i]=bluemoon(x[i], y[i]);
             }
           
           for(i=0; i<100; i++){
           printf("&#37;e\n", delta_y[i]);
           }  
           
         getchar();
         return 0;
    }
    Same problem.
    But if I take out the
    Code:
         for (i=0; i<100; i++){
             x[i+1]=x[i]+delta_x[i];
             y[i+1]=y[i]+delta_y[i];
             A[i]=bluemoon(x[i], y[i]);
             }
    section it's fine.

    EDIT: It also doesn't matter what bluemoon is defined as, still happens. Unfortunately I need that section... lol
    Last edited by sqytoad; 06-09-2008 at 11:04 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    According to your code, delta_y[100] does not exist, since delta_y has 100 elements. Consequently, printing delta_y[100] by itself is accessing the array out of bounds.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    Sorry, I mean printing delta_y[i] from i=0 to i<100.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    I did a quick test (you really need to properly indent your code), and it seems that the problem is due to the array out of bounds when you assign to x[100] and y[100], both of which do not exist. Note that you assign to x[i+1] and y[i+1], but loop from 0 to 99 inclusive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jun 2008
    Posts
    32
    @#$%!! I can't believe I didn't notice that! I'd been looking for that for so long, that was all I thought it could be...
    I guess I'd stared at it so long I'd convinced myself it was fine.

    Oh, well...

    Thanks so much for your help. My larger program is working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

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