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){
}