error: ‘numberOfElements’ undeclared

This is a discussion on error: ‘numberOfElements’ undeclared within the C Programming forums, part of the General Programming Boards category; Hi this is one of my questions i have been given to do but i cant get it to compile ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    12

    error: ‘numberOfElements’ undeclared

    Hi this is one of my questions i have been given to do but i cant get it to compile please help!
    question was :
    Write a function called arraysum that takes two arguments: an integer array and the number of elements in the array. Have the function return as its result the sum of the elements in the array.

    My attempt:
    Code:
    #include <stdio.h>
    
    
    
    
    
    
    int arraysum (int array[], int numberOfElements)
    
    {	
    	
     int i, sum=0;
    	 
    
    	
    	
    
      for (i =0; i <numberOfElements; ++i)
    
    
    
      sum = sum + array[i];
    
    	
    
     return sum;
    
    }
    	
    	
    int main (void)
    
    {
    
    
    	int array1[5] = {15,28,37,26,10};
    	
            numberOfElements = 5;  
    	
            int i, sum =0;
    
    	int arraysum (int array[],int numberOfElements);
    
    
    	
    	
    
    printf("The sum is %d\n", arraysum(array1,numberOfElements));
    
    
    	
    return 0;
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What's this ... int arraysum (int array[],int numberOfElements); ... doing in main?

    Just lose it.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    Please post the complete error message exactly as it appears.

    Jim

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Also, choose a better thread title.
    C Board - Announcements in Forum : C Programming
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    prog8.11.c: In function ‘main’:
    prog8.11.c:27: error: ‘numberOfElements’ undeclared (first use in this function)
    prog8.11.c:27: error: (Each undeclared identifier is reported only once
    prog8.11.c:27: error: for each function it appears in.)

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,697
    prog8.11.c:27: error: ‘numberOfElements’ undeclared (first use in this function)
    Look at line 27
    Code:
    numberOfElements = 5;
    Where have you told the compiler what kind of variable this is? Is it a double, an int, a char?

    Jim

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    12
    thanks jim fixed the problem it works fine now

    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Libraries in C
    By TheOriginalGame in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 11:19 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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