# help with sample program

• 05-29-2013, 10:50 AM
mounir
help with sample program
hello ,
I am new member here
I just want you to help me with this assignment plz.

1) Create an array of double of size 20
example
Enter 1st student's grade: 85.88 <enter>
Enter 2nd student's grade: 68.15 <enter>
Enter 3rd student's grade: 75.45 <enter>
...
Enter 20th student's grade: 95.20 <enter>
You are free to choose the grades.

3) Print the average and standard deviation of the grades
for example
average: 68.47
stdev: 19.45

4) Assign letter grades to the students.
top %10 will get A (two people in this case)
next %20 will get B (3rd, 4th, 5th, 6th people will get B)
next %40 will get C (7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th will get C)
next %20 will get D (15th, 16th, 17th, 18th people will get B)
next %10 will get F (19th, 20th people will get B)

1st student's letter grade is A
2nd student's letter is C
....
hope you got the way to calculate the the stdev because I didn't find any way to put it in the first loop of inputting the grades .
• 05-29-2013, 11:50 AM
JosAH
Re: help with sample program
You can calculate the 'ingredients' for the average and standard deviation while reading the individual grades Xi, calculate the sum(Xi) and the sum of the squares sum(Xi^2); when the loop finishes you know 'n', the number of grades entered by the user; so, the average = sum(Xi)/n and the standard deviation = (sum(Xi^2)-sum(Xi)*sum(Xi)/n)/n; (I forgot whether or not to take the square root of this).

kind regards,

Jos
• 05-29-2013, 02:54 PM
mounir
Re: help with sample program
yes but now how I can include the the sigma and then sqrt of the multiplication I'll try more and I'll write the whole code also here OpenStudy
• 05-29-2013, 06:18 PM
JosAH
Re: help with sample program
Quote:

Originally Posted by mounir
yes but now how I can include the the sigma and then sqrt of the multiplication I'll try more and I'll write the whole code also here OpenStudy

You don't need a 'sigma'; you just have to add the Xi's and the Xi squared in your loop:

Code:

```double Xi= 0; double Xi2= 0; int n= 0; while (<more to read>) {   double x= <read a number from the user>;   Xi+= X;   Xi2+= X*X;   n++; }```
kind regards,

Jos
• 05-30-2013, 11:42 AM
mounir
Re: help with sample program
well thank you I figured a way calculate but how to order my array decreasingly with the students and then signing letters to each ?

public static void main(String[] args) throws Exception {
Scanner in=new Scanner (System.in);
System.out.println("input the number of students");
int arr=in.nextInt(); //if in case Students number less than 20
double[] result=new double[arr+1];
int i,j = 0;
double sum=0.0;
double v=0.0;
{

System.out.println("input the grade of Student "+(i+1));
}
double average=sum/arr;
System.out.println("the Average is "+average);
result[0]=100;
{
for( j=0;j<=i;j++){
if(result[j+1]<result[j]){
double temp=result[j];result[j]=result[j+1];result[j+1]=temp;
}
}

}
System.out.println(result[j]);
}
double std =Math.sqrt((1/((double)(arr-1)))*v);
System.out.println("the stdev is "+std);
}
• 05-30-2013, 03:02 PM
kalata
Re: help with sample program
Well, you need to import class java.util.Arrays and use the sort method: Arrays.sort(<your array>). This method will sort the array in its natural order, in the case of numeric values the natural order is ascending(i.e. the array {2, 0 , 1} after the sort will be {0, 1, 2}). Note that in your case the original order of the grades in the array does matter :)

BR,
Kalin

P.S. Of course, if you're interested, you can write your own sort by implementing one of these algorithms:
https://en.wikipedia.org/wiki/Sorting_algorithm
• 06-04-2013, 12:45 PM
mounir
Re: help with sample program
public static void main(String[] args) throws Exception {
Scanner in=new Scanner (System.in);
System.out.println("input the number of students");
int n=in.nextInt(); //if in case Students number less than 20
int[] S=new int[n+1];
int i,j ;
int s=1;
double sum=0.0;
double v=0.0;

{
System.out.println("input the grade of Student "+(i+1));

}
double average=sum/n;
System.out.println("the Average is "+average);
for(s=0;s<=n;s++)
{S[s]=S[s]+1;}
int tem=s=i;
s=s+1;

}
}
}
double std =Math.sqrt((1/((double)(n)))*v);
System.out.println("the stdev is "+std);
}
}
• 06-04-2013, 12:50 PM
richakapoor
Re: help with sample program
I want to suggest you https://www.udemy.com/java-basics-fo...nCode=TECHDIS0. They are providing free java online course. Just join their course and solve your all problems.The course is spread over 128 lectures in 21 sections with practice problems in sections intended to enhance your practical knowledge of concepts learnt throughout the section.
• 06-04-2013, 01:05 PM
mounir
Re: help with sample program

Code:

``` public static void main(String[] args) throws Exception {         Scanner in=new Scanner (System.in);         System.out.println("input the number of  students");           int n=in.nextInt();      //if in case Students number less than 20            double[] grades=new double [n];           int[] S=new int[n+1];           int i,j ;           int s=1;           double sum=0.0;           double v=0.0;                     for( i=0;i<grades.length;i++)           {                  System.out.println("input the grade of Student "+(i+1));             grades[i]=in.nextDouble();             sum=sum+grades[i];                                }                     double average=sum/n;                     System.out.println("the Average is "+average);                     for(s=0;s<=n;s++)                     {S[s]=S[s]+1;}                         for ( i=1; i<grades.length-1;i++){                         for( j=0;j<grades.length-1;j++){                                   if(grades[i]<grades[i+1]){                                     double temp=grades[i];                                     grades[i]=grades[i+1];                                     grades[i+1]=temp;                                     int tem=s=i;                                     s=s+1;                                                                           }                               }                                        System.out.println("the student "+S[s]+"took the "+grades[i]);                           v =v+(grades[i] - average) * (grades[i] - average);                          }                     System.out.print(grades[i]);                        double std =Math.sqrt((1/((double)(n)))*v);                 System.out.println("the stdev is "+std);      } }```
• 06-06-2013, 09:41 AM
mounir
Re: help with sample program
find it ;)
Code:

```import java.util.*; /**  *  * @author smart  */ public class Count {     /**     * @param args the command line arguments     */                 public static void main(String[] args) throws Exception {         Scanner in=new Scanner (System.in);         System.out.println("input the number of  students");           int n=in.nextInt();      //if in case Students number less than 20            double[] grades=new double [n];           int[] S=new int[n];           int s,i,j;           double average ,v=0.0,sum=0.0;           for(s=0;s<n;s++){S[s]=s;}           for( i=0;i<grades.length ;i++)           {                  System.out.println("input the grade of Student "+(i+1));             grades[i]=in.nextDouble();             sum=sum+grades[i];                      }             average=sum/n;                         for (j=0; j<n-1;j++){                         for(i=0 ,s=0;i<n-1;i++,s++){                                   if(grades[i]<grades[i+1]){                                     double temp=grades[i];                                     grades[i]=grades[i+1];                                     grades[i+1]=temp;                                     int tem=S[s];                                     S[s]=S[s+1];                                     S[s+1]=tem;                                                      }                                                        }                              v =v+(grades[i] - average) * (grades[i] - average);                          }                         double NA=n/10;  //2 i<2                         double NB=n/5;  //4 2<=i<6                         double NC=n*2/5; //8 6<=i<14                         double NE=n/5;  //4 14<=i<18                         double NF=n/10;  //2 18<=i<20                 double std =Math.sqrt((1/((double)(n)))*v);                      System.out.println("the Average is "+average);                 System.out.println("the stdev is "+std);                 for(s=0,i=0;(s<n &&i <n);s++,i++){                     if(i<NA){         System.out.println("the "+(i+1) + " student which is the "+(S[s]+1)+"th took " +grades[i]+"recieve 'A'");                     }                     else if (NA<=i && i<NB+NA){         System.out.println("the "+(i+1) + " student which is the "+(S[s]+1)+"th took " +grades[i]+"recieve 'B'");                     }                     else if (NB+NA<=i && i<NC+NB+NA){         System.out.println("the "+(i+1) + " student which is the "+(S[s]+1)+"th took " +grades[i]+"recieve 'C'");                     }                     else if (NC+NB+NA<=i && i<NC+NB+NA+NE){         System.out.println("the "+(i+1) + " student which is the "+(S[s]+1)+"th took " +grades[i]+"recieve 'E'");                     }                     else {         System.out.println("the "+(i+1) + " student which is the "+(S[s]+1)+"th took " +grades[i]+"recieve 'F'");                     }                      } } }```