Help in Computer Language C homework!
1. Create a primary array that can hold 100 integers.
2. Use the file 'input_numbers.txt' to populate the primary array in your program. You can obtain this file from github.com. See the Lab Supplement- Using Git for more information. The repository you want to use is located on the server github.com, and the path to the repository is /untamedspud/october2019lab9.
3. Read through the file, inserting each integer into the primary array in ascending order as you read them in. So if you read in 40, your array is 40, Then you read in 10, you should have in your array 10 40, and then you read in 20, your array should be 10 20 40
4. Use a function insert_in_array_ascending( ) to insert each value read in from the file into the primary array. This function is responsible for keeping the array ordered as each individual value is inserted.
5. Write a function print_int_array( ) which takes two parameters, an integer array, which will be our primary array, and a count of the number of elements to be printed. After reading in each number and storing it in the array, call print_int_array( ) to display the array so far.
6. Write an interactive function get_value_from_user( ) which prompts the user for a number that may or may not be found in the primary array. Test the numbers [40 41 42 43 7 8 9 10 54 50]
7. Write a function is_in_array( ) which returns true if a value passed in as a parameter exists in the array, and false otherwise. This function should use a binary search to find the value.
input_numbers.txt
40, 4, 18, 33, 2, 49, 22, 39, 19, 3 17, 54, 57, 48, 5, 21, 27, 9 42, 2, 43
test_numbers.txt
40, 41, 42, 43, 7, 8, 9, 10, 54, 50
The code below will satisfy all the requirements of the program. THUMBS UP please if it helps.
CONTENT OF input_numbers.txt

main.c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// to print array
void print_array(int arr[],int count)
{
int i;
for(i = 0; i < count; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// to sort array
void Sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
// to insert in array
int insert_array_in_ascending(int arr[])
{
FILE *fs;
char ch, buffer[32];
int i = 0, j = 0;
fs = fopen("input_numbers.txt", "r");
while(1){
ch = fgetc(fs);
if(ch == EOF){
break;
}
else if(ch == '\n'){
arr[j] =
atoi(buffer);
j++;
bzero(buffer,
32);
i = 0;
Sort(arr,j);
print_array(arr,j);
// then
continue
continue;
}
else{
buffer[i] =
ch;
i++;
}
}
return j;
}
// to Search array for number
bool binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return true;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return false;
}
//arr is the primary array
int main(){
int arr[100],count,num,i,j=10;
count = insert_array_in_ascending(arr);
for( i =0;i<j;i++)
{
printf("\nEnter a Number to Check : ");
scanf("%d",&num);
bool result = binarySearch(arr, 0, count - 1, num);
(!result) ? printf("Element is not present in array")
: printf("Element is present inside the array");
}
}
OUTPUT:


All the test numbers have been checked.
NOTE : Remove the for loop that is prompting user to enter value 10 times i used it for checking all the test Cases.
THUMBS UP!
Help in Computer Language C homework! 1. Create a primary array that can hold 100 integers....