Thread: help on pointers. - return pointer to largest element in array

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    Exclamation help on pointers. - return pointer to largest element in array

    Morning all.

    question from chapter 11, qn 8 modern C programming by king
    Write the following function:
    Code:
    int *find_largest(int a[], int n);
    When passed an array a of length n, the function will return a pointer to the array's largest element
    my solution
    Code:
    #include <stdio.h>
    
    int *find_largest(int a[], int n)
    {
    	int i, x;
    
    	x = 0;
    	for (i = 0; i < 6; i++)
    	{
    		if (a[i] > x)
    			x = a[i];
    	}
    
    	for (i = 0; i < 6; i++)
    	{
    		if (a[i] == x)
    			return &a[i];
    	}
    }
    
    
    int main()
    {
    	int b[6], i, *p;
    
    	for (i = 0; i < 6; i++)
    		scanf("%d", &b[i]);
    
    	p = find_largest(b,6);
    
    	printf("%d", *p);
    
    	getchar();
    	return 0;
    }
    I have traced the code line by line and it prints correctly up to line 31. However, when the program exits (line 35) , it goes to some other screen with very complex code and my output disappears.

    Just to clarify: If I just run the code as is, there is no output. However, when I trace the code line by line, the output appears, but then disappears as the program exits
    My input was 1 2 3 4 5 6
    Last edited by bos1234; 09-26-2013 at 08:12 AM. Reason: clarification

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Except of this
    Code:
    main.c: In function `find_largest':
    main.c:19: warning: control reaches end of non-void function
    the rest seems ok.

    I also run it and works fine for me.

    I do not see the reason of the getchar() at the end, so remove it.
    Last edited by std10093; 09-26-2013 at 08:06 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    hmmm

    if I press F5 to debug and set no break points, there is no output for me. I get this warning:
    Warning 2 warning C4715: 'find_largest' : not all control paths return a value 19 1 testbed
    I am assuming that is for the cases when a[i] < x or a[i] == x (line 10 and 16). But that should be no problem..

    My input was 1 2 3 4 5 6

    I am now googling the warning you have received. Maybe same thing

    thanks

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It's the very same thing.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Why are you looping through the array twice?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Your find_largest function does not make use of the parameter "n".
    2. Your find_largest function breaks if your array contains only negative numbers.


    Pseudo-code (1 loop pass):
    1. Assign a pointer to first element of array
    2. Loop through array from index [2,n)

      • If value at current array index is greater than what the pointer points to then update the pointer to reference the new max value
    3. Return the pointer
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    solved.

    Quote Originally Posted by hk_mp5kpdw View Post
    1. Your find_largest function does not make use of the parameter "n".
    2. Your find_largest function breaks if your array contains only negative numbers.


    Pseudo-code (1 loop pass):
    1. Assign a pointer to first element of array
    2. Loop through array from index [2,n)

      • If value at current array index is greater than what the pointer points to then update the pointer to reference the new max value
    3. Return the pointer
    thank you.
    New code below
    Code:
    #include <stdio.h>
    
    int *find_largest(int a[], int n)
    {
    	int i, *x;
    
    	x = &a[0];
    	for (i = 1; i < n; i++)
    	{
    		if (a[i] > a[i-1])
    			x = &a[i];
    	}
    	
    	return x;
    }
    
    int main()
    {
    	int b[4], i, *p;
    
    	for (i = 0; i < 4; i++)
    		scanf("%d", &b[i]);
    
    	p = find_largest(b,4);
    
    	printf("%d", *p);
    
    	getchar();getchar();
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    144

    alternate solution - which one is better?

    Quote Originally Posted by bos1234 View Post
    thank you.
    New code below
    Code:
    #include <stdio.h>
    
    int *find_largest(int a[], int n)
    {
    	int i, *x;
    
    	x = &a[0];
    	for (i = 1; i < n; i++)
    	{
    		if (a[i] > a[i-1])
    			x = &a[i];
    	}
    	
    	return x;
    }
    
    int main()
    {
    	int b[4], i, *p;
    
    	for (i = 0; i < 4; i++)
    		scanf("%d", &b[i]);
    
    	p = find_largest(b,4);
    
    	printf("%d", *p);
    
    	getchar();getchar();
    	return 0;
    }
    another solution. Not sure which one is better? Line 10 is where the change has been made
    Code:
    #include <stdio.h>
    
    int *find_largest(int a[], int n)
    {
    	int i, *x;
    
    	x = &a[0];
    	for (i = 1; i < n; i++)
    	{
    		if (a[i] > *x)
    			x = &a[i];
    	}
    	
    	return x;
    }
    
    int main()
    {
    	int b[4], i, *p;
    
    	for (i = 0; i < 4; i++)
    		scanf("%d", &b[i]);
    
    	p = find_largest(b,4);
    
    	printf("%d", *p);
    
    	getchar();getchar();
    	return 0;
    }

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The second one is better because the first one is wrong. Try entering 6 1 2 3. The first one will print 3.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    • You should be thankful to oogabooga for spotting the logical error for you.
    • Never leave a program, if you are not sure it's completely correct.
    • I think you know why the first code does not work (because you are testing only adjacent elements).
    • Why name max as x? Wouldn't it better to give a name to the variable that completely describes its role in your program? Name "x" as "max" is my suggestion.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by std10093 View Post
    • Name "x" as "max" is my suggestion.
    I wouldn't. My suggestions is agent86.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    Name "x" as "max" is my suggestion.
    I agree with you on being clear in naming variables, but I wouldn't suggest "max" as a name. There are a few too many implementations that define macros named min() and max() in one or more of the standard headers. Admittedly, it's not standard, but it occurs often enough with some well-known compilers/libraries that names min and max are best avoided. The error messages emitted by compilers in such cases are usually rather cryptic as well (as are most errors resulting from an unwanted macro expansion).

    I'd probably use a name like "largest" or "maximum" or something like that. Apart from being less ambiguous (is max for maximum, or the name of a soft drink made by pepsi?), it's less likely to run afoul of non-standard but common extensions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get the largest element from the array and print it
    By casperghost in forum C Programming
    Replies: 2
    Last Post: 12-27-2009, 06:22 PM
  2. pointer to first element in array
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02:52 AM
  3. Finding largest element in array
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2005, 09:18 PM
  4. How to return the last element of an array?
    By BuezaWebDev in forum C Programming
    Replies: 20
    Last Post: 03-26-2005, 10:57 AM
  5. pointer to the second element of 2-dim array
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-23-2003, 01:34 PM