ARRAY ; COMPUTER SCIENCE WRITTEN ASSIGNMENT
An Array is a container object that holds a fixed number of values of a single type. The simplest type of data structure is a linear array, also called a one-dimensional array. The length of an array is established when the array is created. After creation, its length is fixed. Each item in an array is called an element and each element is accessed by its numerical index. As shown in the preceding illustration, the numbering of an array begins with '0'. The 9th element, for example, would therefore be accessed at index '8'.
1.0 PART A: ONE SINGLE DIMENSIONAL ARRAY
1.1 Declare, Create & Initialize an Array
Obtaining an array is a two-step process. First, we must declare a variable of the desired array type. Second, we must allocate the memory that will hold the array and assign it to the array variable. Thus, in Java language, all arrays are dynamically allocated.
1.1.1 Declaring & Creating an Array
An array declaration has two components; the 'type' and the 'name'. 'Type' declares the element type of the array. The element type determines the data type of each element that compromises the array. Like an array of int type, we can also create an array of other primitive data types like char, float, double and so on.
To declare an array, the following format is used;
dataType [] arrayName;
for example;
String [] names;
When an array is declared, only a reference of the array is created. To actually create or give memory to an array, we must create an array using the general form of 'new' as follows;
arrayName = new dataType[arraySize];
Here dataType specifies the type of data being allocated, arraySize specifies the number of elements in the array and arrayName is the name of the array variable that is linked in the array.
For example;
names = new String[7];
Declaring and creating an array can be written separately or combine in one statement.
dataType[] arrayName;
arrayName = new dataType[arraySize];
or
dataType[] arrayName = new dataType[arraySize];
1.1.2 Initializing an Array
After an array is declared, its elements must be initialized by assigning values to the array to avoid any error. If we didn't assign any value to the array elements, the compiler will assign value with default to the element. For example;
No Data Type Default Value;
boolean False
int 0
double 0.0
String null
Alternatively, we can use the initializer list to create an array and give initial values at the same time, for example, the declaration;
double[] marks = {97.5, 41.8, 36.5, 80.5,.56.0, 97.0};
will create an array of six elements such that the 1st element marks[0] are initialized to 97.5, the 2nd element marks[1] are initialized by 41.8 and so on.
marks[0] = 97.5;
marks[1] = 41.8;
marks[2] = 36.5;
marks[3] = 80.5;
marks[4] = 56.0;
marks[5] = 97.0;
This type of declaration doesn't need 'array size' or 'new' keyword Java.
1.2 Access Array Element Using Indexes
Each element in the array is accessed via its index. The index begins with '0' and ends at the total array size ( total_array _size minus 1). All the elements os the array can be accessed using Java for Loop. to access elements in an array, use the following format;
for (int i = 0; i < arrayName.length; i++)
{
//operations on array
}
1.3 Array Operations
There are plenty of array operations. Basically, we access elements in the array to do some operations on arrays such as entering data into arrays with input value entered by the user, displaying each element in the array and find the smallest or the largest element.
1.3.1 Input & Display Elements
Assume the array is created as follows;
int [] arr = new int[3];
The following loop initializes the array with user input values;
for(int x = 0 ; x<arr.length ; x++){
arr[x] = sc.next();
}
To print an array, we have to print each element in the array using a loop;
for(int x = 0 ; x<arr.length ;x++){
System.out.print(arr[x]);
}
Input & display element in Java code example;
1.3.2 Find Maximum & Minimum Elements
To find the maximum value use a variable named max to store the maximum value. Initialize max to '0'. Then, compare each element with max, and update max if the element has a greater value than max.
int max = 0; //Initialize max to '0'
for(int x = 0; x<numbers.length; x++) {
if(numbers[x]>max)//compare each element with max
max = numbers[x];//update max if element greater than max
}
To find the minimum value use a variable named min to store the minimum value. Initialize min to '100'. Then, compare each element with min, and update min if the element has a lower value than min.
int min = 100; //initialize min to 100
for(int x=0; x<numbers.length;x++){
if(numbers[x]<min)//compare each element with min
min=numbers[x];//update min if element lower than min
}
Suppose that the array Int is {20,30,25,35,-16,60,-100}. The maximum and minimum value of this array can be sorted using the following coding;
2.0 PART B: PROJECT DEVELOPMENT
2.1 Introduction to Problem Solving
Problem-solving is the process of transforming the description of a problem into a solution by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problems solving strategies, techniques and tools.
There are 5 steps in problem-solving in coding:
Step 1: Problem Analysis
Step 2: Design a Solution
Step 3: Implementation
Step 4: Testing
Step 5: Documentation
2.1.1 Step 1: Problem Analysis(IPO)
Problem analysis is an analysis of a problem statement to identify the input to the problem, the relevant process to produce output and the required output. This step is essential to obtain a viable solution to rectify the problem. When planning an approach to a problem, we must understand the parameters of the problem first. Base on the situation give, we can ascertain three key elements; the expected input, the expected output and ay relevant rules for processing.
Input : n, Id, Distance1, Distance2
Process : To calculate & display Winner, highestDistance, recordBreaker, highestRecord,breakRecord, Mean based on n, Id, Distance1, Distance2
Output : Winner, highestDistance, recordBreaker, highestRecord, breakRecord,Mean
2.1.2 Step 2: Design a Solution; Algorithm
An algorithm is an organized logical approach to a particular problem. Often at times, algorithms are represented with Pseudocode as its simply an implementation of an algorithm in the form of annotations and informative text written in plain English. In this situation, the algorithm will be created in Pseudocode based on the problem analysis.
Begin
Set array to n, Distance1, Distance2, Id
Initialize Winner=0, breakRecord=0, highestRecord=0, Initialize totalDistance=0
Final double Record=8.50;
Read n
Initialize i=0
for ( i<Id.length) then,
Read Id, Distance1, Distance2
Calculate totalDistance = totalDistance+Distance1+Distance2
if(Distance1<=Distance2)then,
Distance=Distance2
else
Distance=Distance1
End if
if(Distance>highestDistance)then,
highestDistance = Distance
Winner = Id
End if
if(Distance>Record) then,
highestRecord = Distance
breakRecord = breakRecord+1
recordBreaker = Id
End if
i++
End for
Calculate mean = totalDistance/(n*2)
Display Winner, highestDistance, recordBreaker, highestRecord Display breakRecord, mean
End
2.1.3 Step 3: Implementation
Implementation is the process of implementing an algorithm by writing a computer program using a programming language.
2.1.4 Step 4: Test
After translating the algorithm into Jave code, we must execute the code to convince that the program will do exactly what it is expected to do. In other words, this step is to verify that the code is right. In this test, we should be able to spot all the errors as well as fixing the error. Debugging is the process of removing errors form the program.
(I) Snytax Error
Java syntax errors refer to mistake made by programmer in implementing the grammar of the java programming language. It doesn't cover mistakes in the logic of the program, itself. Syntax errors are the easiest to find and correct. The compiler will tell you where it got into trouble and its best guess as to what you did wrong.
Etc; Error occurs when a syntactically correct statement does not appear where it should be. The reason is the compiler interpret this is as a declaration and can't find a class els as the type of the variable max.
(II) Run-time Error
A run-time error will only occur when the code is actually running. Run-time error lead to program crashes and bugs in the code. As a result, its hard to track down this error.
Etc; Error occur when the program of the array index is out of range. These exceptions are caused by attempting to access the array with an index that is outside the limits of the array. The errors cause an exception immediately and lead to facilitating debugging.
e.g; Exception is thrown when attempting the index an array Id[i] where the index[i] is not within the values 0 and Id.length-1, inclusive.
(III) Logic Error
A logic error or bug is when the program compiles and runs, but does the wrong thing. The Java system, of course, has no idea what your program is supposed to do, so it provides no additional information to help us to find the error.
Etc; The error of logic is the output is not what expected ouput. The formula of the calculation is wrong. As a result the output doesn't exceed the expected output.
(IV) Errors
(V) Final Codes
(VI) Final Output
2.1.5 Step 5: Documentation
The documentation contains a description of the problem to help others understand the coding. A comment is a way to create documentation. The comment explains Java code as it makes the code more readable. A comment would not be executed when testing the alternative code. There are two ways to write a comment; single-line comments and multiple–line comments.
3.0 Conclusion
Arrays are one of the oldest and most important data structures and are used by almost every program. They are also used to implement many other data structures, such as lists and strings. They effectively exploit the addressing logic of computers. In most modern computers and many external storage devices, the memory is a one-dimensional array of words, whose indices are their addresses. Processors, especially vector processors, are often optimized for array operations.
Arrays are useful mostly because the element indices can be computed at run time. Among other things, this feature allows a single iterative statement to process arbitrarily many elements of an array. For that reason, the elements of an array data structure are required to have the same size and should use the same data representation. The set of valid index tuples and the addresses of the elements and hence the element addressing formula are usually, but not always, fixed while the array is in use.
To sum up, arrays are more efficient and beneficial when compared to linked lists and hash tables. They are faster and can be utilized anywhere. They store data of similar data types together and can be used anywhere in the code. Hence they are more efficient when it comes to memor6y allocations and are most advisable to be used in all modern languages.
4.0 References
4.1 Books
Abu Nu’man Ibn Ali(2019), Java Learn By Example Object-Oriented Techniques
Mohd Amran(2018), Learning JAVA in 28 Hours: Java Practical Module
Addison, P. (2012). Principles Program Design: Problem Solving with JavaScript. eBooks – Google Play. Retrieved December 28, 2019
4.2 Article
Elliot, I. (2017, January 3). JavaScript Data Structure – Array Object. iProgrmmer. Retrieved December 29, 2019https://www.i-programmer.info/programming/javascript/2865-javascript-data-structures-the-
Omole, O & Hibbard, J. (2019, November 25). Quick Tip: How to Sort an Array of Objects in JavaScript. SitePoint. Retrieved January 3, 2020 https://www.sitepoint.com/sort-an-array-of-objects-in-javascript/
Friesen, J. (2017, November 11). Datastructures and algorithms in Java, Part 2: One-dimensional arrays. JavaWorld. Retrieved January 5, 2020
Friesen, J. (2017, November 11). Datastructures and algorithms in Java, Part 2: One-dimensional arrays. JavaWorld. Retrieved January 5, 2020
4.3 Website
Master JavaScript Comments: Single – Line Vs Multi – Line. (2016, January 7). Retrieved January 3, 2020https://www.bitdegree.org/learn/javascript-comment/amp
Perez, S. ( July 2, 2018). JavaScript Weekly: Using a Structured Problem – Solving Approach. Retrieved January 7, 2020
Perez, S. ( July 2, 2018). JavaScript Weekly: Using a Structured Problem – Solving Approach. Retrieved January 7, 2020












Comments
Post a Comment