Help needed urgently!

This is a discussion on Help needed urgently! within the C Programming forums, part of the General Programming Boards category; Hi I have an assignment which requires me to do the following: Required to write a function that finds an ...

  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
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:59 AM
  2. Help urgently needed, please =(
    By *Michelle* in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-03-2003, 08: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, 01:17 PM
  5. help urgently needed!
    By meys in forum C Programming
    Replies: 3
    Last Post: 10-12-2001, 05:11 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21