Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with weighted average

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with weighted average

    I thought it was all right with the code... but i'm not getting to the answer I want...
    That's the input file i have to use to solve it
    exemplo1.txt
    That's the answer
    salva.jpg
    That's what I am getting.... (the number's at the right are the results)
    salva2.jpg
    Here's the code, appreciate who could help me...
    Solved! apparently JAVA doesn't work well with 3 "for" inside each other

    Import java.util.Scanner;
    import java.util.Locale;
    public class ep1 {
     
    	public static void main (String [] args) {
    		Scanner ler = new Scanner (System.in);				
    		ler.useLocale(Locale.ENGLISH);		
    		int nCargos = ler.nextInt();
    		String [] cargo = new String [nCargos];				
    		double [][] peso = new double [nCargos] [14];		
    		for (int i = 0; i < nCargos; i++) {
    			cargo[i] = ler.nextLine();              //I can't read the string if i do not use it twice, idk why also
    			cargo[i] = ler.nextLine(); 			
    			System.out.print (cargo[i] + " : \n");	
    			System.out.println();			
    		for (int u = 0; u < 14; u++) {
    			peso [i] [u] = ler.nextDouble();
    			System.out.printf (peso[i][u] + " ");						
    		  }
    		  System.out.println();
    		  System.out.println("-------------------------------------------------------------");
    	   }
    	   int nPessoas = ler.nextInt();
    	   String [] nome = new String [nPessoas];
    	   double [][] pontos = new double [nPessoas] [14];
    	   double media = 0;
    	   double [] medias = new double [nPessoas];
    	   for (int D = 0; D < nPessoas; D++) {
    		   nome [D] = ler.nextLine(); // Mesmo caso do Scanner nextLine anterior
    		   nome [D] = ler.nextLine();		   
    		   System.out.print (nome[D] + " : \n");
    		   System.out.println();
    		   for (int c = 0; c < 14; c++) {                          
    			   pontos [D] [c] = ler.nextDouble();
    			   System.out.print (pontos[D][c] + " ");
    			   media += pontos [D] [c];
    		   }
    		   media = media/14;
    		   System.out.println();
    		   System.out.println ("A media de "+ nome[D] + " e: "+ media);
    		   System.out.println();
    		   System.out.println("-------------------------------------------------------------");		   
    	   }	   
    	   double s = 0;
    	   double s1 = 0;
    	   double den = 0;
    	   double [][] pts = new double [nCargos] [nPessoas];
    	   for (int i = 0; i < nCargos; i++) {
    		   for (int k = 0; k < nPessoas; k++) {
    			  for (int c = 0; c < 14; c++) {
    				s1 = pontos[k][c] * peso[i][c];                  // Something is wrong around here
    				den += peso[i][c];
    				s += s1;				
     
    			  }
    			  System.out.println (s + " " + den);
     
    			  pts [i][k] = s/den;
    			  System.out.print (nome [k] + "tem a média " + pts [i] [k] + " para o cargo " + cargo [i] + " \n");
    			  System.out.println ();
    		   }
    	   }
    	   System.out.println ();
    	   System.out.println("-------------------------------------------------------------");
    	   String [] nomes2 = nome;
    	   double te = 0;
    	   String tem = "";	   
    	   for (int i = 0; i < nCargos; i++) {
    		   System.out.println (cargo[i]);
    		   System.out.println();
    		   for (int g = 30; g > 0; g--) {
    			   for (int k = nPessoas - 1; k > 0; k--) {
    				   if ( pts [i] [k] > pts [i] [k-1]) {
    					   te = pts [i] [k];
    					   pts [i] [k] = pts [i] [k-1];
    					   pts [i] [k-1] = te;
    					   tem = nomes2[k];
    					   nomes2 [k] = nomes2 [k-1];
    					   nomes2 [k-1] = tem;
    				   }
    			   }			   
    		   }
    		   for (int c = 0; c < nomes2.length; c++) {
    			   System.out.println (nomes2 [c] + " " + pts [i] [c]);			   
    		   }
    		   System.out.println();
     
     
    	   }	   
    	}
     
    }
    Last edited by Afueru; May 12th, 2019 at 11:39 AM. Reason: Didn't saw the coding formatation option

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    270
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Problem with weighted average

    Can you explain what you want to achieve? How the application should work?
    Whatever you are, be a good one

Similar Threads

  1. Problem with a weigted average code
    By neophyte67 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 28th, 2014, 08:53 PM
  2. Replies: 1
    Last Post: July 26th, 2014, 04:38 AM
  3. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  4. [SOLVED] Average and counting problem (sorta)
    By Elyril in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 19th, 2014, 07:04 PM
  5. Java Weighted interval scheduling
    By ryclegman in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 23rd, 2012, 03:06 PM