I need to write a program that will insert random integers into a linked list an
ID: 3540369 • Letter: I
Question
I need to write a program that will insert random integers into a linked list and calculate sum,average,range,median, and a random number from a random spot from linked list.
Ask the user how many nodes in linked list.
use loop to get for each node a random number between 0 and 150
create an integer object, nd put it in linked list.
sort the random numbers in linked list.
use list iterator to traverse the linked list to print out the index number and each number in the nodes with field widths to line up on the right the two columns of numbers. Include centered column headings.
Repeat the traversing with list iterator, to sum up the random number.
then, calculate and print the labeled sum, mean(2 decimal places), range, median, and a random number from a random spot from linked list of random number. ( Get a random index number for your linked list with random(), then look up the number at that spot in your linked list, telling what spot it came from).
This is how I started the program; I still need to do the calculations:
public class linkedlist
{
public static void main(String[] args) throws IOException
{
int i,number, ran;
Scanner sc = new Scanner(System.in);
LinkedList list = new LinkedList();
ListIterator li;
String line;
Random random = new Random();
int pick = random.nextInt(150);
ListIterator listIterator = list.listIterator();
System.out.println("Enter # of nodes");
number = sc.nextInt();
if (number > 0)
{
for (i = 0; i < number; i++)
list.add(1+ (int)(Math.random()*150));
Collections.sort(list);
System.out.print(list + " ");
System.out.println(listIterator.nextIndex());
}
else
System.out.println(" number is less than 0 ");
}
}
Explanation / Answer
Sample output
Enter # of nodes
10
[8, 17, 37, 46, 100, 106, 130, 131, 132, 146]
Total sum of all the numbers is :853
Average of all the number is :85.3
Range of the numbers start from 8 and goes up to :146
The median is :103.0
Random position generated is :3
Note : location 0 means first location, 1 means second location and so on
Number at this location in the list is :46
Your code
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Random;
import java.util.Scanner;
public class linkedlist
{
public static void main(String[] args) throws IOException
{
int i,number, ran;
Scanner sc = new Scanner(System.in);
LinkedList list = new LinkedList();
ListIterator li;
String line;
Random random = new Random();
int pick = random.nextInt(150);
ListIterator listIterator = list.listIterator();
System.out.println("Enter # of nodes");
number = sc.nextInt();
if (number > 0)
{
for (i = 0; i < number; i++)
list.add(1+ (int)(Math.random()*150));
Collections.sort(list);
System.out.print(list + " ");
int sum = 0;
for(int j = 0; j<number; j++){
sum = sum + (Integer) list.get(j);
}
System.out.println("Total sum of all the numbers is :"+sum);
double average = (double)sum/(double)number;
System.out.println("Average of all the number is :"+average);
int min = (Integer) list.get(0);
int max = (Integer) list.get(number-1);
System.out.println("Range of the numbers start from "+min+ " and goes up to :"+max);
double median = 0;
if (number% 2 == 0){
int added = (Integer)list.get(number/2)+ (Integer)list.get((number/2)-1);
median = (double)(added)/(double)2;
}
else
median = (Integer) list.get(number/2);
System.out.println("The median is :"+median);
}
else
System.out.println(" number is less than 0 ");
int location =(0+ (int)(Math.random()*number));
System.out.println("Random position generated is :"+location);
System.out.println("Note : location 0 means first location, 1 means second location and so on");
System.out.println("Number at this location in the list is :"+list.get(location));
}
}