How do I set up the code for #5? I have 1-4 computed fine. Would it be > Sum(hei
ID: 3712804 • Letter: H
Question
How do I set up the code for #5? I have 1-4 computed fine. Would it be
> Sum(height) / length (length) ?
Define the following vectors, which represent the weight and height of people on a
particular team (in inches and pounds):
height <- c(59,60,61,58,67,72,70)
weight <- c(150,140,180,220,160,140,130)
Define a variable:
a <- 150
Step 1: Calculating means
1) Compute, using R, the average height (called mean in R
2) Compute, using R, the average weight (called mean in R)
3) Calculate the length of the vector ‘height’ and ‘weight’
4) Calculate the sum of the heights
5) Compute the average of both height and weight, by dividing the sum (of the height
or the width, as appropriate), by the length of the vector. How does this compare to
the ‘mean’ function?
Explanation / Answer
public class p1 {
public static void main(String[] args)
{
int[] height={59,60,61,58,67,72,70};
int[] width={150,140,180,220,160,140,130};
int a=150;
int len_height,len_width,i,j;
double [] R=new double[2];
//3.CALCULATING LENGTH OF VECTOR HEIGHT AND WIDTH
len_height=height.length;
len_width=width.length;
//1.CALCULATING AVERAGE HEIGHT
int avg=0;
for(i=0;i<len_height;i++)
{
avg=avg+height[i];
}
R[0]=avg/len_height;
System.out.println("AVERAGE HEIGHT="+R[0]);
//2.CALCULATING AVERAGE WIDTH
avg=0;
for(i=0;i<len_width;i++)
{
avg=avg+width[i];
}
R[1]=avg/len_width;
System.out.println("AVERAGE WIDTH="+R[1]);
//4.SUM OF HEIGHT
int sum=0;
for(i=0;i<len_height;i++)
{
sum=sum+height[i];
}
System.out.println("SUM OF LENGTH="+sum);
//5. AVERAGE OF BOTH HEIGHT AND WIDTH
double final_avg=0;
//TOTAL AVERAGE=(AVERAGE OF HEIGHT+AVERAGE OF WIDTH)/2
final_avg=(R[0]+R[1])/2;
System.out.println("AVERAGE OF BOTH OF HEIGHT AND WIDTH="+final_avg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.