0% found this document useful (0 votes)
25 views12 pages

Macadangdang Ipt Activity1

The document discusses programs to perform operations on arrays such as adding the diagonal elements of a two-dimensional array, finding negative elements and their sum in an array, calculating the sum of the maximum and minimum elements in an array, and inserting or deleting elements from an array at specified positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views12 pages

Macadangdang Ipt Activity1

The document discusses programs to perform operations on arrays such as adding the diagonal elements of a two-dimensional array, finding negative elements and their sum in an array, calculating the sum of the maximum and minimum elements in an array, and inserting or deleting elements from an array at specified positions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PANGASINAN STATE UNIVERSITY

Asingan, Pangasinan

Bachelor of Science in Information Technology

Name: John Kelvin A. Macadangdang Score:


Course/Year: BSIT 3 Date Submitted:

ACTIVITY 1

PROBLEM 1: Create a program to add the diagonal of two-dimensional array.


Diagonal are the position where value of x axis and y axis coordinates are
same. For example.
22 50 11 2 49
92 63 12 64 37
75 23 64 12 99
21 25 71 69 39
19 39 58 28 83

CODE: HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Diagonal Sum Calculator</title>
<style>
.highlight {
color: red;
}
</style>
</head>
<body>
<h1>Diagonal Sum Calculator</h1>
<label for="arrayInput">Enter the two-dimensional array (comma separated):</label>
<br>
<textarea id="arrayInput" rows="5" cols="50"></textarea>
<br>
<button onclick="calculateDiagonalSum()">Calculate Diagonal Sum</button>
<br>
<p id="result"></p>

<script>
function calculateDiagonalSum() {
const arrayInput = document.getElementById('arrayInput').value;
const arrayRows = arrayInput.trim().split('\n');
const array = [];

for (let row of arrayRows) {


const rowData = row.trim().split(',').map(Number);
array.push(rowData);

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


}

const diagonalSum = calculateDiagonalSumFromJS(array);


const resultElement = document.getElementById('result');
resultElement.innerHTML = 'Diagonal sum: ' + diagonalSum + '<br>';

for (let i = 0; i < array.length; i++) {


for (let j = 0; j < array[i].length; j++) {
if (i === j) {
resultElement.innerHTML += '<span class="highlight">' + array[i][j] +
'</span> ';
} else {
resultElement.innerHTML += array[i][j] + ' ';
}
}
resultElement.innerHTML += '<br>';
}
}

function calculateDiagonalSumFromJS(array) {
let sum = 0;
const size = Math.min(array.length, array[0].length);

for (let i = 0; i < size; i++) {


sum += array[i][i];
}

return sum;
}
</script>
</body>
</html>

C#
using System;

class program
{
static void Main()
{

int[,] array = {
{ 22, 50, 11, 2, 49 },
{ 92, 63, 12, 64, 37 },
{ 75, 23, 64, 12, 99 },
{ 21, 25, 71, 69, 39 },
{ 19, 39, 58, 28, 83 }
};

int diagonalSum = CalculateDiagonalSum(array);

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


Console.WriteLine("Diagonal sum: " + diagonalSum);
}

static int CalculateDiagonalSum(int[,] array)


{
int sum = 0;
int size = Math.Min(array.GetLength(0), array.GetLength(1));

for (int i = 0; i < size; i++)


{
sum += array[i, i];
}

return sum;
}
}

OUTPUT: (Screen Shoot)

PROBLEM 2: Create a program to input elements in array and print all


negative elements and get the sum of all negative elements.
CODE: HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Negative Elements and Sum Calculator</title>
</head>
<body>
<h1>Negative Elements and Sum Calculator</h1>

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


<label for="arrayInput">Enter the elements of the array (comma separated):</label>
<br>
<input type="text" id="arrayInput" size="50">
<br>
<button onclick="processArray()">Process Array</button>
<br>
<p id="negativeElements"></p>
<p id="sumOfNegatives"></p>

<script>
function processArray() {
const arrayInput = document.getElementById('arrayInput').value;
const array = arrayInput.split(',').map(Number);

let negativeElements = [];


let sumOfNegatives = 0;

for (let num of array) {


if (num < 0) {
negativeElements.push(num);
sumOfNegatives += num;
}
}

document.getElementById('negativeElements').innerText =
'Negative elements: ' + (negativeElements.length > 0 ? negativeElements.join(', ') : 'None');
document.getElementById('sumOfNegatives').innerText =
'Sum of negative elements: ' + sumOfNegatives;
}
</script>
</body>
</html>

C#
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Collections.Generic;
using System.Linq;

public class NegativeElementsAndSumModel : PageModel


{
[BindProperty]
public string ArrayInput { get; set; }

public string NegativeElementsDisplay { get; private set; }


public string SumOfNegativesDisplay { get; private set; }

public void OnGet()


{

public IActionResult OnPost()


{
if (string.IsNullOrEmpty(ArrayInput))
{
ModelState.AddModelError(string.Empty, "Array input cannot be empty.");
return Page();
}

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


var array = ArrayInput.Split(',').Select(int.Parse).ToArray();

var negativeElements = new List<int>();


int sumOfNegatives = 0;
foreach (var num in array)
{
if (num < 0)
{
negativeElements.Add(num);
sumOfNegatives += num;
}
}

NegativeElementsDisplay = negativeElements.Count > 0 ?


$"Negative elements: {string.Join(", ", negativeElements)}" :
"No negative elements found.";

SumOfNegativesDisplay = $"Sum of negative elements: {sumOfNegatives}";

return Page();
}
}

OUTPUT: (Screen Shoot)

PROBLEM 3: Create a program to input elements in an array from user, find


maximum and minimum element in array and get the sum of maximum and
minimum element.
CODE: HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Maximum and Minimum Elements Sum Calculator</title>
</head>
<body>
<h1>Maximum and Minimum Elements Sum Calculator</h1>
<label for="arrayInput">Enter the elements of the array (comma separated):</label>
<br>
<input type="text" id="arrayInput" size="50">
<br>
<button onclick="calculateMaxMinSum()">Calculate Sum</button>

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


<br>
<p id="result"></p>

<script>
function calculateMaxMinSum() {
const arrayInput = document.getElementById('arrayInput').value;
const array = arrayInput.split(',').map(Number);

if (array.length === 0) {
document.getElementById('result').innerText = 'Please enter some elements.';
return;
}

let max = array[0];


let min = array[0];

for (let i = 1; i < array.length; i++) {


if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}

const sum = max + min;


document.getElementById('result').innerText = `Maximum element: ${max}, Minimum
element: ${min}, Sum of max and min: ${sum}`;
}
</script>
</body>
</html>

C#
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System;
using System.Linq;

public class IndexModel : PageModel


{
[BindProperty]
public string ArrayInput { get; set; }
public string ResultMessage { get; private set; }

public void OnGet()


{

public IActionResult OnPost()


{
if (string.IsNullOrEmpty(ArrayInput))

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


{
ResultMessage = "Array input cannot be empty.";
return Page();
}

var array = ArrayInput.Split(',').Select(int.Parse).ToArray();

if (array.Length == 0)
{
ResultMessage = "No elements found in the array.";
return Page();
}

int max = array[0];


int min = array[0];

foreach (var num in array)


{
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
}

int sum = max + min;


ResultMessage = $"Maximum element: {max}, Minimum element: {min}, Sum of max and min:
{sum}";

return Page();
}
}

OUTPUT: (Screen Shoot)

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


PROBLEM 4: Create a program to insert and delete element from array at
specified position.

CODE: HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Manipulation</title>
</head>
<body>
<h1>Array Manipulation</h1>
<p id="output"></p>

<label for="positionInsert">Enter the position where you want to insert a new element:</label>
<input type="number" id="positionInsert">
<label for="elementInsert">Enter the element to insert:</label>
<input type="number" id="elementInsert">
<button onclick="insertElement()">Insert</button>

<label for="positionDelete">Enter the position from where you want to delete an element:</label>
<input type="number" id="positionDelete">
<button onclick="deleteElement()">Delete</button>

<script>
let array = [1, 2, 3, 4, 5];

function printArray() {
document.getElementById("output").innerText = "Array: " + array.join(" ");
}

function insertElement() {
let position = parseInt(document.getElementById("positionInsert").value);
let element = parseInt(document.getElementById("elementInsert").value);

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


if (isNaN(position) || isNaN(element)) {
alert("Please enter valid position and element.");
return;
}
if (position < 0 || position > array.length) {
alert("Position is out of range.");
return;
}
array.splice(position, 0, element);
printArray();
}

function deleteElement() {
let position = parseInt(document.getElementById("positionDelete").value);
if (isNaN(position)) {
alert("Please enter a valid position.");
return;
}
if (position < 0 || position >= array.length) {
alert("Position is out of range.");
return;
}
array.splice(position, 1);
printArray();
}

printArray();
</script>
</body>
</html>

C#
using System;

public partial class Program


{
static void Main(string[] args)
{
int[] array = { 1, 2, 3, 4, 5 };

Console.WriteLine("Original Array:");
PrintArray(array);

Console.WriteLine("\nEnter the position where you want to insert a new element:");


int insertPosition = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the element to insert:");
int insertElement = int.Parse(Console.ReadLine());

array = InsertElement(array, insertElement, insertPosition);

Console.WriteLine("\nArray after insertion:");


PrintArray(array);

Console.WriteLine("\nEnter the position from where you want to delete an element:");


int deletePosition = int.Parse(Console.ReadLine());

array = DeleteElement(array, deletePosition);

Console.WriteLine("\nArray after deletion:");


PrintArray(array);

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


}

static int[] InsertElement(int[] array, int element, int position)


{
int[] newArray = new int[array.Length + 1];
for (int i = 0, j = 0; i < newArray.Length; i++)
{
if (i == position)
{
newArray[i] = element;
}
else
{
newArray[i] = array[j];
j++;
}
}
return newArray;
}

static int[] DeleteElement(int[] array, int position)


{
int[] newArray = new int[array.Length - 1];
for (int i = 0, j = 0; i < array.Length; i++)
{
if (i != position)
{
newArray[j] = array[i];
j++;
}
}
return newArray;
}

static void PrintArray(int[] array)


{
foreach (int element in array)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
}

OUTPUT: (Screen Shoot)

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


PROBLEM 5: Crate a program in to find the fourth largest element in an
array.

CODE: HTML
<!DOCTYPE html>
<html>
<head>
<title>Fourth Largest Element</title>
</head>
<body>
<h1>Fourth Largest Element</h1>
<p>The fourth largest element in the array is:</p>
</body>
</html>

C#
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace YourNamespace.Pages
{
public class FourthLargestModel : PageModel
{
public int FourthLargest { get; set; }

public void OnGet()


{
int[] array = { 10, 5, 15, 20, 25, 30 }; // Example array, you can modify it as needed

FourthLargest = FindFourthLargest(array);
}

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES


private int FindFourthLargest(int[] arr)
{
if (arr.Length < 4)
{
throw new ArgumentException("Array length should be at least 4.");
}

Array.Sort(arr);

return arr[arr.Length - 4];


}
}
}

OUTPUT: (Screen Shoot)

IPT 101 INTEGRATIVE PROGRAMMING AND TECHNOLOGIES

You might also like