0% found this document useful (0 votes)
4 views8 pages

Lab 6

This document summarizes an experiment on arrays for a programming course. It includes examples of 1D and 2D arrays, exercises to practice array concepts, and post-lab tasks. The examples demonstrate calculating array sums and averages, reversing arrays, finding maximum/minimum values, and adding matrices. Exercises have students work with arrays to calculate averages and counts, sort arrays, and manipulate 2D arrays representing matrices. Post-lab tasks involve removing duplicate array values and locating the largest element in a 2D array.

Uploaded by

Riaz Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views8 pages

Lab 6

This document summarizes an experiment on arrays for a programming course. It includes examples of 1D and 2D arrays, exercises to practice array concepts, and post-lab tasks. The examples demonstrate calculating array sums and averages, reversing arrays, finding maximum/minimum values, and adding matrices. Exercises have students work with arrays to calculate averages and counts, sort arrays, and manipulate 2D arrays representing matrices. Post-lab tasks involve removing duplicate array values and locating the largest element in a 2D array.

Uploaded by

Riaz Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

CSC241 Object Oriented Programming Fall 2023

COMSATS University Islamabad

Sahiwal Campus

CSC241 Object Oriented Programming

Experiment 06: Arrays Application

DEPARTMENT OF ELECTRICAL & COMPUTER ENGINEERING

Prepared By: Checked By: Approved By:

Dr. Saqib Saleem Dr. Ghulam Farid DQEC

----------------------------------- ----------------------------------- -----------------------------------

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Lab Experiment 06:


Arrays Application
Objectives:
1. To implements the concept of one dimensional Array
2. To implements the concepts of Multidimensional dimensional Arrays
Pre-Lab
Arrays are special data types that let us store specified number of variables from the same type
using one variable name. Arrays are an indexed data type, which means they are storing
different elements discriminating between them using unique index for each one. Declaring
and using an array in Java is similar to declaration of any variable; you have to specify data
type and name, in addition to this, you have also to specify the length of the array before using
it.
In-Lab
Example 1: This program is used to calculate the sum of array values

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Example 2: This program demonstrates how to reverse the array values.

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Example 3: This program demonstrates how to find Maximum and minimum value in arrays.

Exercises:
Exercise 1: Write a program using arrays to solve the problem. The problem is to read 10 numbers, get
the average of these numbers, and find the number of the items greater than the average.

Exercise 2: Count the occurrences of each letter in the array. To do so, create an array, say counts,
of 26 int values, each of which counts the occurrences of a letter. That is counts[0] counts the
number of a‘s, counts[1] counts the number of b‘s, and so on.

Exercise 3: Write a program that reads 10 numbers in an array, write a method which will return the
sort array.

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Multidimensional Array
Up to this point, we were storing elements in linear manner, that is, we provide one index to
locate the element in one-dimensional structure. Java also provides arrays of arrays, this means
we create an array, and each element in that array is an array itself. These arrays are also called
multidimensional arrays.

Example 4: Two dimensional arrays are common in representing matrices, for example, we
can write a program that adds two matrices (remember that matrices must have same number
of rows and same number of columns when it comes to addition).

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Example 5: This program demonstrates how to pass Two Dimensional arrays to methods.

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Exercises:
Exercise 4: Write a program that reads 16 integers from the user, stores them in 5 by 4 matrix, the last
row of the matrix should contain the sums of the columns (see figure 9.2) calculated by your program.
Then the whole matrix is printed.

Exercise 5: Write a Java program to multiply two matrices of 5x5. Suggest the data.
Exercise 6: (Largest row and column) Write a program that randomly fills in 0s and 1s into a 4-by-4
matrix, prints the matrix, and finds the first row and column with the most 1‘s. Here is a sample
input/output of the pro-gram:
0011
0011
1101
1010
The largest row index: 2 The largest column index: 2

COMSATS University Islamabad, Sahiwal Campus


CSC241 Object Oriented Programming Fall 2023

Post Lab
1. (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate
values in the array. Write a JAVA program that reads in at least ten integers, invokes the
method, and displays the results.

2. (Locate the largest element) Write the following method that returns the location of the
largest element in a two-dimensional array.
The return value is a one-dimensional array that contains two elements. These two
elements indicate the row and column indices of the largest element in the two-
dimensional array. Write a test program that prompts the user to enter a two dimensional
array and displays the location of the largest element in the array.
Enter the number of rows and columns of the array:3 4
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at (1, 2)

Note: Submit your Lab report consisting of all in-lab exercise tasks results and post lab tasks
within two working days. After the deadline your lab report won’t be considered.

COMSATS University Islamabad, Sahiwal Campus

You might also like