Closed Thread
Results 1 to 2 of 2

Thread: Help with C++ please

  1. #1
    january is offline Newbie
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Help with C++ please

    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("&#37;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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Help with C++ please

    Quote Originally Posted by january View Post
    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;
    }
    [/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..

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts