In: Computer Science
It's java file
1. declare an vector without specifying the size
2. use push_back to add random integers between 100 and 999 to the vector
3. write a function that returns the smallest, largest, and average of the numbers in the vector
3. display the smallest, largest, and average of the numbers in the vector
Hi, please fallow the important steps for solution of your code.
1. In this we used Math.random() function is used to generate the random numbers based on given minimum and max value.
this function needs to convert to int because by default it was double. So that's reason we used to cast int
2. In vector we can get the element based on the index. Index will start from 0 (Zero)
getIndex(0) will return first value
3. size() nethod returns the total number of elements or size of the vactor.
4. function will return only one value
Please find the code and screen shot of the code along with comments
Java file
-----------------------------
import java.util.Vector; public class Vector_Random { public static void main(String[] args) { // vector with out size Vector<Integer> userVector = new Vector<>(); // this line will generate random number between 1 to 5. you may chnage int randomNumber = (int) (1 + (Math.random() * 5) ); // used push_back it will add a random number between 100 and 999 in vector object for (int i = 0; i < randomNumber; i++) { userVector.add((int) ( 100 + (Math.random() * 999) )); } //printing vector on the console System.out.println(userVector); // this function will return average of vector System.out.println("Average is : " + getAverageFromVector(userVector)); } private static double getAverageFromVector(Vector<Integer> userVector) { // initially we declared avg and total is 0 double avg =0,total=0; // assign first value from the vector to smallest and greatest int smallest=userVector.get(0), greatest =userVector.get(0); for (int i = 0; i < userVector.size(); i++) { // every value will be added to the total total+=userVector.get(i); // this is the logic and check each value with smallest and highest and store the value based on condition if(userVector.get(i) < smallest) { smallest = userVector.get(i); } if(userVector.get(i) > greatest) { greatest = userVector.get(i); } } // printing details System.out.println("Smallest is : " +smallest); System.out.println("greatest is : " +greatest); // calculate the avg avg = total / userVector.size(); // return the avg return avg; } }
Snippet code:
import java.util.Vector;
public class Vector_Random {
public static void main(String[] args) {
// vector with out size
Vector<Integer> userVector = new Vector<>();
// this line will generate random number between 1 to 5. you may chnage
int randomNumber = (int) (1 + (Math.random() * 5) );
// used push_back it will add a random number between 100 and 999 in vector object
for (int i = 0; i < randomNumber; i++) {
userVector.add((int) ( 100 + (Math.random() * 999) ));
}
//printing vector on the console
System.out.println(userVector);
// this function will return average of vector
System.out.println("Average is : " + getAverageFromVector(userVector));
}
private static double getAverageFromVector(Vector<Integer> userVector) {
// initially we declared avg and total is 0
double avg =0,total=0;
// assign first value from the vector to smallest and greatest
int smallest=userVector.get(0), greatest =userVector.get(0);
for (int i = 0; i < userVector.size(); i++) {
// every value will be added to the total
total+=userVector.get(i);
// this is the logic and check each value with smallest and highest and store the value based on condition
if(userVector.get(i) < smallest) {
smallest = userVector.get(i);
}
if(userVector.get(i) > greatest) {
greatest = userVector.get(i);
}
}
// printing details
System.out.println("Smallest is : " +smallest);
System.out.println("greatest is : " +greatest);
// calculate the avg
avg = total / userVector.size();
// return the avg
return avg;
}
}
Sample Output:
All the best..