Thread: function passing argument..array ?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07:18 AM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM