Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1)I am lost with the search algorithm and how to output the result.
2)How to output the average and the grades below 50.

I do not know how to go about the linear search algorithm.

Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class pdemo {
    private String[] names;
    private double[] grade;
    public int Nos;
    private String num;
    public char a=0,b=0,c=0,x=0;
    private BufferedReader stdin;
    Scanner on = new Scanner(System.in);
    public static final int NOT_FOUND = -1;
    
    public pdemo()
            throws IOException {
        stdin = new BufferedReader(new InputStreamReader(System.in));
        System.out.printf("%s\n", "Welcome Enter the Number of student taking This Couse");
        num = stdin.readLine();    Nos = Integer.parseInt(num);
        names = new String[Nos];    grade=new double[Nos];
    }
    
    public void inputNames()
            throws IOException {
        for (int i = 0;
                i < names.length;
                i++) {
            System.out.print("Enter name: ");
            names[i] = stdin.readLine();
        }
    }

    public void inputGrades()
            throws IOException {
        for(int i=0;
                i<grade.length;
                i++) {
            System.out.println("Enter the grade ");
            // grade[i] =Double.parseDouble(num);
            grade[i]=on.nextDouble();
        }
    }
    
    public void trav()
            throws IOException {
        for (int da=0;
                da<names.length;
                da++) {
            System.out.println( da+ ":"+ names[da]+" :"+grade[da]);
        }
    }

    public void sele()
            throws IOException {
        System.out.println("CHOSE FORM THE LIST OF OPERATION");
        System.out.printf("%s\n %s\n %s\n ",
                "a = Search for a particular Student",
                "b=Find the average of all Student",
                "c= Find all student with the marks blow 50");
        // for(int i=0; i < num; i++)
        String in = stdin.readLine();
    }
}



Here are the codes I am having problems with.

linear search algorithm
Java
public void search(String[] names, double[]  grade,String num,int i)
        throws IOException {
    if(a==a) {
        System.out.println("What is the name of the student u are looking for?");
        names[i] = stdin.readLine();
    }
    {
        int low = 0;
        int high = names.length - 1;
        int mid;
        while( low <= high ) {
            mid = ( low + high ) / 2;
            if (names[mid].compareTo( num)< 0)
                low = mid + 1;
            else if (names[ mid ].compareTo( num) > 0)
                high = mid - 1;
            else
                return mid;
        }

        return NOT_FOUND; // NOT_FOUND = -1
    }
}


Java
System.out.println(names[i]+""+grade[i]);
}
public void average(String [] names, double [] grade,int num) {
    if (b==b) {
        double ave=0;
        for(int i=0;
                i < num;i++) {
            ave+=grade[i];
        }
        System.out.println(ave);
    }
}

public void below(String [] names, double [] grade,int num)
{
    {
        for(int i=0;
                i <
                num;i++) {
            if(grade[i] < 50) {
                System.out.println(names[i]+grade[i]);
            }
        }
    }
}



Java
import java.io.*;
public class Main {
    public static int n;

    /**
     * @param args the command line arguments
     */
    private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

    public static void main(String[] args)
            throws IOException {
        pdemo demo = new pdemo();
        demo.inputNames();
        demo.inputGrades();
        demo.trav();
        demo.sele();
    {
}


I have a problem here too:

Java
demo.search(args, grade, null, n);
demo.average(args, grade, null, n);
demo.below(args, grade, null, n);
Posted
Updated 7-Dec-10 2:51am
v5
Comments
Dalek Dave 6-Dec-10 6:20am    
Minor Edit for Code Block Tags.
Manfred Rudolf Bihy 6-Dec-10 6:26am    
Corrected some typos. Added "Algorithms" tag.
@OP: Would you please elaborate what problems you're facing. Syntax errors, compilation erros, run time errors ...
Nagy Vilmos 7-Dec-10 8:32am    
If you compile this code what happens?
If it compiles and you can runit, what is the result of running it?

Try putting some System.out.println()'s into your code so can you see the logic flow, or run it in debug mode using any one of the freely available IDE's
Nagy Vilmos 7-Dec-10 8:51am    
Edited to make code readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900