-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_assignment6.c
More file actions
76 lines (67 loc) · 1.57 KB
/
lab_assignment6.c
File metadata and controls
76 lines (67 loc) · 1.57 KB
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
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
int search(int numbers[], int low, int high, int value)
{
if (low > high) {
return -1; // Returns false when low is greater than high which is a condition in which the value being searched is not present in the list
}
int mid = (high + low) / 2;
if (value == numbers[mid]) {
return mid;
}
if (value > numbers[mid]) { // value is on the right side of the mid
mid++;
return search(numbers,mid,high,value);
} else {
mid--; // value is on the left side
return search(numbers,low,mid,value);
}
return -1;
}
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);
}