Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, I just checked a solution here: http://www.chegg.com/homework-help/questi

ID: 3534898 • Letter: H

Question

Hello, I just checked a solution here:

http://www.chegg.com/homework-help/questions-and-answers/rainfall-statistics-write-program-lets-user-enter-total-rainfall-12-months-starting-januar-q2394221


and it looks a bit incomplete. What the assignment actually required, in additon to what PreciousGiraffe7634 has mentioned, is


"the program should also print for each month a line of this form: "R inches in M" where R is the inches of rainfall for month M. These lines should be in ascending order based on the value of R."


I thought that it should be a for loop-based selestion sort, perhaps something like this to put just above the cout section:


void selectionSort(int array[i], int size)
{
int startScan, minIndex, minValue;
for (startScan=0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = array[startScan];

for(int index = startScan + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minValue = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}


so then something like these couts could show the actual inches of rain for each month:


cout << minValue << " inches in " << months[];

...


cout << maxValue << " inches in " << months[];


I am just not exactly sure how this would work in the actual code. Any ideas?

Thank you for your help in advance.

Explanation / Answer

import java.util.Scanner;



public class RainfallStatistics

{

public static void main(String argv[])

{

Scanner input=new Scanner(System.in);

double rainfall[]=new double[12];

double value;

String months[]={"January","February","March","April","May","June","July","August","September","October","Novemeber","December"};

System.out.println("Enter the total rainfall for each of 12 months ");

for(int i=0;i<12;i++)

{

while(true)

{

System.out.print("Enter rainfall for "+months[i]+" :");

value=input.nextDouble();

if(value<0.0)

System.out.println("invalid data (negative rainfall) -- retry");

else

{

rainfall[i]=value;

break;

}

}




}


double totalRainfall=0.0;

for(int i=0;i<12;i++)

totalRainfall+=rainfall[i];


System.out.println("Total rainfall for the year = "+totalRainfall);

System.out.println("The average monthly rainfall = "+totalRainfall/12.0);



selectionSort(rainfall,months);

System.out.println("The months with the highest rainfall ="+months[11]);

System.out.println("The months with the lowest rainfall ="+months[0]);


for(int i=0;i<12;i++)

System.out.println(months[i]+" : "+rainfall[i]);





}



public static void selectionSort(double rainfall[],String months[])

{

double temp;

String tempStr="";


for(int i=0;i<rainfall.length-1;i++)

{


for(int j=i+1;j<rainfall.length;j++)

{

if(rainfall[i]>rainfall[j])

{

temp=rainfall[i];

rainfall[i]=rainfall[j];

rainfall[j]=temp;


tempStr=months[i];

months[i]=months[j]

;

months[j]=tempStr;


}


}

}


}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote