Thread: Arrays and functions? (easy question)

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Arrays and functions? (easy question)

    Code:
    #include "stdafx.h"
    
    
    #define size 100
    int countPrimes(int,int);
     
    int main()
    {
    	
    /* Declaration of Variables */
    	int arr[size], i, j=1, remainder, primecount=0, max=size;
    
    
    /* Fill the array with numbers 1 through the max size (100) */
        for(i = 0;i < size; i++)
        {
          arr[i] = j++;
        }
     
    
    
        for(j = 2; j <= size; j++)
    	{
    
    
            for(i = j; i < size; i++)
    		{
              remainder = arr[i] % j;
     
    /* If the number is not prime, set it to a 0. */
    		  if(remainder == 0)
    			 arr[i] = 0;
    			
            }
        }
    
    
    /* Count the prime numbers in the array */
    		for(int i=0;i<size;i++){
    		if (arr[i]!=0)
    			primecount++;
    	}
    
    
    
    
    /* Print the array, not including the 0s. */
        for(i = 0; i < size; i++)
        {
    	if (arr[i]!=0)
    		printf("%d ",arr[i]);
        }
    
    
    /* Print the amount of prime numbers calculated. */
    		printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);
    	return 0;  
    }
    I want to use functions for main, instead of doing everything inside of main.. I don't know how to pass arrays though... I want to just have like the functions, "arrayFiller", "primeChecker", "primeCounter", "arrayPrinter".... But how?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Arrays should be passed using their name, and their size:
    Code:
    void foo( type arrayname[SIZE] )
    {
    }
    ...
    type anarray[SIZE];
    ...
    foo( anarray );
    So if we flesh that out a bit further:
    Code:
    void foo( int a[5] )
    {
        ...
    }
    ...
    int myarray[5];
    ...
    foo( myarray );
    Now optionally you can omit the left most dimension:
    Code:
    void bar( char c[][5] )
    {
        ...
    }
    char a[ 10 ][ 5 ];
    ...
    bar( a );
    This includes single dimension arrays:
    Code:
    void bar( int a[] )
    {
        ...
    }
    ...
    int array[ 1000 ];
    ...
    foo( array );
    See also: Arrays and Pointers


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

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Nice!, so how do I "return" an array? Got everything else working.

    Code:
    #include "stdafx.h"
    
    
    #define size 100
    
    
    int calcPrimes(int arr[size]);
    int countPrimes(int arr[size]);
    void printPrimes(int arr[size],int,int);
    
    
    int main()
    {
    	
    /* Declaration of Variables */
    	int arr[size],primecount;
    
    
    	arr[size]=calcPrimes(arr);
    	primecount=countPrimes(arr);
    	printPrimes(arr,primecount,size);
    
    
    return 0;
    }
    
    
    /* Function to fill the array and calculate primes */
    int calcArray(int arr[size]){
    	int j=1,i,remainder;
    
    
        for(i = 0;i < size; i++)
        {
          arr[i] = j++;
        }
    
    
    	for(j = 2; j <= size; j++)
    	{
    		for(i = j; i < size; i++)
    		{
    		  remainder = arr[i] % j;
    
    
    		  if(remainder == 0)
    		     arr[i] = 0;
    		}
        }
     return arr;
     }
    
    
    
    
    /* Count the prime numbers in the array */
    int countPrimes(int arr[size]){
    		
    		int primecount=0;
    		for(int i=0;i<size;i++){
    		if (arr[i]!=0)
    			primecount++;
    	}
    return primecount;
    }
    
    
    /* Print the amount of prime numbers calculated. */
    void printPrimes(int arr[size], int primecount, int max){
    
    
        for(int i = 0; i < size; i++)
        {
    	if (arr[i]!=0)
    		printf("%d ",arr[i]);
        }
    		printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);
    	}

    Currently getting error message: "error C2440: 'return' : cannot convert from 'int []' to 'int'" What does my function type need to be?
    Last edited by Lej1; 03-10-2012 at 01:15 AM.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You don't need to return anything at all. What you're actually passing to the calcArray function is a pointer to the beginning of the array, together with the size of the array.

    That means that when you modify the contents of the array, you're modifying the original rather than a copy as you might expect.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    Ah! makes sense. So I've changed the function type to void, but it's still not working.

    Code:
    #include "stdafx.h"
    
    
    #define size 100
    
    
    void calcPrimes(int arr[size]);
    int  countPrimes(int arr[size]);
    void printPrimes(int arr[size],int,int);
    
    
    int main()
    {
        
    /* Declaration of Variables */
        int arr[size],primecount;
    
    
        calcPrimes(arr);
        primecount=countPrimes(arr);
        printPrimes(arr,primecount,size);
    
    
    return 0;
    }
    
    
    /* Function to fill the array and calculate primes */
    void calcArray(int arr[size]){
        int j=1,i,remainder;
    
    
        for(i = 0;i < size; i++)
        {
          arr[i] = j++;
        }
    
    
        for(j = 2; j <= size; j++)
        {
            for(i = j; i < size; i++)
            {
              remainder = arr[i] % j;
    
    
              if(remainder == 0)
                 arr[i] = 0;
            }
        }
    
    
     }
    
    
    
    
    /* Count the prime numbers in the array */
    int countPrimes(int arr[size]){
            
            int primecount=0;
            for(int i=0;i<size;i++){
            if (arr[i]!=0)
                primecount++;
        }
    return primecount;
    }
    
    
    /* Print the amount of prime numbers calculated. */
    void printPrimes(int arr[size], int primecount, int max){
    
    
        for(int i = 0; i < size; i++)
        {
        if (arr[i]!=0)
            printf("%d ",arr[i]);
        }
            printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);
        }

    "error LNK2019: unresolved external symbol "void __cdecl calcPrimes(int * const)" (?calcPrimes@@YAXQAH@Z) referenced in function _main"

    EDIT: Oh wow.. Nevermind. I see what I did there.. it was a tough one to spot
    Last edited by Lej1; 03-10-2012 at 01:55 AM.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you ever get this error:

    error LNK2019: unresolved external symbol
    It means you told the compiler you will be providing something (usually via a function prototype), but you either did not provide that (usually a function) at all, or how you provided it differs from what you told the compiler you'd provide. That could be because of a misspelling/typo, or different arguments or return type.

    You know the problem, but to clarify for future generations. You told the compiler here:

    Code:
    void calcPrimes(int arr[size]);
    "I am going to provide a function later called calcPrimes which takes a pointer to an integer (because that's how an array is seen by the compiler here) and returns nothing"

    When the compiler gets here:

    Code:
    calcPrimes(arr);
    it says, "OK, this was prototyped. The linker will provide the actual function, so I'll just stick a stub here for the linker to find and insert the function definition"

    So the program compiles OK. But then the linking step occurs, the linker finds this stub for a function as prototyped, and can't find it (because you named it calcArray), and you get this linker error.

    For more info, Compiling and Linking - Cprogramming.com and An Introduction to GCC - An overview of the compilation process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-06-2008, 05:26 PM
  2. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM
  3. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM