Find the largest absolute value in array
Here given code implementation process.
// C program
// Find the largest absolute value in array
#include <stdio.h>
//Find the absolute largest element in given collection
int largest_absolute(int collection[], int size)
{
//loop controlling variable
int i = 0;
//Get first element
int result = collection[i];
// Define auxiliary variables
int a = 0;
int b = 0;
//Traverse the collection elements from 1 to n-1
for (i = 1; i < size; ++i)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
}
return result;
}
//Display element of given collection
void display_element(int collection[], int size)
{
for (int i = 0; i < size; ++i)
{
printf(" %d", collection[i]);
}
printf("\n");
}
int main()
{
// Define the array elements
int collection1[] = {
4,
2,
-12,
6,
11,
-5,
9,
3
};
int collection2[] = {
8,
2,
-3,
7,
1,
31,
12,
11
};
int collection3[] = {
1,
1,
2,
-5,
1,
5,
4,
3
};
//Get the size of given collection1
int size = sizeof(collection1) / sizeof(collection1[0]);
printf("Element : ");
display_element(collection1, size);
int result = largest_absolute(collection1, size);
printf("Largest Absolute : %d \n", result);
//Get the size of given collection2
size = sizeof(collection2) / sizeof(collection2[0]);
printf("\nElement : ");
display_element(collection2, size);
result = largest_absolute(collection2, size);
printf("Largest Absolute : %d \n", result);
//Get the size of given collection3
size = sizeof(collection3) / sizeof(collection3[0]);
printf("\nElement : ");
display_element(collection3, size);
result = largest_absolute(collection3, size);
printf("Largest Absolute : %d \n", result);
return 0;
}
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
/*
Java Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
public int largest_absolute(int[] collection, int size)
{
//loop controlling variable
int i = 0;
//Get first element
int result = collection[i];
// Define auxiliary variables
int a = 0;
int b = 0;
//Traverse the collection elements from 1 to n-1
for (i = 1; i < size; ++i)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
}
return result;
}
//Display element of given collection
public void display_element(int[] collection, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + collection[i] + "");
}
System.out.print("\n");
}
// main function to test
public static void main(String args[])
{
AbsoluteValue obj = new AbsoluteValue();
// Define the array elements
int[] collection1 = {
4,
2,
-12,
6,
11,
-5,
9,
3
};
int[] collection2 = {
8,
2,
-3,
7,
1,
31,
12,
11
};
int[] collection3 = {
1,
1,
2,
-5,
1,
5,
4,
3
};
//Get the size of given collection1
int size = collection1.length;
System.out.print("Element : ");
obj.display_element(collection1, size);
int result = obj.largest_absolute(collection1, size);
System.out.print("Largest Absolute : " + result + " \n");
//Get the size of given collection2
size = collection2.length;
System.out.print("\nElement : ");
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
System.out.print("Largest Absolute : " + result + " \n");
//Get the size of given collection3
size = collection3.length;
System.out.print("\nElement : ");
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
System.out.print("Largest Absolute : " + result + " \n");
}
}
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
//Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
public:
//Find the absolute largest element in given collection
int largest_absolute(int collection[], int size)
{
//loop controlling variable
int i = 0;
//Get first element
int result = collection[i];
// Define auxiliary variables
int a = 0;
int b = 0;
//Traverse the collection elements from 1 to n-1
for (i = 1; i < size; ++i)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
}
return result;
}
//Display element of given collection
void display_element(int collection[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << collection[i] << "";
}
cout << "\n";
}
};
int main()
// main function to test
{
AbsoluteValue obj = AbsoluteValue();
// Define the array elements
int collection1[] = {
4 , 2 , -12 , 6 , 11 , -5 , 9 , 3
};
int collection2[] = {
8 , 2 , -3 , 7 , 1 , 31 , 12 , 11
};
int collection3[] = {
1 , 1 , 2 , -5 , 1 , 5 , 4 , 3
};
//Get the size of given collection1
int size = sizeof(collection1) / sizeof(collection1[0]);
cout << "Element : ";
obj.display_element(collection1, size);
int result = obj.largest_absolute(collection1, size);
cout << "Largest Absolute : " << result << " \n";
//Get the size of given collection2
size = sizeof(collection2) / sizeof(collection2[0]);
cout << "\nElement : ";
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
cout << "Largest Absolute : " << result << " \n";
//Get the size of given collection3
size = sizeof(collection3) / sizeof(collection3[0]);
cout << "\nElement : ";
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
cout << "Largest Absolute : " << result << " \n";
return 0;
}
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
//Include namespace system
using System;
/*
C# Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
public int largest_absolute(int[] collection, int size)
{
//loop controlling variable
int i = 0;
//Get first element
int result = collection[i];
// Define auxiliary variables
int a = 0;
int b = 0;
//Traverse the collection elements from 1 to n-1
for (i = 1; i < size; ++i)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
}
return result;
}
//Display element of given collection
public void display_element(int[] collection, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + collection[i] + "");
}
Console.Write("\n");
}
// main function to test
public static void Main(String []args)
{
AbsoluteValue obj = new AbsoluteValue();
// Define the array elements
int[] collection1 = {
4 , 2 , -12 , 6 , 11 , -5 , 9 , 3
};
int[] collection2 = {
8 , 2 , -3 , 7 , 1 , 31 , 12 , 11
};
int[] collection3 = {
1 , 1 , 2 , -5 , 1 , 5 , 4 , 3
};
//Get the size of given collection1
int size = collection1.Length;
Console.Write("Element : ");
obj.display_element(collection1, size);
int result = obj.largest_absolute(collection1, size);
Console.Write("Largest Absolute : " + result + " \n");
//Get the size of given collection2
size = collection2.Length;
Console.Write("\nElement : ");
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
Console.Write("Largest Absolute : " + result + " \n");
//Get the size of given collection3
size = collection3.Length;
Console.Write("\nElement : ");
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
Console.Write("Largest Absolute : " + result + " \n");
}
}
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
<?php
/*
Php Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
public function largest_absolute( & $collection, $size)
{
//loop controlling variable
$i = 0;
//Get first element
$result = $collection[$i];
// Define auxiliary variables
$a = 0;
$b = 0;
//Traverse the collection elements from 1 to n-1
for ($i = 1; $i < $size; ++$i)
{
//Get initial result
$a = $result;
$b = $collection[$i];
if ($a < 0)
{
//When [a] value is negative
//Make positive numbers
$a = -$a;
}
if ($b < 0)
{
//When [b] value is negative
//Make positive numbers
$b = -$b;
}
if ($a == $b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if ($result < 0)
{
//Get new result
$result = $collection[$i];
}
}
else if ($a < $b)
{
//Get new result
$result = $collection[$i];
}
}
return $result;
}
//Display element of given collection
public function display_element( & $collection, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $collection[$i] ."";
}
echo "\n";
}
}
function main()
{
$obj = new AbsoluteValue();
// Define the array elements
$collection1 = array(4, 2, -12, 6, 11, -5, 9, 3);
$collection2 = array(8, 2, -3, 7, 1, 31, 12, 11);
$collection3 = array(1, 1, 2, -5, 1, 5, 4, 3);
//Get the size of given collection1
$size = count($collection1);
echo "Element : ";
$obj->display_element($collection1, $size);
$result = $obj->largest_absolute($collection1, $size);
echo "Largest Absolute : ". $result ." \n";
//Get the size of given collection2
$size = count($collection2);
echo "\nElement : ";
$obj->display_element($collection2, $size);
$result = $obj->largest_absolute($collection2, $size);
echo "Largest Absolute : ". $result ." \n";
//Get the size of given collection3
$size = count($collection3);
echo "\nElement : ";
$obj->display_element($collection3, $size);
$result = $obj->largest_absolute($collection3, $size);
echo "Largest Absolute : ". $result ." \n";
}
main();
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
/*
Node Js Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
largest_absolute(collection, size)
{
//loop controlling variable
var i = 0;
//Get first element
var result = collection[i];
// Define auxiliary variables
var a = 0;
var b = 0;
//Traverse the collection elements from 1 to n-1
for (i = 1; i < size; ++i)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
}
return result;
}
//Display element of given collection
display_element(collection, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + collection[i] + "");
}
process.stdout.write("\n");
}
}
function main()
{
var obj = new AbsoluteValue();
// Define the array elements
var collection1 = [4, 2, -12, 6, 11, -5, 9, 3];
var collection2 = [8, 2, -3, 7, 1, 31, 12, 11];
var collection3 = [1, 1, 2, -5, 1, 5, 4, 3];
//Get the size of given collection1
var size = collection1.length;
process.stdout.write("Element : ");
obj.display_element(collection1, size);
var result = obj.largest_absolute(collection1, size);
process.stdout.write("Largest Absolute : " + result + " \n");
//Get the size of given collection2
size = collection2.length;
process.stdout.write("\nElement : ");
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
process.stdout.write("Largest Absolute : " + result + " \n");
//Get the size of given collection3
size = collection3.length;
process.stdout.write("\nElement : ");
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
process.stdout.write("Largest Absolute : " + result + " \n");
}
main();
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
# Python 3 Program
# Find the largest absolute value in array
class AbsoluteValue :
# Find the absolute largest element in given collection
def largest_absolute(self, collection, size) :
# loop controlling variable
i = 0
# Get first element
result = collection[i]
# Define auxiliary variables
a = 0
b = 0
# Traverse the collection elements from 1 to n-1
i = 1
while (i < size) :
# Get initial result
a = result
b = collection[i]
if (a < 0) :
# When [a] value is negative
# Make positive numbers
a = -a
if (b < 0) :
# When [b] value is negative
# Make positive numbers
b = -b
if (a == b) :
# When two elements are equal absolute
# Check That whether previous result is negative or not
if (result < 0) :
# Get new result
result = collection[i]
elif(a < b) :
# Get new result
result = collection[i]
i += 1
return result
# Display element of given collection
def display_element(self, collection, size) :
i = 0
while (i < size) :
print(" ", collection[i] ,"", end = "")
i += 1
print("\n", end = "")
def main() :
obj = AbsoluteValue()
# Define the array elements
collection1 = [4, 2, -12, 6, 11, -5, 9, 3]
collection2 = [8, 2, -3, 7, 1, 31, 12, 11]
collection3 = [1, 1, 2, -5, 1, 5, 4, 3]
# Get the size of given collection1
size = len(collection1)
print("Element : ", end = "")
obj.display_element(collection1, size)
result = obj.largest_absolute(collection1, size)
print("Largest Absolute : ", result ," \n", end = "")
# Get the size of given collection2
size = len(collection2)
print("\nElement : ", end = "")
obj.display_element(collection2, size)
result = obj.largest_absolute(collection2, size)
print("Largest Absolute : ", result ," \n", end = "")
# Get the size of given collection3
size = len(collection3)
print("\nElement : ", end = "")
obj.display_element(collection3, size)
result = obj.largest_absolute(collection3, size)
print("Largest Absolute : ", result ," \n", end = "")
if __name__ == "__main__": main()
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
# Ruby Program
# Find the largest absolute value in array
class AbsoluteValue
# Find the absolute largest element in given collection
def largest_absolute(collection, size)
# loop controlling variable
i = 0
# Get first element
result = collection[i]
# Define auxiliary variables
a = 0
b = 0
# Traverse the collection elements from 1 to n-1
i = 1
while (i < size)
# Get initial result
a = result
b = collection[i]
if (a < 0)
# When [a] value is negative
# Make positive numbers
a = -a
end
if (b < 0)
# When [b] value is negative
# Make positive numbers
b = -b
end
if (a == b)
# When two elements are equal absolute
# Check That whether previous result is negative or not
if (result < 0)
# Get new result
result = collection[i]
end
elsif(a < b)
# Get new result
result = collection[i]
end
i += 1
end
return result
end
# Display element of given collection
def display_element(collection, size)
i = 0
while (i < size)
print(" ", collection[i] ,"")
i += 1
end
print("\n")
end
end
def main()
obj = AbsoluteValue.new()
# Define the array elements
collection1 = [4, 2, -12, 6, 11, -5, 9, 3]
collection2 = [8, 2, -3, 7, 1, 31, 12, 11]
collection3 = [1, 1, 2, -5, 1, 5, 4, 3]
# Get the size of given collection1
size = collection1.length
print("Element : ")
obj.display_element(collection1, size)
result = obj.largest_absolute(collection1, size)
print("Largest Absolute : ", result ," \n")
# Get the size of given collection2
size = collection2.length
print("\nElement : ")
obj.display_element(collection2, size)
result = obj.largest_absolute(collection2, size)
print("Largest Absolute : ", result ," \n")
# Get the size of given collection3
size = collection3.length
print("\nElement : ")
obj.display_element(collection3, size)
result = obj.largest_absolute(collection3, size)
print("Largest Absolute : ", result ," \n")
end
main()
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
/*
Scala Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
def largest_absolute(collection: Array[Int], size: Int): Int = {
//loop controlling variable
var i: Int = 0;
//Get first element
var result: Int = collection(i);
// Define auxiliary variables
var a: Int = 0;
var b: Int = 0;
//Traverse the collection elements from 1 to n-1
i = 1;
while (i < size)
{
//Get initial result
a = result;
b = collection(i);
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection(i);
}
}
else if (a < b)
{
//Get new result
result = collection(i);
}
i += 1;
}
return result;
}
//Display element of given collection
def display_element(collection: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + collection(i) + "");
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: AbsoluteValue = new AbsoluteValue();
// Define the array elements
var collection1: Array[Int] = Array(4, 2, -12, 6, 11, -5, 9, 3);
var collection2: Array[Int] = Array(8, 2, -3, 7, 1, 31, 12, 11);
var collection3: Array[Int] = Array(1, 1, 2, -5, 1, 5, 4, 3);
//Get the size of given collection1
var size: Int = collection1.length;
print("Element : ");
obj.display_element(collection1, size);
var result: Int = obj.largest_absolute(collection1, size);
print("Largest Absolute : " + result + " \n");
//Get the size of given collection2
size = collection2.length;
print("\nElement : ");
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
print("Largest Absolute : " + result + " \n");
//Get the size of given collection3
size = collection3.length;
print("\nElement : ");
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
print("Largest Absolute : " + result + " \n");
}
}
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
/*
Swift Program
Find the largest absolute value in array
*/
class AbsoluteValue
{
//Find the absolute largest element in given collection
func largest_absolute(_ collection: [Int], _ size: Int) -> Int
{
//loop controlling variable
var i: Int = 0;
//Get first element
var result: Int = collection[i];
// Define auxiliary variables
var a: Int = 0;
var b: Int = 0;
//Traverse the collection elements from 1 to n-1
i = 1;
while (i < size)
{
//Get initial result
a = result;
b = collection[i];
if (a < 0)
{
//When [a] value is negative
//Make positive numbers
a = -a;
}
if (b < 0)
{
//When [b] value is negative
//Make positive numbers
b = -b;
}
if (a == b)
{
// When two elements are equal absolute
// Check That whether previous result is negative or not
if (result < 0)
{
//Get new result
result = collection[i];
}
}
else if (a < b)
{
//Get new result
result = collection[i];
}
i += 1;
}
return result;
}
//Display element of given collection
func display_element(_ collection: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", collection[i] ,"", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: AbsoluteValue = AbsoluteValue();
// Define the array elements
let collection1: [Int] = [4, 2, -12, 6, 11, -5, 9, 3];
let collection2: [Int] = [8, 2, -3, 7, 1, 31, 12, 11];
let collection3: [Int] = [1, 1, 2, -5, 1, 5, 4, 3];
//Get the size of given collection1
var size: Int = collection1.count;
print("Element : ", terminator: "");
obj.display_element(collection1, size);
var result: Int = obj.largest_absolute(collection1, size);
print("Largest Absolute : ", result ," \n", terminator: "");
//Get the size of given collection2
size = collection2.count;
print("\nElement : ", terminator: "");
obj.display_element(collection2, size);
result = obj.largest_absolute(collection2, size);
print("Largest Absolute : ", result ," \n", terminator: "");
//Get the size of given collection3
size = collection3.count;
print("\nElement : ", terminator: "");
obj.display_element(collection3, size);
result = obj.largest_absolute(collection3, size);
print("Largest Absolute : ", result ," \n", terminator: "");
}
main();
Output
Element : 4 2 -12 6 11 -5 9 3
Largest Absolute : -12
Element : 8 2 -3 7 1 31 12 11
Largest Absolute : 31
Element : 1 1 2 -5 1 5 4 3
Largest Absolute : 5
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment