Read in file containing a square matrix of N rows by N columns of integers as di
ID: 641339 • Letter: R
Question
Read in file containing a square matrix of N rows by N columns of integers as discussed in classThe first line of the file contains a single value specifying the "size" of N for the rest of the grid.
Eg., if the first line contains "10" then the remainder of the lines form a 10x10 matrix.
The remainder of the file contains rows and columns of ascending (in each direction) values.
The values are "space delimited".
There may be duplicate values in the grid.
A small sample is illustrated below.
The program should accept the following as "command line parameters"
The path/name of the input file.
The "target value" to search for in the matrix.
To the screen, display the total number of comparisons needed to find (or not find) the given element:
Using a "brute force" linear technique
"Exhaustive Search": Row-By-Row, Column-by-Column
Using the "optimized" search technique as discussed in class.
As a reminder, this was achieved by starting in the lower-left (or even upper-right) corner.
Deliverables:
A single .ZIP file submitted to Blackboard containing the following:Source code (in this case, in a single Java file) for the program.
A single "executable jar" file which can run as a "stand-alone" program.
(As discussed in class and noted in the "Other Resources")
Also, be sure to include a "comment header" at the top of your program as follows:
Sample input.txt:
Explanation / Answer
import java.io.*;
import java.util.*;
class filerd
{
public static void main(String[] args) throws IOException
{
//FileInputStream in = null;
//in.useDelimiter("");
Scanner read = new Scanner (new File("input.txt"));
read.useDelimiter(" ");
try
{
//in = new FileInputStream("c:/aptjava/input.txt");
int c;
int no, c2, c1=1;
no=Integer.parseInt(args[4]);
System.out.println(no);
while (read.hasNext() )
{
c=read.nextInt();
/*if(c.equals(' '))
{
read.nextLine();
continue;
//System.out.println("Hi");
}*/
//c2=Integer.parseInt(c);
System.out.println(c);
if (c==no)
{
System.out.println("Found " + no + "in " + c1 + "comparisons");
break;
}
c1++;
}
}finally {
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.