[go: up one dir, main page]

0% found this document useful (0 votes)
27 views22 pages

Arrays revision

The document discusses arrays in programming, explaining one-dimensional and two-dimensional arrays, their declaration, initialization, and various operations such as searching and sorting. It covers concepts like linear and binary search, sorting techniques like bubble sort and selection sort, and provides examples of programming tasks involving arrays. Additionally, it includes sample questions and programming exercises related to arrays for educational purposes.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
27 views22 pages

Arrays revision

The document discusses arrays in programming, explaining one-dimensional and two-dimensional arrays, their declaration, initialization, and various operations such as searching and sorting. It covers concepts like linear and binary search, sorting techniques like bubble sort and selection sort, and provides examples of programming tasks involving arrays. Additionally, it includes sample questions and programming exercises related to arrays for educational purposes.

Uploaded by

akarshhagarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 22
at are referred to by a common name. The 2 of variables of the same data type thi us memory Jocations. elements are arranged ina single row 4 Anarray is a group values are stored in contiguo 2 One-dimensional arrays: Here the the position of an element. with an index number indie, ti i nd columns where the positi Here the elements are arranged in rows al e position Ng index and the column index. statement (yo (iv) garbage value (iii) 0000 | | Ans. garbage value | 13.Write statements to show how finding the length of a character array char ch{] differs from finding String object str. Ans. int len = ch. length; he le eget oy To find the length of a String str len = str.length(); 14 int nf] = (1, 2, 3, 5, 7, 9, 13, 16}; What are the values of x and y? Use yy X = Math.pow(n[4], n[2]); 4 y = Math-sqrt(n(5] + n[7]); - X= 343.0 and y=5.0 - What is an array? Write a statement to declare an integer array of 10 elements. UCSE 20 . Array can be defined as set of variables contains values of same di different subscripts required to separate the values. ant ar[] = new int[10]; ata types having same variable name jy 16, Name the search or sort algorithm that: (i) Makes several passes through the array, _itwhere it belongs in the array. LICSE, 2013 Selecting the next smallest item in the array each time and Placing VES PilAteach stage, compares the sought key value with the key value of the middle element of the array, Ans. (i) Selection Sort Algorithm. (ii) Binary Search Algorithm. 1, Write a program to enter 20 inte, Bers in an array. Calculate and print the sum and average of the odd and even integers. a [iq Ans. import java.util .Scanner; class ArraySumOddEven { : metie Public static void main(String args[]) { Scanner sc=new Scanner (System. in); int a[]=new int[20]; int sumo=0, sume=0,co=0, ce=0; double avgo=0.0, avge=0.0; System.out-printIn ("Enter 20 integers"); for(int i=0;i<20;i++) { ali]=se.nextiInt Qe voting, } ( 80) Touchpad Question Bank (CA 86)-X sumet=a [i]; cetti J, else avge= (dou gystem.out-println Sum= "+sume+"\nAverage= "+tavge) 7 avgo= (double) sumo/co; system.out-print1n("Sum= "+sumo+"\nAverage= ivavoo)e ble) sume/ce; j program to enter 10 integers into an array. Find and print the largest number, the smallest number and java.util.Scanner; rayLargesmall if(a[i] ar(il) = ar(il; arlil; -printin("Sum = " + sum); println ("Largest number = " + 1); print1n ("Smallest number = " + s); [ICSE, 2016] REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM. ioe sant Locations: MEXICO, BR < sorry not found! Be ag util.Scanner; class SevenWonders : public static void main(String args[]) { “gtring wonders[] = {"CHICHEN ITZA", "CHRIST THE REDEEMER", "“TAJMAHAL" nGREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"); : string loca{] = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", ITALY"); Scanner in = new Scanner (System. in) ; “system.out.print ("Enter country"); String cn = in.nextLine(); “for (i = 0; i < loca.length; it+) { £ (loca[i].equals(cn)) { ie stem.out.println(loca[i] + " - " + wonders[i]) = loca.length) sprintl1n ("Sorry Not Found!"); input and store roll numbers, names and marks in 3 subjects of n number of students in five rensional arrays and display the remark based on average marks as given below: [ICSE 2015] Remark EXCELLENT DISTINCTION FIRST CLASS PASS POOR e metatic void main(String args[]) n= WEST, -[] rollno = new int{n]; ring[] names = new String[(n]; U1 pis = new int{n]; new ee 3 tt print ("Enter c.nextInt (); int ("Enter ext Int () 7 os 0 nt ("Enter marks in subject 1: marks in subject 2: marks in subject 3: i 7 else if (avg >= 60 && avg <= 74) { ___ system.out.print1n ("FIRST CLASS") ; } pease, if (avg >= 40 && avg <= 59) gram to input twenty names in an array. Arrange these names in descending order of alphabets, [ICSE 2015] mat ex sc= new Scanner (System. in); ig nf] = new String{20]; peor j< 19 - is 3+) }.compareToignoreCase (n[j +1)) <0) 44, Find the errors in the given program segment and re-write the statements correctly to assign Value. s integer array. a West agi, Ans.int a = new int (5); My "for(int i = 0; i < Write a ‘program to accept the year of graduation from school as an integer value from the user, ys, Us rch technique on the sorted array of integers given below, output the message “Record exists” Jue input is located in the array. IFnot, output the message “Record does not exist”. ithe 1, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010} tl CSE ng ; i++) ali] = i7 Scanner sc= new Scanner (System. in); yxr,min=0,max,mid,i,pos=-1; = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2po9 Touchpad Question Bank (CA 86): input 10 integer elements j ral 4m to inpul Bk lents in an array and sort them in descending order using bubble sort le soi {ICSE 2013] piic static void (String args(}) © = new Scanner (System. in); new int[10]; temp; ‘ Be ccsour-pcintin(vencer 10 integers:”); for(i = 0; i < a.length; i++) a{i] = sc-nextInt(); for(i = 0; i < a.length-1; i++) { for(j = 0; j < a-length - 1 - i; 4++){ if(alj] < alj + 1¢ temp = ali]; a(j] =alj + liz alj + 1) = temp; em.out .print1n(“Sorted elements in descending order:”); for(i = 0; i < a.length; i++) System.out.print (a[i] + “\t”); System. out.printin(); a Sl | an to accept the names of 10 cities in a single dimension array and their STD (Subscriber Trunk nother single dimension integer array. Search for a name ofa city input by the user in the list. “Search successful” and print the name of the city along with its STD code, or else display the ar [ICSE 2012] ic void main(String args[1) { tt SIZE = 10; new Scanner (System. in); ities[] = new String[SIZE]; = new String[SIZE]; inter " + SIZE +" cities and their STD codes:"); dipted woke oa AD) dnt aow2on) bx Arrays @& for (int i = 0; i < SIZE; i++) { " System. out.print ("Enter City Name: "); cities[i] = in.nextLine(); System.out.print ("Enter its STD Code: "); stdCodes[i] = in.nextLine(); } System.out.print ("Enter name of city to search: String city = in.nextLine(); int index; for (index = 0; index < SIZE; index++) { if (city.compareToIgnoreCase (cities [index]) == 0) { break; } if (index < SIZE) { System.out.println("Search Successful"); System.out.println("City: " + cities [index]); System.out.print1n("STD Code: " + stdCodes[index]) ; } else { System.out.println ("Search Unsuccessful") ; 18. Write a program to input and sort the weight of ten people. Sort and display them in descending order using . the selection sort technique. [ICSE 201) | Ans.import java.util.*; | class Sorting{ public static void (String args{]) | Scanner sc= new Scanner (System: in); double weights[] = new double[10]; int i,j, maxpos; double temp, max; System.out.printIn("Enter weights of 10 peopl for(i = 0; 4 < 10; i++) weights[i] = sc.nextDouble (); for(i = 0; i < 10-1; it+)( max = weights[i]; maxpos = i; for(j = i +1; 4 < 10; j+#)( if (max < weights{j]) Touchpad Question Bank (CA 86)-X max = weights[j]; maxpos = Jj; t temp = weights [i]; yeights[i] = weights {maxpos]; weights (maxpos] = temp; gtem.out .print1n ("Weights according to descending order:"); for(i = 0% 4 < 107 it+) gystem.out print1n (weights[i]); saram to perform binary search on a list of integers given below, to search for an element input by the found, display the element along with its position, otherwise display the message “Search element 3, 20, 30, 45, 89, 97. [ICSE 2010} fa.util.Scanner; Jass BinarySearch ttatic void main(String args[]) ler in = new Scanner (System. in); Manes) 7, 9, 11, 15, 20, 30, 45, 89, 97I7 -out print ("Enter number to search: "); -nextInt (); , max = arr.length-1, index= Jatyerero 6 ssiaed of zzel2 s ani) Jeawal bas tesrgin clin ars?cerei'> PSIEMARS } else { Pe system-out.printin(n+" found at position "+index) ; } 20. Define a class to perform binary search on a list user if it is found display the element along with is position, otherwise display the message “Search oes the not found”. llese 2m, 2,5, 7, 10, 15, 20, 29, 30, 46, 50 Ans. class question 2 ( of integers given below, to search for an element inp, int x () = {2, 5, 7, 10, 15, 20, 29, 30, 46, SO}; int n; question2(int x) ae m=x; 23c09) void search() { int £=0, 1=x.length,m; while (£<=1) { m=(£+1)/2; if (x [m]==1 { System.out.printin(*Found at ="+m); System.exit (0); } if (x[m]n) l=m-1; } system.out.printin(*Search element not found”) } 21, Define a class to declare a character array of size ten, accept the characaters into the array and display the characaters with highest and lowest ASCII (American Standard Code for Information Interchange) value: EXAMPLE: ticse 2022] INPUT MR, tr’, 'q! TAY, NS, (pl, tm! 0" ,Q\1O" , EF (9) Touchpad Question Bank (CA 86)-X ee highest ASCII value ““a “sh if (x[i]>1arge) arge=x(ili bf (x{i)