mt213 C Language. Nothing too complicated Part 1: Write the function definition
ID: 3579045 • Letter: M
Question
mt213
C Language. Nothing too complicated
Part 1: Write the function definition for a function named findMe that: Takes as input parameters: a pointer to an integer array, the length of the array, and an integer to find in the array Returns TRUE (1), if the integer is found in the array or FALSE (0) if the integer is not found. There should be no printf or scanf statements in this function.
Part 2: Assume an integer array named randomArray with length 500 is populated with random data. Determine whether the value 123 is in the array with a function call to your function. Display the result with a printf statement: Ex: "Integer 123 was found in the array." or "Integer 123 was not found in the array."
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int findme(int *array,int length,int x)
{
int i,ret;
ret=0;
for(i=0;i<length;i++)
{
if(array[i] == x)
{
ret = 1;
break;
}
}
return ret;
}
int main(void)
{
int randomArray[500],i,ret;
for(i=0;i<500;i++)
{
randomArray[i] = rand()%100 +100; //random number in the range 100 to 200
//printf("%d ",randomArray[i]);
}
ret =findme(randomArray,500,123);
if(ret == 1)
printf(" Integer 123 was found in the array.");
else
printf(" Integer 123 was not found in the array.");
return 0;
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.