can someone tell me why this isnt working. When I execute it, it says, "Unhandled exception at 0x00411589 in Joiner_test2c.exe: 0xC00000FD: Stack overflow"
The problem is, Write a function that verifies if a given number exists in an array of floats. The function is supposed to return the first position in where the number is encountered. If the given number does not exist, the function returns –1.Then write a program that asks the user to enter an array of floats and calls the function.
Code:#include <stdio.h> #include <stdlib.h> #include <math.h> float* fillArray(int numElements); int checkArray(float* arrayM, int numElements); int main() { float* arrayM; int numElements; int position; printf("Enter the number of floats in the array: "); scanf_s("%d",&numElements); arrayM = fillArray(numElements); if (position >= 0) printf("The first occurance of the number is in element number: %d\n", position); else printf("The number is not in the array entered.\nTry increasing precision.\n"); getchar(); return 0; } float* fillArray(int numElements) { int x; float* arrayM = fillArray(numElements); printf("Begin entering floating point values...\n"); for(x = 0; x < numElements; x++) { printf("Element %d: ", x); scanf("%f", (arrayM+x)); putchar('\n'); } printf("Filling of array completed.\n"); return arrayM; } int checkArray(float* arrayM, int numElements) { float searchNum; float precision; int position = -1; int x; printf("\nEnter the number you would like to search for: "); scanf("%f", searchNum); printf("\nEnter the precision you would like."); printf("\nEnter 0 for exact values (not recommended)."); scanf("%f", precision); for(x=0; x < numElements; x++) { if (fabs(*(arrayM+x)-searchNum) <= precision) { return x; } } return x; }
Last edited by ZekeDragon; 03-21-2010 at 02:44 PM. Reason: Please use [code] tags (the # button) when posting code.
Endless recursion FTW. well until there is no more stack
You see, when a function is called, space on stack is allocated for the called function. What you got here is a function that only calls itself with no halting condition, so what happens is that it allocates room on stack and doesn't return... So since the stack is not endless, you get what you get..
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks