Sum of an array with pointers

This is a discussion on Sum of an array with pointers within the C Programming forums, part of the General Programming Boards category; Hi, I've been trying to sort this out all day, and it's driving me crazy because I'm pretty sure its ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    5

    Question Sum of an array with pointers

    Hi, I've been trying to sort this out all day, and it's driving me crazy because I'm pretty sure its just a matter of straightening out the functions and pointers, but I can't make anything work.

    I need to calculate the sum of the first 20 Fibonacci numbers with the fibonacci numbers stored in an array. This is where I'm at, any help would be very much appreciated. Thanks!

    Code:
    #include <stdio.h>
    
    int arraySum(int *array, const int n)
    {
    	int sum = 0;
    	int * const arrayEnd = array + n;
    	
    		 for ( ; array < arrayEnd; ++array )
    		 sum +=*array;
    		 
    		 return sum;
    		 }
    
    
    int main(void)
    {
    		 int arraySum (int *array, const int n);
    		 int i, numFibs= 20;
    	
    	int fibonacci[numFibs];
    	
    	fibonacci[0] = 0;
    	fibonacci[1] = 1;
    	
    	for (i = 2; i < numFibs; ++i)
    		fibonacci[i] = fibonacci[i-2] + fibonacci[i-1];
    	
    		 
    	printf("Their sum is %i\n ", arraySum (fibonacci, 20) );
    	
    	return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can't do this:
    Code:
    int numFibs= 20;
    int fibonacci[numFibs];
    because the array's dimension needs to be static and reserved at compile time, and you are making it dynamic to be allocated at runtime. Won't work in C.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dino View Post
    You can't do this:
    Code:
    int numFibs= 20;
    int fibonacci[numFibs];
    because the array's dimension needs to be static and reserved at compile time, and you are making it dynamic to be allocated at runtime. Won't work in C.
    It will work in C99, although I don't really see the point.


    Quzah.
    Last edited by quzah; 11-16-2009 at 09:33 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Any other clues? Sorry I am terrible at this. Tried just plugging fibonacci[20] in, but that clearly doesn't work...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What does "doesn't work" mean?


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    oh sorry, the compiler just reads "running" the way it had before when i had

    Code:
    int numFibs= 20;
    int fibonacci[numFibs];
    Does it make sense to separate the two? So the fibonacci is it's own function?

    kind of like this, except not because that still doesn't work

    Code:
    #include <stdio.h>
    
    int arraySum(int *array, const int n)
    {
    	int sum = 0;
    	int * const arrayEnd = array + n;
    	
    		 for ( ; array < arrayEnd; ++array )
    		 sum +=*array;
    		 
    		 return sum;
    		 }
    int fibonacci(int *fib)
    {
    	int i;
    	fib[20];
    	fib [0] = 0;
    	fib [1] = 1;
    	
    	for(i = 2; i < 20; ++i)
    	fib[i] = fib[i-2] + fib[i-1];
    	return 0;
    }
    			  
    int main(void)
    {
    		 int arraySum (int *array, const int n);
    		 int fibonacci(int *fib);
    	int fib[20];
    		 
    	printf("Their sum is %i\n ", arraySum (fib, 20) );
    	
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Just for the record, this is for a continuing ed class - there is no grade involved. I work as a designer and just want a better understanding of programming languages.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Jamathorn View Post
    oh sorry, the compiler just reads "running" the way it had before when i had

    Code:
    int numFibs= 20;
    int fibonacci[numFibs];
    Does it make sense to separate the two? So the fibonacci is it's own function?

    kind of like this, except not because that still doesn't work

    Code:
    #include <stdio.h>
    
    int arraySum(int *array, const int n)
    {
    	int sum = 0;
    	int * const arrayEnd = array + n;
    	
    		 for ( ; array < arrayEnd; ++array )
    		 sum +=*array;
    		 
    		 return sum;
    		 }
    int fibonacci(int *fib)
    {
    	int i;
    	fib[20];
    	fib [0] = 0;
    	fib [1] = 1;
    	
    	for(i = 2; i < 20; ++i)
    	fib[i] = fib[i-2] + fib[i-1];
    	return 0;
    }
    			  
    int main(void)
    {
    		 int arraySum (int *array, const int n);
    		 int fibonacci(int *fib);
    	int fib[20];
    		 
    	printf("Their sum is %i\n ", arraySum (fib, 20) );
    	
    	return 0;
    }
    Where are you calling the fibonacci function? You dont need to have another array in your function arraySum, it can be done without it too, simply running the loop till 'n'.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    When I run the first posting, I get 10945. What's the answer supposed to be?

    I didn't even try the second posting because the fibonacci() function is defined but not called and fib[] is passed uninitialized to arraySum().

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    5
    Wow, you're right that does work...I'm not sure why I couldn't make that happen before. Now I feel extra ridicules, though slightly less broken. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 03:45 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

Tags for this Thread


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