function passing argument..array ?

This is a discussion on function passing argument..array ? within the C Programming forums, part of the General Programming Boards category; question given Finding the Smallest Value (small.c) (a) Write a function, smallest( ), that returns the smallest value in a ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    14

    function passing argument..array ?

    question given

    Finding the Smallest Value (small.c)
    (a) Write a function, smallest( ), that returns the smallest value in a signed integer
    array. The array and its size are passed as arguments.

    (b) Write a main program that inputs MAX values from the keyboard into a signed
    integer array, array
    , and prints, using smallest( ), the smallest value to the screen



    hi all...
    i really new for function especially those passing argument...
    as the code written below, workable but seens like different from the question'requirement....

    i did read book, but not really clear..therefore post and ask for guidance...
    question mentioned a lot of array+ & argument.

    FOr the written code below, can my smallest() function considered as passing argument? cause i return k..

    for array if passing argument sure related to pointer?

    thanks ...

    Code:
    /*Small.c*/
    
    #include<stdio.h>
    
    
    smallest();
    
    int n,i,y,h;
    int max[60];
    
    main()
     {
    
    	for(i=0;i<4;i++)
    	{
    	printf("Please enter number:");
    	scanf("%d", &max[i]);
    	/*printf("%d", max[i]);  test for input */
    	}
    	h=smallest();
    
    	printf("minimum no is %d",h);
    	getch();
    
    	}
    smallest()
     {
    
    	int j,k;
    
    
    	for(j=0;j<4;j++)
    	{
    	k=((k>max[j])?max[j]:k);
    	/*conditioner operator:If y>max[i],=max[i], else=y */
    	printf("%d\n",k);
    
    
    	}
    	printf("%d",k);
    	return k ;
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    FOr the written code below, can my smallest() function considered as passing argument? cause i return k..
    No, that is the return value. More likely I would expect a function prototype such as:
    Code:
    int smallest(int numbers[], int size);
    for array if passing argument sure related to pointer?
    Yes, an array decays to a pointer when passed as an argument. This is why you should pass the size as an argument, as sizeof(array) would return you the size of the pointer instead of the size of the array.

    Of course, in your example you are using global variables, and hence do not pass any arguments at all. This is generally a Bad Thing. Make your currently global variables local to main() (which should be declared as returning an int) or smallest(), as necessary.
    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

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    14
    thanks a lot...

    really not familiar with passing pointer/argument, but i had re-arrange the structure..
    but i still not sure will i declare wrongly
    for the function prototype, call function in main() & funtion ??

    for function prototype & function , there are written all the same right?

    example=int smallest(int num[], int size);
    Code:
    
    
    #include<stdio.h>
    
    #define MAX  4
    
    int smallest(int num[], int size);
    
    main()
     {
    	int n,i,y,h;
    	int num[20];
    
    	for(i=0;i<MAX;i++)
    	{
    	printf("Please enter number:");
    	scanf("&#37;d", &num[i]);
    	/*printf("%d", num[i]);  test for input */
    	}
    	
    	h=smallest(num,sizeof num);
    	printf("The smallest number is %d",h);
    
    	getch();
    
    }
    	
    int smallest(int *num,int size)
     {
    
    	int j,k;
    
    	for(j=0;j<MAX;j++)
    	{
    	k=((k>num[j])?num[j]:k);
    	
    
    				/*conditioner operator:If y>max[i],=max[i], else=y */
    	printf("%d\n",k);   \\test
    
    
    	}
    	printf("%d",k);   \\test
    	return k ;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Problems passing an array to a function
    By ndenklau in forum C Programming
    Replies: 5
    Last Post: 09-20-2006, 08:14 AM
  3. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 06:18 AM
  4. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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