30 views

Uploaded by Mnjira Abdallah

array

- Array
- 1329448605class11_forwebsite
- problem
- Vhdl- Array, Record and Access Types
- 301.chap37.Data 4 - Arrays
- 1d truss
- Multidimensional 2D Arrays
- Java 5 Advanced Programming
- EECO-I-C6-exercises
- TableLayOutManager
- Aug00_GlenM
- Codigo List
- Cond Sum Array
- Messages (1) entre PLC
- Arrays
- hw7.pdf
- week7
- New Microsoft Word Document (2) - Copy
- By Vijay Kumar
- Fs 8700 73 Bacnetmstp

You are on page 1of 3

Write a short Java program to solve each of the following problems. Each one will involve an

array, plus a Repeat-X or an accumulate algorithm (and maybe more than one) --- it's up to you

to figure out how!

1. Read 10 ints from the keyboard, and store them in an array. For each int in the array,

display a line with that many "*" characters.

public class PrintLines {

public static void main(String [] args) {

Scanner kb = new Scanner(System.in);

// Repeat-X algorithm to read in 10 ints and store in an array

int [] numStars = new int[10];

for(int i=0; i<numStars.length; i++) {

numStars[i] = kb.nextInt();

}

// Repeat-X algorithm to print 10 lines

for(int i=0; i<numStars.length; i++) {

// Repeat-X algorithm to print numStars[i] "*"s

for(int j=0; j<numStars[i]; j++) {

System.out.print("*");

}

System.out.println();

}

}

}

2. Read in 10 ints from the keyboard, and store them in an array. Find the maximum and

minimum values in the array, and display them on the screen.

public class PrintMaxMin {

public static void main(String [] args) {

Scanner kb = new Scanner(System.in);

// Repeat-X algorithm to read in 10 ints and store in an array

int [] intArr = new int[10];

for(int i=0; i<intArr.length; i++) {

intArr[i] = kb.nextInt();

}

// Accumulate algorithm to find maximum element

int max = intArr[0];

for(int i=1; i<intArr.length; i++) {

if(intArr[i] > max) {

max = intArr[i];

}

}

System.out.println("max = " + max);

// Accumulate algorithm to find minimum element

int min = intArr[0];

for(int i=1; i<intArr.length; i++) {

if(intArr[i] < min) {

min = intArr[i];

}

}

System.out.println("min = " + min);

}

}

3. Read in 10 ints from the keyboard, and store them in an array. Find the position (or

index) of the maximum and minimum values in the array, and swap them (move the

biggest element to the position of the smallest, and move the smallest element to the

position of the biggest).

// this is just to allow the Arrays.toString() command at the end

// it's not necessary to solve the problem; just a convenience

import java.util.Arrays;

public class PrintMaxMin {

public static void main(String [] args) {

Scanner kb = new Scanner(System.in);

// Repeat-X algorithm to read in 10 ints and store in an array

int [] intArr = new int[10];

for(int i=0; i<intArr.length; i++) {

intArr[i] = kb.nextInt();

}

// Accumulate algorithm to find position of max element

// this is just like the earlier problem, but now we need to

// keep track of max AND the position of max in the array

int max = intArr[0];

int maxPos = 0; // maxPos starts at position 0

for(int i=1; i<intArr.length; i++) {

if(intArr[i] > max) {

max = intArr[i];

maxPos = i;

}

}

// Accumulate algorithm to find position of min element.

// This is just like the earlier problem, but now we need to

// keep track of min AND the position of min in the array

int min = intArr[0];

int minPos = 0;

for(int i=1; i<intArr.length; i++) {

if(intArr[i] < min) {

min = intArr[i];

minPos = i;

}

}

// swap the biggest and smallest elements

intArr[minPos] = max;

intArr[maxPos] = min;

// here's a handy command to print out the entire array

System.out.println(Arrays.toString(intArr));

}

}

4. Read 10 ints from the keyboard, and store them in an array. Display "true" on the screen

if there is an even number of even numbers among these 10. Otherwise, display "false".

public class EvenEvens {

public static void main(String [] args) {

Scanner kb = new Scanner(System.in);

// Repeat-X algorithm to read in 10 ints and store in an array

int [] intArr = new int[10];

for(int i=0; i<intArr.length; i++) {

intArr[i] = kb.nextInt();

}

// Accumulate algorithm to find number of even elements

int countEven = 0;

for(int i=0; i<intArr.length; i++) {

if(intArr[i] % 2 == 0) {

countEven++;

}

}

if(countEven % 2 == 0) { // even number of evens

System.out.println("true");

} else {

System.out.println("false");

}

}

}

- ArrayUploaded byBasic204
- 1329448605class11_forwebsiteUploaded byAtul Gupta
- problemUploaded byRakib Ahsan
- Vhdl- Array, Record and Access TypesUploaded byPreetham Prinson D'souza
- 301.chap37.Data 4 - ArraysUploaded byEdward Bujak
- 1d trussUploaded byKapil D Gangwar
- Multidimensional 2D ArraysUploaded byDoula Isham
- Java 5 Advanced ProgrammingUploaded bydipaby
- EECO-I-C6-exercisesUploaded byRotaru Ana-Maria
- TableLayOutManagerUploaded byvaibhav_vbv
- Aug00_GlenMUploaded bysaumitra2
- Codigo ListUploaded byJosé Walter Losada Cabrera
- Cond Sum ArrayUploaded byapi-26344229
- Messages (1) entre PLCUploaded byValuli Ruiz
- ArraysUploaded bySree Murthy
- hw7.pdfUploaded byGeorgeSidiras
- week7Uploaded byMOnurBayram
- New Microsoft Word Document (2) - CopyUploaded byAmy Oliver
- By Vijay KumarUploaded bykumud_kedia
- Fs 8700 73 BacnetmstpUploaded byKay Karthi
- Array Based Stack Text TutorialUploaded byShivaniChopra
- Array PolikuUploaded byMaxalexter O'neil
- ch06ppt (1)Uploaded bynDna
- chapter 2 C++Uploaded byTariq Khan
- Nano Arduino 2013Uploaded byAlfredo Salazar
- Basics of OpenCV APIUploaded byrphmi
- Class XII Computer Science Practice QuestionsUploaded bydeemalhotra73
- C Language NotesUploaded bymomghost
- WEB-PPT3-EIE-dept(1)Uploaded byZeeshanMirza
- L02Uploaded byhisuin

- CAD Tutorial Fangfang 110227Uploaded byJames Perez
- File 65690Uploaded bysman2014
- Behavioral InterviewingUploaded byMurtaza Shaikh
- CPM_Reading3.pdfUploaded byMnjira Abdallah
- Hardware MaintenanceUploaded byMnjira Abdallah
- UCU 001 Communication SkillsUploaded byTheo Ntwali
- HTML Basics 1Uploaded byaggarwalmegha
- System Analysis and Design by Dr. JawaharUploaded byAnjum Monga

- Ee 686 Optimization Theory-2014Uploaded bynarendra
- 7BM09_0EUploaded byArisena Argan
- 10.1.1.150Uploaded byNiharika Challapalli
- Explicit and ImplicitUploaded byRaju Sharma
- UNIT 1 TO 2Uploaded bytbijle
- 2375-mainUploaded byAshish Sharma
- Image CompressionUploaded byKuldeep Mankad
- strategies-for-solving-multiplication-problems2Uploaded byapi-333680177
- Text CompressionUploaded byVatsal Thacker
- Ldpc Ieee PaperUploaded byPravin Salunkhe
- Dynamic ResponseUploaded byAlex Javier
- Kalman Filter Design - MATLABUploaded byandromeda1047
- different simplex methodsUploaded bydaselknam
- IMAGE DENOISING VIA SELF-LEARNING BASED IMAGE DECOMPOSITION USING DWTUploaded byijaert
- Convex OptmizationUploaded byKarrthik Sharma
- Second International Conference on Signal, Image Processing and Embedded Systems (SIGEM 2016)Uploaded byCS & IT
- ADSP Quiz-1 Paper With AnswersUploaded byAvinash Baldi
- Patel 2016Uploaded bymeena
- Gaussian LU Eliminations SeidelUploaded byHarris Anches
- r7411510 Neural NetworksUploaded bysivabharathamurthy
- Assignment No 2 Cs 502 SolutionUploaded byJone Hopkins
- SamplingUploaded bySatya Jadaun
- Process Scheduling problemsUploaded byhaneeshky
- Digital Control Systems (4)Uploaded byMohammed Abdul Jabbar
- Unit II AlgoUploaded byTarun Mittal
- PenaltyUploaded byShrutiPargai
- 04 Bitmapped ImagesUploaded byThongchut Kbgi
- digital communication notesUploaded byAnushapr Anushapr
- Introduction to WaveletUploaded bySridhar Koneru Venkkat
- Minggu 1-Klasifikasi SinyalUploaded byImasAgustinSyarif