arrays part 2
Question 2: Useful Array Algorithms and Operations (5 marks)
Upgrade the program in Question 1 so that the program includes:
? A static method search() that:
o Has a parameter of integer array and another integer value
o Searches the integer value in the array, and
o Returns the element index if the integer appears in the array
o Returns -1 otherwise.
? A static method sum() that computes the summation of all the values in the array and returns it.
? The program will then read user inputs and computes the summation by using the above method.
? The program reads another integer from the user.
? The program searches the value in the integer array by using the above method, and prints the result.
View Answers
September 1, 2010 at 5:53 PM
Hi Friend,
Try the following code:
import java.util.*;
public class ArrayExamples{
public static int[] readIntegers(){
int arr[]=new int[5];
Scanner input=new Scanner(System.in);
System.out.println("Enter numbers: ");
for(int i=0;i<arr.length;i++){
arr[i]=input.nextInt();
}
return arr;
}
public static int largestNo(int arr[]){
int max=arr[0];
for (int i=1; i<arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int smallestNo(int arr[]){
int min=arr[0];
for (int i=1; i<arr.length; i++) {
if (arr[i]< min) {
min = arr[i];
}
}
return min;
}
public static int search(int arr[], int element){
boolean found=false;
int index=0;
for(int i=0;i<arr.length;i++){
if(arr[i]==element){
index=i;
found=true;
}
}
if(found){
return index;
}
else{
return -1;
}
}
public static int sum(int arr[]){
int summ=0;
for (int i=0; i<arr.length; i++) {
summ+=arr[i];
}
return summ;
}
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int arr[]=ArrayExamples.readIntegers();
int largest=ArrayExamples.largestNo(arr);
int smallest=ArrayExamples.smallestNo(arr);
System.out.println("Largest No: "+largest);
System.out.println("Smallest No: "+smallest);
System.out.println("Enter element to search: ");
int element=input.nextInt();
int index=ArrayExamples.search(arr,element);
System.out.println("Element "+element+" is at postion "+index);
int summation=ArrayExamples.sum(arr);
System.out.println("Summation of all the elements of array: "+summation);
}
}
Thanks
Related Tutorials/Questions & Answers:
arrays part 2 - Java Beginnersarrays part 2 Question
2: Useful Array Algorithms and Operations (5 marks)
Upgrade the program in Question 1 so that the program includes:
? A static method search() that:
o Has a parameter of integer array and another
Printing 2 arraysPrinting
2 arrays Hi,
I have
2 arrays:
String [] head = {"Name... want this
2 arrays to be printed out in the following manner:
head[0] tab personal[0]
head[1] tab personal[1]
head[
2] tab personal [
2]
I tried different
Advertisements
Arrays Arrays Hi I need help with the following exercises.
Exercise 1: Write a Java application in which the user is prompted for the total number... of all the values as well.
Exercise
2:
Write a Java application in which you
Arrays called Rebel.java;
2.Create two
arrays. One of them must store integer numbers... the numbers in the second array by 3;
7.Print out the contents of both
arrays.
8.Swap...);
}
numbers[0] = 6;
words[0] = "1";
numbers[
2] = 8;
words[
2] = "3
arraysarrays using
arrays in methods
Java use
arrays in methods
import java.util.*;
class ArrayExample{
public static int getMaxValue(int[] arr){
int maxValue = arr[0];
for(int i=1;i < arr.length;i
arraysStore a table with students and information (name, ID, password, crypted password, etc) in a multi-dimensional array "stud"
Arrays and Strings:
Store a table with students and information (name, ID, password, crypted password
ArraysImplement Java code which takes
2 dimensional integer array as input and prints out heaviest island Implement Java code which takes
2 dimensional integer array as input and prints out heaviest island
arrays tempArray1And2[] = new int[(tempArray1.length+
tempArray1.length)-
2 Medicare Part AMedicare
Part A unbiased solution for comparing and enrolling in Medicare Advantage, Medicare Supplement and Prescription Drug Plans
java arraysjava arrays can i know how can we intilize the three dimentional
arrays in java? and how can we assign thae values to that array
java arraysjava arrays i need a java program to store student details like id,name,addr,marks,average,total..using
arrays..input data using scanner class and by using class, object and constructor
java arraysjava arrays how do you write the code for multipliying
2 double
arrays to be in a 3rd double array?
here is my code:
package employeepay;
/**
*
* @author Owner
*/
public class Main
{ADS_TO_REPLACE_1
/**
* @param args
Java with ArraysJava with Arrays I was given the assignment to create two parallel
arrays; both of size 50 (declares 50 as a constant) One is an array of strings... and store it in the
arrays. The input for the problem should be provided in a text
Part IPart I. Understanding XML
A1. Understanding XML :
Learn XML..._TO_REPLACE_6
Part II. The Simple API for XML Parsing (SAX)
Part III. XML...
Part IV. Using XSLT
Java Arrays [] counts ={3,2,1,0};
int len =
2;
reduce(counts,len);
for(int k: counts
java arrays. Add a new student record
2. Delete a student record
3. Display a student record
ModuleNotFoundError: No module named 'Arrays'ModuleNotFoundError: No module named '
Arrays' Hi,
My Python... '
Arrays'
How to remove the ModuleNotFoundError: No module named '
Arrays'... to install padas library.
You can install
Arrays python with following command
arrays in java - Java Beginnersarrays in java Hi All,
I have two
arrays. in this two array some name are same. I want to merge those
arrays into single. But while merging I want to delete duplicate entries. How merge those
arrays.
Thanks,
mln15584
java; arrays - Java Beginnersjava
arrays example How can you create a program, by using
arrays and the output would be X. by using char or string.Thank you
ModuleNotFoundError: No module named 'part'ModuleNotFoundError: No module named '
part' Hi,
My Python program is throwing following error:
ModuleNotFoundError: No module named '
part'
How to remove the ModuleNotFoundError: No module named '
part' error
Variable length arrays?Variable length
arrays? Hi:
I enjoy reading from your site...", "jasmine", "lily"};
itemList[1] = {"lotus"};
itemList[
2] = {"apple", "orange... of
arrays or if there is a smart way to handle this.
Thanks for your help.
Varma
What is javascript Arrays? What is javascript
Arrays? Hi,
I am learner of JavaScript. My question is that What is JavaScript
Arrays ? How to Define the JavaScript
arrays in you program. Please give an example show that i will try my self.
Thanks
Are arrays primitive data types? Are
arrays primitive data types? Hi,
Are
arrays primitive data types?
thanks
Hi,
In Java,
Arrays are objects. Identifier are some simple variable names which is defined as the value container. The type of value
Arrays - Java Interview QuestionsArrays 1)write a program that take integer array as input and find the second largest number in array?(Array values taken from keyboard)?
2)Given... a program that take
arrays A & B as input and find missing element in B array
Introduction to java arrays Introduction to java
arrays
...
of
Arrays in Java Programming language. You will learn how the Array class...
arrays. To meet this feature java
has provided an Array class which abstracts
combine two arrays in phpcombine two
arrays in php combine two
arrays in php
$newArray = array_combine($diff, $pages_name['data']);
foreach ($newArray as $data) {
var_dump($data);
exit('zzzzz');
echo $data . '<br>
java 2d arraysjava 2d arrays Create a program that will:
a) Create the array and populate it with the given values.
9, 8
2, 17
49, 4
13, 119
2, 19
11, 47.... and if i do it manualy it is wrong.
public class
Arrays {
public static void main
What are Interceptors in Struts 2 and how do they work?What are Interceptors in Struts
2 and how do they work?
Understanding Struts
2 Interceptors concept is very important in learning the
Struts
2. You should have good knowledge of different types of Interceptors in
Struts
2 swap two integer arraysswap two integer arrays int [] array1 = {1,2,3,4,5}
int [] array2 = {6,7,8,9,10}
how can we swap the values of bith integer
arrays
results like this
array1 = {6,7,8,9,10}
array2 = {1,2,3,4,5