Morning all.
question from chapter 11, qn 8 modern C programming by king
my solutionWrite the following function:
When passed an array a of length n, the function will return a pointer to the array's largest elementCode:int *find_largest(int a[], int n);
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.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; }
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