The problem is, Write a function that verifies if a given number exists in an array of floats. The function is supposed to return the first position in where the number is encountered. If the given number does not exist, the function returns –1.Then write a program that asks the user to enter an array of floats and calls the function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float* fillArray(int numElements);
int checkArray(float* arrayM, int numElements);
int main()
{
float* arrayM;
int numElements;
int position;
printf("Enter the number of floats in the array: ");
scanf_s("%d",&numElements);
arrayM = fillArray(numElements);
if (position >= 0)
printf("The first occurance of the number is in element number: %d\n", position);
else
printf("The number is not in the array entered.\nTry increasing precision.\n");
getchar();
return 0;
}
float* fillArray(int numElements)
{
int x;
float* arrayM = fillArray(numElements);
printf("Begin entering floating point values...\n");
for(x = 0; x < numElements; x++)
{
printf("Element %d: ", x);
scanf("%f", (arrayM+x));
putchar('\n');
}
printf("Filling of array completed.\n");
return arrayM;
}
int checkArray(float* arrayM, int numElements)
{
float searchNum;
float precision;
int position = -1;
int x;
printf("\nEnter the number you would like to search for: ");
scanf("%f", searchNum);
printf("\nEnter the precision you would like.");
printf("\nEnter 0 for exact values (not recommended).");
scanf("%f", precision);
for(x=0; x < numElements; x++)
{
if (fabs(*(arrayM+x)-searchNum) <= precision)
{
return x;
}
}
return x;
}
Edited by ZekeDragon, 21 March 2010 - 01:44 PM.
Please use [code] tags (the # button) when posting code.


Sign In
Create Account

Back to top









