Thread: Incrementing Array - I think the solution's simple but I'm missing something

  1. #1
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34

    Incrementing Array - I think the solution's simple but I'm missing something

    I want to initialise an array called 'speed' which has values [0 0.5 1 1.5 ... up to 50]. I'm testing it for 5 values in the array. I know I could do it manually but I would prefer to be able to change it easily later.

    My current code is giving me no error messages when I compile it but I'm getting a segmentation fault when running it and I'm not sure why. I know segmentation fault is due to memory problems but am unsure where I'm going wrong - I think it's probably with the 'i+1' section. The lack of displaying 'i' is a bit odd..

    Code:
    /* Relevant section */
    #include <stdio.h>
    #include <stdlib.h>
    
     float speed[5]={'\0'};
     int i=0;
    
    for (i=0; i<5;)
         
      {
        speed[0]=0;
        speed[i+1]=speed[i]+0.5;
     
        printf("Speed array: %f\n",speed[i]);
        printf("i=\n",i);
    
        i++;
      }
    What it prints:

    Code:
    Speed array: 0.000000
    i=
    Speed array: 0.500000
    i=
    Speed array: 1.000000
    i=
    Speed array: 1.500000
    i=
    Speed array: 2.000000
    i=
    Segmentation fault
    Any advice would be gratefully received!

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    13
    The reason why your output is like this i = nothing, check this one on your code :
    printf("i=\n",i);
    you have to identify what is the data type of i, here its int so make it like this:
    printf("i=%d\n",i);

    if you can see the output already loops in 5 times so it already satisfies the loop, try to have return in your code to control the flow.
    and one thing as far as i know this segmentation fault just happen when there is an array accessing an empty memory or sometimes having stack overflow so it will terminate ur program in unexpected manner.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The '\0' is a char representation of zero, not a float. Use 0.0 instead.

    When i = 4, i+1 is illegal, since it goes beyond the end of the array.

    There is no need to keep assigning 0 to your lowest array element, 5 times. Once will do (do it outside the for loop). If you don't want the for loop changing speed[0], you could just have i start at 1, instead of zero.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34
    Great, thank you, it works now - probably not the prettiest code ever but oh well. The i problem made me wince.. stupid mistake.

    Code:
      float speed[50]={0.0};
    
      speed[0]=0;
      i=1;
    
      while (i<50) 
         
        { if ((i+1)!=50)
    		  {   
        speed[i+1]=speed[i]+0.5;
     
        printf("Speed array: %f\n",speed[i]);
        printf("i=%d\n",i);
    
        i++;
    		  }
          else
    	{
    	return(0);
    	}
    
      }

  5. #5
    Registered User
    Join Date
    Jul 2010
    Location
    Liverpool
    Posts
    34
    EDIT: thought I had a problem with this section cause it was coming up with seg faults again but turns out it was to do with trying to close a file that I hadn't actually opened.. sorry!
    Last edited by snoikey; 07-14-2010 at 07:20 AM. Reason: no problems whatsoever :)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM