Thread: Can someone help me with an assignment

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Unhappy Can someone help me with an assignment

    I kinda dont understand how to create functions with arrays :/

    Code:
    You are to write a single C program that creates a one dimensional array of size MAX(to be declared as a declarative constant  using directive 
    
    #define MAX 10)
    
    
    ,  initializes its  elements  with  a given  value using the function 
    InitArray, then populate the array with some random numbers between 1 and MAX inclusive, and finally search the array for certain keys. 
    
    The structure for this C program is as follows:
    
    
    
    int main()
    
    {
    
    
    
    //array A to be locally declared here
    
    
    
    i
    nt 
    size = MAX;
    
    
    // note: actual size used can be less than 
    
    // 
    MAX memory allocation
    
    
    // populate the array with a number from the user between 0-10
    
    
    
    
    InitArray(A, size, GetNumber(0, 10));
    
    
    
    PrintArray(A, size);
    
    
    // to verify the output
    
    ...
    
    ...
    
    ...
    
    
    
    
    Return 0;
    
    }
    
    
    
    List of function:
    
    GetNumber
    This function prompts the user to enter an integer value that will be used to fill up the array elements
    
    
    (array initialization). The inputted value will be validated to be within the range of min..max. The valid entry will be returned
    
    .
     
    
    
    
    InitArray
    
    
    This  functionuses  the  value  returned  from  GetNumber  to  initialize  all  array  elements with.  The  input parameters of this function are: name of the array, size of the array and the initialization value. It does not return a value.
    
    
    
    PrintArray
    
    
    Thisis a void function that accepts the name and size of array as two input parameters and prints the contents of the array.
    
    
    
    PopulateArray
    
    This function is void return and it fills array elements with random
    
    values in the range of 1 .. MAX/2. It has two input parameters which are the name and size of the array.
    
    
    
    SearchArray
    
    This function uses a linear search to look for a certain key (value) in the array elements and returns the number of occurrence(s) for that key among the array elements. It returns zero if no matching value 
    
    was found.
    
    
    
    
    
    Sample output
    
    
    of the program
    :
    
    MAX=10
    
    
    Please enter an integer between 0and 10:  11
    
    
    You have entered an invalid entry!
    
    Please  enter an  integer between 0 and  10: 7
    
    
    /*generated  by GetNumber*/
    
    
    /* all elements of array A are initialized to 7 by InitArray */
    
    
    
    
    A=[7,7,7,7,7]                //generated by PrintArray //
    call PopulateArray
    
    
    
    A=[1,5,5,4,3]// generated by PrintArray
    
    
    
    // call SearchArray
    
    Please enter a key between 1 and 5: 4
    
    
    
    No occurrence found for value 4
    
    
    in the array! 
    /* generated by a invoking SearchArray for key=4*/
    
    
    
    Do you want to search for more
    
    keys (Y/N)? 
    Y
    
    Please enter a key between 1 and 5: 5
    
    
    
    2 occurrences found for value 5 in the array! /* generated by a invoking SearchArray for key=5*/
    
    
    Do you want to search for more keys (Y/N)? 
    N
    
    Goodbye!
     
    
    this is what i have
    Code:
    #include <stdio.h>
    #define MAX 10
    
    int GetNumber(int min, int max);
    int InitArray(int A, int size, int GetNumber(int min, int max));
    int PrintArray(int A, int size);
    int PopulateArray();
    int SearchArray();
    
    int main()
    {
        
    }
    
    int GetNumber(int one, int two){
        int ans;
        scanf ("%d", &ans);
        
        if (ans >= int min && ans <= int max){
            return ans;
        }
        if (ans > int max || ans < int min){
            printf ("Invalid Entry\n");
            break;
        }
    }
    
    int InitArray(int A, int size, int GetNumber(int min, int max) ){
        int MAX = size
        int result;
        result = GetNumber (0,10);
        int A[size] = {result}; // make it 10 slots not 1
        return 0;
    }
    
    int PrintArray (int A, int size){
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if (ans >= int min && ans <= int max)
    This is not the correct way to do an if statement. It should be:
    Code:
    if (ans >= min && ans <= max)
    Just make sure min and max are declared somewhere (in your case, they are not).

    Code:
    int InitArray(int A, int size, int GetNumber(int min, int max)
    This is not a valid function declaration. This should be:
    Code:
    int InitArray(int A, int size, int value)
    Not that your assignment was giving an example call to InitArray, that was not the prototype.
    Last edited by bithub; 06-12-2013 at 06:06 PM.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Ty, what else should i include

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tmand6 View Post
    Ty, what else should i include
    int main() function should (as the int indicates), return an int. Same problem with int GetNumber().

    In main() you need to call at least one other function. An empty main() will do nothing.

    Work through your functions one at a time, and test them. See if they work as they should. Refer to the assignment and make sure it's doing exactly what is requested.

    If you find a problem, post back, and include your most recent code so we can see what you're talking about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on my Assignment Please :C
    By l2krence in forum C Programming
    Replies: 2
    Last Post: 11-03-2010, 10:31 PM
  2. C Assignment Help
    By shelby115 in forum C Programming
    Replies: 1
    Last Post: 10-13-2010, 12:31 PM
  3. S2 assignment
    By pelisa in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2010, 11:48 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. First assignment help
    By FirstC++ in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2004, 07:57 AM