I am having this problem in multiple programs using pointers. The logic in the secondary function is correct but the answers come out wrong. If you can spot something not right, please help. I am using Visual Studio 2013 C++
The program is supposed to have a user input the length of an array and input the numbers used. Then the second function should find the largest and second to largest numbers in the array.
The code:
//program_homework_009_page_255_number_6_two_largest
//Purpose: To have a user input the numbers of an array and return the largest and second to largest number.
#include <stdio.h>
void find_two_largest(int a[], int n, int *largest, int *second_largest);
int main()
{
int x[100];
int y;
int big; //second to biggest
int bigger; //biggest
int i;
printf("\n Please input the desired length for array x:");
scanf_s("%d", &y);
for (i = 0; i <= y - 1; i++)
{
printf("\n Please enter the %d number for array x: ", i + 1);
scanf_s("%d", &x[i]);
}
find_two_largest(x, y, &bigger, &big);
printf("\n The largest number in your array is: %d, and the second to largest it:%d", bigger, big);
getchar();
getchar();
return 0;
}
void find_two_largest(int a[], int n, int *largest, int *second_largest)
{
int i;
*largest = a[0];
*second_largest = a[0];
for (i = 0; i <= n; i++)
{
if (a[i] < *largest)
{
*largest = a[i];
}
if (a[i] < *second_largest < *largest)
{
*second_largest = a[i];
}
}
}