-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_assignment_6.c
72 lines (64 loc) · 1.3 KB
/
lab_assignment_6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
int search(int numbers[], int low, int high, int value)
{
if (low > high) {
return -1;
}
int mid = (low + high) /2;
if (numbers[mid] == value) {
return mid;
}
if (numbers[mid] < value) {
return search(numbers, mid + 1, high, value);
} else {
return search(numbers, low, mid -1, value);
}
}
void printArray(int numbers[], int sz)
{
int i;
printf("Number array : ");
for (i = 0;i<sz;++i)
{
printf("%d ",numbers[i]);
}
printf("\n");
}
int main(void)
{
int i, numInputs;
char* str;
float average;
int value;
int index;
int* numArray = NULL;
int countOfNums;
FILE* inFile = fopen("input.txt","r");
fscanf(inFile, " %d\n", &numInputs);
while (numInputs-- > 0)
{
fscanf(inFile, " %d\n", &countOfNums);
numArray = (int *) malloc(countOfNums * sizeof(int));
average = 0;
for (i = 0; i < countOfNums; i++)
{
fscanf(inFile," %d", &value);
numArray[i] = value;
average += numArray[i];
}
printArray(numArray, countOfNums);
value = average / countOfNums;
index = search(numArray, 0, countOfNums - 1, value);
if (index >= 0)
{
printf("Item %d exists in the number array at index %d!\n",value, index);
}
else
{
printf("Item %d does not exist in the number array!\n", value);
}
free(numArray);
}
fclose(inFile);
}