Basic C program
Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and then write the output into another file (out.txt) in the same format, i.e., each line contains one coordinate point and the value of x and y are space delimited. After writing the sorted points in the file, your program should display “sorted and output written”. After that, your program should prompt the user for an input coordinate point x and y values and your program should perform a binary search to determine whether the point exists in the given list of points. If it is found it should show “Found and record number”.
Code Image:
Input File Image:
Sample Output:
Run 1:
Run 2:
Output File Image:
Code to Copy:
#include <stdio.h>
#include <stdbool.h>
//Implementation of struct type
//of data structure to store x and y
struct coordinatePoints {
//Declare x and y coor
int x, y;
};
//Implementation of isSmallCoordinate function
// to check if the point p is smaller than point q geometry wise.
bool isSmallCoordinate(struct coordinatePoints point1, struct coordinatePoints point2) {
//check whether first coordinate of point1.x is less than point2.x
if (point1.x<point2.x){
return true;
}
// check if the x coordinate is equal than smaller
// than the y coordinate is selected or not .
else if (point1.x == point2.x){
return point1.y<point2.y;
}
else {
return false;
}
}
//Implementation of merge function
void arrayElementsmerge(struct coordinatePoints arrPoint[], int l, int midElement, int r) {
int numberofElements1 = midElement - l + 1;
int numberofElements2 = r - midElement;
//Declare i,j,k as type of integer
int i, j,k;
struct coordinatePoints left[numberofElements1], right[numberofElements2];
// copying both elements the subparts
for (i = 0; i<numberofElements1; i++) {
left[i] = arrPoint[l + i];
}
//Iterate the loop
for (k = 0; k<numberofElements2; k++) {
right[k] = arrPoint[midElement + 1 + k];
}
//Initailze the i and j
i = 0;
j = 0;
int count = l;
//Iterate the loop to sorted merging for both
//left and right parts
while (i<numberofElements1 && j<numberofElements2) {
//call isSmallCoordinate function to
//check which coordinate is smaller
if (isSmallCoordinate(left[i], right[j])) {
arrPoint[count++] = left[i++];
}
else {
arrPoint[count++] = right[j++];
}
}
//Iterate the loop fill in the remaining values.
while (i<numberofElements1) {
arrPoint[count++] = left[i++];
}
//Iterate the loop
while (j<numberofElements2) {
arrPoint[count++] = right[j++];
}
return;
}
//Implementation of mergeSort function
void mergeSort(struct coordinatePoints a[], int left, int right) {
int midValue;
//check left value is greater
//then right
if (left >=right)
{
return;
}
midValue = (left + right) / 2;
//call recursively mergeSort function for left half of elements
mergeSort(a, left, midValue);
//call recursively mergeSort function for right half of elements
mergeSort(a, midValue + 1, right);
//call merge method to merge both values
arrayElementsmerge(a, left, midValue, right);
return;
}
int main() {
//Declare n,i,p,q ,l , r as type of integer
int n, i, p, q, l, r;
//open the in.txt in read mode
FILE *openfile= fopen("in.txt", "r");
fscanf(openfile, "%d", &n);
//Declare struct type of array that stores
// the points.
struct coordinatePoints arr[n];
for (i = 0; i<n; i++) {
fscanf(openfile, "%d %d", &p, &q);
arr[i].x = p;
arr[i].y = q;
}
//call mergeSort function
mergeSort(arr, 0, n - 1);
// Writing the contents into output to a file
//named as out.txt.
FILE *outfile = fopen("out.txt", "w");
//Iterate the loop
for(i=0;i<n;i++){
fprintf(outfile,"%d %d\n",arr[i].x,arr[i].y);
}
//close the file
fclose(outfile);
//Display statement
printf("Sorted and Output written\n");
// search process through
//binary search.
printf("Search input(x,y): ");
//Get the input from user
scanf("%d %d", &p, &q);
//Declare input as type coordinatePoints
struct coordinatePoints input;
input.x = p;
input.y = q;
l = 0;
r = n - 1;
//Iterate the loop
while (l<r) {
//Get the midPosition value
int midPosition = (l + r) / 2;
//check is there any small coordinate exists
if (isSmallCoordinate(arr[midPosition], input)) {
l = midPosition + 1;
}
else
{
r = midPosition ;
}
}
//check arr[l] is either equal to input
//if input lies in the list of points or it is greater than input .
if (arr[l].x == input.x && arr[l].y == input.y) {
//Display statement
printf("Output:Found at record %d\n", l + 1);
}
else {
//Display statement
printf("Output:Not found\n");
}
return 0;
}
Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...
The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...
help me Problem: Write a simple C program that calculates the corresponding y-coordinate for any x-coordinate onaline defined by two points. These points areinputspecifiedas pairsofxandycoordinates in the two-dimensional Cartesian coordinate system. Youneed to determine the slope ofthe line and they-intercept for the line plotted by these two points and store this data in memory. The programmustpromptthe userforthe coordinates of the first point X1 and then Y1, and then prompt the user for the second point, X2 and then Y2. After...
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...
This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...
Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...
C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...
Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...
Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...
pythonComplete the below function that takes the name of two files, inFilename andoutFilename as arguments and reads the text from the inFilename. In this fileeach line contains the Turkish Republic Identity Number (TCNO), name, surnameand telephone number of a person. Your function should sort all personsaccording to their TCNO, write the sorted data into outFilename. If the fileinFilename does not exist, then the function must create an empty file namedoutFilename.For example, if the function is called such asreadText("in.txt", "out.txt")and in.txt...
(in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...