Thread: Help needed urgently!

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Help needed urgently!

    Hi
    I have an assignment which requires me to do the following:

    Required to write a function that finds an integer in an array and returns
    its corresponding index. The function must be called findNumber. It must have FOUR parameters:
     The first parameter is the array to be searched
     The second parameter is the integer to be found within the array
     The third parameter is the size of the array
     The fourth parameter is an integer that indicates whether the array is sorted. A value of 1
    means the array is sorted; a value of zero means the array is not sorted.


    __________________________________________________ __

    Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.

    This is my code : (It compiles perfectly but it does not produce any outputs)

    Code:
    #include <stdio.h>
    #define SIZE 10
    
    size_t findNumber(int *sort, const int array[],int key,size_t size);
    
    int main(void){
        int a[SIZE];
        size_t x;
        int search_key;
        size_t element;
        int isSorted=0;
    
       
    
        for (x = 0;x < SIZE; ++x){
            a[x]=2*x;
        }
    
        puts("Enter the integer you are looking for:");
        scanf_s("%d", &search_key);
    
        element= findNumber(&isSorted,a,search_key,SIZE);
       
        if (isSorted==1) {
                       printf("Array is sorted");
        }
        else{
            printf("Array is not sorted");
        }
                           
                      
    
        printf("Your value has been found in element %d  \n",element);
    
       
    
        return 0;
       
    
    }
    
    size_t findNumber(int *sort,const int array[],int key,size_t size){
        size_t n;
       
    
            for (n = 9; n < SIZE; --n){
            if (array[n] >= array[n-1] || array[n] <= array[n-1] ){
                *sort=1; }
                   
                *sort=0;
            }
          
            for (n = 0; n < size; ++n) {
            if (array[n] == key) {
                return n;
            }
           
                return -1;
            
        }
    
    }

    ______________________________________

    Will appreciate any help to help solve this problem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_code
    Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.
    You read your requirements wrongly. The function only has one result, i.e., the return value that is the index of the integer in the array (though there is no mention of what happens if the integer is not in the array).

    Therefore, the fourth parameter (note that your function is currently declared with the parameters in a wrong order) is presumably for the function to decide whether to use binary search or linear search.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Algorithms In C++ (Urgently needed)
    By gameuser in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2009, 04:59 AM
  2. Help urgently needed, please =(
    By *Michelle* in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-03-2003, 07:14 AM
  3. need help please urgently
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 12-12-2001, 08:32 AM
  4. help urgently needed!
    By meys in forum C Programming
    Replies: 0
    Last Post: 10-15-2001, 12:17 PM
  5. help urgently needed!
    By meys in forum C Programming
    Replies: 3
    Last Post: 10-12-2001, 04:11 AM

Tags for this Thread