Question

Write a program that performs one pass of the natural merge algorithm on the two data...

Write a program that performs one pass of the natural merge algorithm on the two data files provided (merge_1.txt and merge_2.txt) and state which value appears at index 100.

Enter that value to answer this quiz.

File format: a single integer that is the number of values to follow, followed by at least that number of integers. That first integer is not part of the data.

merge_1.txt:

100(number of elements in the list and not part of the count)
350 659 684 484 735 514 723 473 127 747 735 167 482 954 914 173 186 910 819 879 403 543 776 674 796 342 84 197 130 968 296 863 739 6 301 684 542 515 199 310 338 274 3 57 391 411 968 621 901 370 16 136 818 56 28 171 599 331 979 329 919 946 814 88 75 425 691 650 777 374 688 625 315 199 412 536 617 200 938 293 560 623 426 958 991 279 321 389 310 5 500 578 161 356 889 792 685 625 59 971 

merge_2.txt:

100(number of elements in the list and not part of the count)
125 178 578 693 343 271 596 622 840 902 804 17 9 130 191 332 995 245 854 351 545 689 667 339 777 492 565 491 533 716 924 726 174 425 929 787 576 715 528 48 822 429 557 361 597 266 891 799 546 151 16 166 955 870 249 693 924 198 953 434 859 944 466 491 487 639 156 350 815 828 957 284 397 309 974 998 56 952 270 433 695 175 747 910 200 522 94 619 477 258 164 781 591 372 751 677 587 877 700 161 

"Advance java"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

======================================

Program Merge.java

======================================

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Merge {
   //takes 3 arguments , filename1 , filename2 and position , returns element at that pos within the merged array
   public static int merge(String fname1,String fname2,int pos) {
       File f1 = new File(fname1);
       File f2 = new File(fname2);
       Scanner sc1= null,sc2 =null;
       try {
           sc1 = new Scanner(f1);
           sc2 = new Scanner(f2);
       }
       catch(FileNotFoundException e) {
           e.printStackTrace();
       }
   if(sc1 != null && sc2 != null) {
       int len1 = sc1.nextInt();
       int len2 = sc2.nextInt();
      
       int[] arr1 = new int[len1];
       int[] arr2 = new int[len2];
      
       for(int i=0;i<len1;i++) arr1[i] = sc1.nextInt();
       for(int i=0;i<len2;i++) arr2[i] = sc2.nextInt();
      
       //declare merged array
       int[] merged = new int[len1 + len2];
      
       //merge operation
       int i=0,j=0,k=0;
       while(i<len1 && j < len2) {
           if(arr1[i] < arr2[j]) {
               merged[k] = arr1[i];
               i++;
           }
           else {
               merged[k] = arr2[j];
               j++;
           }
           k++;
       }
       if(i == len1) {
           while(j<len2) {
               merged[k++] = arr2[j++];
           }
       }
       else {
           while(i<len1) {
               merged[k++] = arr1[i++];
           }
       }
       //return element at positon
       return merged[pos];
   }
       return -1;
   }
   public static void main(String[] args) {
//caller function

System.out.println(merge("C:\\Users\\Admin\\Documents\\merge_1.txt","C:\\Users\\Admin\\Documents\\merge_2.txt",3));
   }
}

======================================

Demo:

merge_1.txt : 4 4 7 8 11

merge_2.txt : 4 1 6 9 10

position : 3 ( 0 based indexing)

output: 7

======================================

Add a comment
Know the answer?
Add Answer to:
Write a program that performs one pass of the natural merge algorithm on the two data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java PART ONE ================================================== Write a program that will read employee earnings data from an...

    in java PART ONE ================================================== Write a program that will read employee earnings data from an external file and then sort and display that data. Copy and paste the input file data below into an external file, which your program will then read. You will want to use parallel arrays for this program. Modify the select sort algorithm to receive both arrays as parameters as well as the size of the arrays. Use the algorithm to sort your earnings array....

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • need help to complete this java program // add appropriate import statements here. // These imports...

    need help to complete this java program // add appropriate import statements here. // These imports you can leave as is. import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.paint.Color; import javafx.stage.Stage; /** @author yourAccountNameHere */ public class ConnectTheDots extends Application {            /*     * Do not add code to main(). Add it below in connectTheDots instead.     */     public static void main(String[] args) {         launch(args);     }         /*...

  • In C++ Programming: Using a single for loop, output the even numbers between 2 and 1004...

    In C++ Programming: Using a single for loop, output the even numbers between 2 and 1004 (inclusive) that iterates (loops) exactly 502 times. The outputted numbers be aligned in a table with 10 numbers per row. Each column in the table should be 5 characters wide. Do not nest a loop inside of another loop. Hint: First create and test the code that output the numbers all on one line (the command line will automatically wrap the output to new...

  • If the two signal handling functions in 3000pc were replaced by one function, would there be...

    If the two signal handling functions in 3000pc were replaced by one function, would there be any significant loss of functionality? Briefly explain /* 3000pc.c */ 2 3 4 5 6 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <unistd.h> 11 #include <sys/mman.h> 12 #include <errno.h> 13 #include <string.h> 14 #include <sys/types.h> 15 #include <sys/wait.h> 16 #include <semaphore.h> 17 #include <string.h> 18 #include <time.h> 19 20 #define QUEUESIZE 32 21 #define WORDSIZE 16 22 23 const int wordlist_size =...

  • Create a program that will use the attached input file and perform the following operations. Read...

    Create a program that will use the attached input file and perform the following operations. Read the file into an appropriate JCF data structure. Look up a (list of) names and numbers matching a last name or the first letters of a last name, ignoring case. Look up a (list of) names and numbers matching a number or the first digits of a number. Add a name and number to the list. Sort the list by first name, last name...

  • The Language Is SQL The language is SQL 13- List full details of agents with first...

    The Language Is SQL The language is SQL 13- List full details of agents with first names having exactly three or four letters sorted by the state attribute. 14- List the first name, the last name, the address, the city, the state, the branch number, and the email of agents working in the branch 8005 and having email addresses from yahoo.com. 15-List the first name, the last name, the address, the city, the state, the branch number, and the email...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT