Alvin Compile
Alvin Compile
BSCpE 2F1
namespace EXERCISE_1_DSA
{
class Program
{
static void Main(string[] args)
{
int a;
1. START
2. DECLARE a AS INTEGER
3. DECLARE input AS STRING
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EXERCISE_2_DSA
{
class Program
{
static void Main(string[] args)
{
int R, C;
1. START
2. DECLARE R AS INTEGER
3. DECLARE C AS INTEGER
8. FOR i FROM 1 TO C DO
8.1 PRINT NEW LINE
8.2 FOR j FROM 1 TO R DO
8.3 DISPLAY (i * j) followed by a tab
8.4 END FOR
9. END FOR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise3try
{
class Program
{
static void Main(string[] args)
{
int n1, answer;
answer = 0;
do
{
Console.Clear();
Console.Write("Enter any positive number: ");
n1 = Convert.ToInt32(Console.ReadLine());
if (n1 <= 0)
{
Console.Write("Try again, positive number only");
Console.ReadLine();
Console.Clear();
}
}
while (n1 <= 0);
{
for (int number = 1; number <= n1; number++)
{
answer += number;
Console.Write(number);
if (number < n1)
Console.Write(" + ");
}
Console.WriteLine(" = " + answer);
Console.ReadLine();
}
}
}
}
EXERCISE 3 (Calculates the SUM of the First n Natural Numbers Flow Chart)
EXERCISE 3 (Calculates the SUM of the First n Natural Numbers Pseudocode)
1. START
2. DECLARE n1 AS INTEGER
3. DECLARE answer AS INTEGER ← 0
4. REPEAT
4.1 CLEAR screen
4.2 DISPLAY "Enter any positive number: "
4.3 READ n1
5. IF n1 <= 0 THEN
5.1 DISPLAY "Try again, positive number only"
5.2 WAIT for user to press a key
5.3 CLEAR screen
6. END IF
7. UNTIL n1 > 0
25. END
EXERCISE 7 (AMOUNT AND PERCENTAGE OF LOAN AND YEARS TO BE PAID CODE)
Console.WriteLine("____________________________________
using System; ____________________________________");
using System.Collections.Generic;
using System.Linq; for ( int loanmonths = 1; loanmonths <= months;
loanmonths++)
using System.Text;
{
namespace Loan
double interespermonth, principlepermonth;
{
interespermonth = R_Balance * rate;
class Program
principlepermonth = M_payment - interespermonth;
{
T_Paid += M_payment + interespermonth;
static void Main(string[] args)
R_Balance -= principlepermonth;
{
T_Irate += interespermonth;
t_mpayment += M_payment;
double percent, rate, loan, T_Paid, R_Balance, T_Irate,
t_mpayment, t_int, paid, balance;
int year; Console.WriteLine(" {0,-7} | {1, -15:0.00} | {2, -8:0.00} |
{3, -10:0.00} | {4, -10:0.00} |", loanmonths, M_payment,
interespermonth, T_Paid, R_Balance);
Console.Write("Enter Amount of Loan: "); if (loanmonths % 12 == 0)
loan = Convert.ToDouble(Console.ReadLine()); {
Console.Write("Enter Percentage: ");
percent = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter Loan Duration (in years): "); t_int = T_Irate;
year = Convert.ToInt32(Console.ReadLine()); paid = T_Paid;
balance = R_Balance;
int months = year*12;
Console.WriteLine("-------------------------------------------------------------
rate= (percent/100)/12;
-----------");
Console.WriteLine("YEAR {0}: | {1, -15:0.00} | {2,
double M_payment = loan*(rate * Math.Pow(1 + rate, -8:0.00} | {3, -10:0.00} | {4, -10:0.00} |", loanmonths/12 ,
months))/ (Math.Pow(1 + rate, months) - 1); t_mpayment, t_int, paid, balance);
Console.WriteLine("-------------------------------------------------------------
T_Paid = 0; -----------");
T_Irate = 0; }
R_Balance = loan;
t_mpayment = 0; }
t_int=0;
paid=0; double T_LAmount = T_Irate + loan;
6. DISPLAY "Enter Amount of Loan: " 24. DISPLAY loanmonths, M_payment, interespermonth,
T_Paid, R_Balance
7. READ loan
do
case 2:
{
Console.Clear();
Console.Clear();
Console.WriteLine("You selected View All
Console.WriteLine("\n-------------------------------");
Record");
Console.WriteLine("-------------MENU--------------");
Console.WriteLine("\n---Loan Records---");
Console.WriteLine("-------------------------------");
if (count == 0)
Console.WriteLine("1. Add Loan Record");
{
Console.WriteLine("2. View All Records");
Console.WriteLine("No records found.");
Console.WriteLine("3. Search Loan Records");
}
Console.WriteLine("4. Delete Loan Record");
Console.WriteLine("5. Exit");
else
Console.WriteLine("\nEnter Your Choice: ");
{
for (int i = 0; i < count; i++)
choices = Convert.ToInt32(Console.ReadLine());
{
double principal = loan_amount[i];
double annualRate = interest_rate[i];
switch (choices)
int years = year[i];
{
case 1:
double monthlyRate = (annualRate / 100) /
Console.Clear(); 12;
{
Console.Write("You selected Add Loan double monthlyPayment = (principal *
Record"); monthlyRate * Math.Pow(1 + monthlyRate, totalMonths))
/(Math.Pow(1 + monthlyRate, totalMonths) - 1);
Console.Write("\nEnter Name: ");
names[count] = Console.ReadLine();
double totalPayment = monthlyPayment *
Console.Write("Enter Loan Amount: "); totalMonths;
Console.WriteLine("\nLoan Record
Found!");
Console.WriteLine("------------------------------------------------------------- Console.WriteLine("Name: " + names[i]);
-----------------------------------------------------------");
Console.WriteLine("Loan Amount: " +
Console.WriteLine("|Name | Loan loan_amount[i]);
Amount | Interest Rate | Years To Pay | Monthly
Payment | Total Payment |"); Console.WriteLine("Interest Rate: " +
interest_rate[i]);
Console.WriteLine(String.Format("{0,-12} |
{1,-17} | {2,-19} | {3,-18} | {4,-21} | {5,-0} ", names[i] , Console.WriteLine("Years to Pay: " +
loan_amount[i] , interest_rate[i] , year[i] , year[i]);
( Math.Round(monthlyPayment, 2)) , ( Math.Round(totalPayment,
2)))); Console.WriteLine("Monthly Payment: " +
Math.Round(monthlyPayment, 2));
} found = true;
} break;
Console.ReadKey(); }
break; if (!found)
{
searchName = Console.ReadLine(); }
{ Console.Clear();
} }
}
CASE 3:
PROMPT "Enter Loan Amount"
DISPLAY "Search Loan Record"
READ loan_amount[count]
PROMPT "Enter Name"
READ searchName
PROMPT "Enter Interest Rate"
SET found = false
READ interest_rate[count]
if (!found)
if (!found)
Console.WriteLine("Person not found in Female
Console.WriteLine("Person not found in Male stack.");
stack.");
}
}
else
else
{
Console.WriteLine("No females in the stack.");
} Console.WriteLine("\nAll Persons in Stack:");
} if (mstack.Count == 0 && fstack.Count == 0)
else Console.WriteLine("No persons in the stack.");
{ else
Console.WriteLine("Invalid gender input. Please enter {
'Male' or 'Female'.");
Console.WriteLine("+----------------------+-------+---------+");
}
foreach (Person person in mstack.ToArray())
Console.WriteLine("| {0,-20} | {1,-5} | {2,-7} |",
Console.ReadKey(); person.Name, person.Age, person.Gender);
} foreach (Person person in fstack.ToArray())
static void show() Console.WriteLine("| {0,-20} | {1,-5} | {2,-7} |",
person.Name, person.Age, person.Gender);
{
Console.WriteLine("+----------------------+-------+---------+");
Console.Clear();
}
Console.WriteLine("Male Stack:");
Console.ReadKey();
if (mstack.Count == 0)
}
Console.WriteLine("No males in the stack.");
static void countgender()
else
{
{
Console.Clear();
Console.WriteLine("+----------------------+-------+");
int malecount = mstack.Count;
foreach (Person person in mstack.ToArray())
int femalecount = fstack.Count;
Console.WriteLine("| {0,-20} | {1,-5} |", person.Name,
person.Age); int totalcount = malecount + femalecount;
Console.WriteLine("+----------------------+-------+"); string male = "Male: " + malecount.ToString();
} string female = "Female: " + femalecount.ToString();
Console.WriteLine("\nFemale Stack:"); string total = "Total: " + totalcount.ToString();
if (fstack.Count == 0) Console.WriteLine("Count by Gender:");
Console.WriteLine("No females in the stack."); Console.WriteLine(male);
else Console.WriteLine(female);
{ Console.WriteLine(total);
Console.WriteLine("+----------------------+-------+---------+");
foreach (Person person in fstack.ToArray()) Console.ReadKey();
Console.WriteLine("| {0,-20} | {1,-5} |", person.Name, }
person.Age);
}
Console.WriteLine("+----------------------+-------+---------+");
}
}
F ACTIVITY 1 (INFORMATION SYSTEM FLOWCHART)
F ACTIVITY 1 (INFORMATION SYSTEM PSEUDOCODE)
START WAIT for key press
BREAK
DEFINE class Person:
- Name (string) CASE "2": // Delete name from gender stack
- Age (int) CLEAR screen
- Gender (string) PROMPT for gender (Male/Female)
CONSTRUCTOR (name, age, gender) READ gender
CONVERT gender to lowercase
DECLARE two stacks:
- mstack for males IF gender == "male" THEN
- fstack for females IF mstack is not empty THEN
DISPLAY all names and ages in mstack
LOOP forever: PROMPT for name to delete
CLEAR screen READ nameToDelete
DISPLAY menu:
1. Input Name, Age, Gender INITIALIZE temporaryStack
2. Delete Name SET found = false
3. Show All
4. Count by Gender WHILE mstack not empty DO
5. Exit POP person
IF person.Name == nameToDelete THEN
PROMPT for choice DISPLAY "Removed"
READ choice SET found = true
BREAK
SWITCH (choice): ELSE
PUSH person to temporaryStack
CASE "1": // Input details
CLEAR screen WHILE temporaryStack not empty
PROMPT for name PUSH back to mstack
READ name
PROMPT for age IF not found
READ age (validate if integer) DISPLAY "Person not found"
IF invalid THEN
DISPLAY error and RETURN ELSE
PROMPT for gender (Male/Female) DISPLAY "No males in the stack"
READ gender
CONVERT gender to lowercase ELSE IF gender == "female" THEN
IF fstack is not empty THEN
IF gender == "male" THEN DISPLAY all names and ages in fstack
PUSH new Person to mstack PROMPT for name to delete
ELSE IF gender == "female" THEN READ nameToDelete
PUSH new Person to fstack
ELSE INITIALIZE temporaryStack
DISPLAY "Invalid gender" SET found = false
WHILE fstack not empty DO DISPLAY contents of fstack
POP person
IF person.Name == nameToDelete THEN DISPLAY all persons from both stacks (name, age,
gender)
DISPLAY "Removed"
SET found = true
WAIT for key press
BREAK
BREAK
ELSE
PUSH person to temporaryStack
CASE "4": // Count by Gender
CLEAR screen
WHILE temporaryStack not empty
COUNT males in mstack
PUSH back to fstack
COUNT females in fstack
DISPLAY male count
IF not found
DISPLAY female count
DISPLAY "Person not found"
DISPLAY total count
ELSE
WAIT for key press
DISPLAY "No females in the stack"
BREAK
ELSE
DISPLAY "Invalid gender input"
CASE "5":
EXIT program
WAIT for key press
BREAK
DEFAULT:
DISPLAY "Invalid choice"
CASE "3": // Show All
CLEAR screen
END LOOP
DISPLAY contents of mstack
F EXERCISE 2 (PET SYSTEM CODE)
if (gender == "male")
foreach (Pet Pet in femaleStack)
{
if (Pet.Name.Equals(name,
if (maleStack.Count > 0) StringComparison.OrdinalIgnoreCase))
{ {
Pet removed = maleStack.Pop(); Console.WriteLine("Found: " + Pet.Name + "Type: " +
Pet.Type + ", Age: " + Pet.Age + ", Gender: " + Pet.Gender);
Console.WriteLine(removed.Name + " was
removed."); found = true;
} break;
else }
Console.WriteLine("No males in the stack.");
} if (!found)
else if (gender == "female") {
{ Console.WriteLine("Name not found.");
if (femaleStack.Count > 0) }
{ Console.Write("Press any key to continue......");
Pet removed = femaleStack.Pop(); Console.ReadKey();
Console.WriteLine(removed.Name + " was }
removed.");
}
static void DisplayStack()
else
{
Console.WriteLine("No females in the stack.");
Console.Clear();
}
else Console.WriteLine("===================================
==============");
{
Console.WriteLine("Male Pets Stack:");
Console.WriteLine("Invalid gender input.");
if (maleStack.Count == 0)
}
Console.WriteLine("No Pets students in the stack.");
Console.Write("Press any key to continue......");
else
foreach (Pet Pet in maleStack.ToArray()) maleCount++;
Console.WriteLine(Pet.Name + ", Type: " + Pet.Type }
+ ", Age: " + Pet.Age);
foreach (var student in femaleStack)
{
Console.WriteLine("\
n================================================") femaleCount++;
;
}
Console.WriteLine("Total Male Pets:" + maleCount);
Console.WriteLine("Female Pets Stack:");
Console.WriteLine("Total Female Pets:" + femaleCount);
if (femaleStack.Count == 0)
int total = maleCount + femaleCount;
Console.WriteLine("No female Pets in the stack.");
Console.WriteLine("Total Pets:" + total);
else
Console.WriteLine("\
foreach (Pet Pet in femaleStack.ToArray()) n===========================================");
Console.ReadKey();
Console.WriteLine("\
n================================================")
;
}
static void UpdatePet()
Console.WriteLine("\nAll Students in Stack:");
{
if (maleStack.Count == 0 && femaleStack.Count == 0)
Console.Clear();
Console.WriteLine("No Students in the stack.");
Console.Write("Enter the name of the pet to update: ");
else
string name = Console.ReadLine();
{
foreach (Pet Pet in maleStack.ToArray())
Console.WriteLine(Pet.Name + ", Type: " + Pet.Type
bool updated = false;
+ ", Age: " + Pet.Age + ", Gender: " + Pet.Gender);
Stack<Pet> tempMale = new Stack<Pet>();
while (maleStack.Count > 0)
foreach (Pet Pet in femaleStack.ToArray())
{
Console.WriteLine(Pet.Name + ", Type: " + Pet.Type
+ ", Age: " + Pet.Age + ", Gender: " + Pet.Gender); Pet pet = maleStack.Pop();
Console.ReadKey(); if (pet.Name.Equals(name,
StringComparison.OrdinalIgnoreCase))
}
{
}
Console.Write("Enter new name: ");
pet.Name = Console.ReadLine();
Console.Write("Enter new age: ");
static void CountStack()
pet.Age = int.Parse(Console.ReadLine());
{
Console.Write("Enter new gender (Male/Female): ");
Console.Clear();
pet.Gender = Console.ReadLine();
Console.WriteLine("=======Pet Count=======");
Console.Write("Enter new type of pet: ");
Console.WriteLine("\
n========================================"); pet.Type = Console.ReadLine();
int maleCount = 0;
int femaleCount = 0; if (pet.Gender.ToLower() == "female")
{
foreach (var student in maleStack) femaleStack.Push(pet);
{ updated = true;
continue; {
} maleStack.Push(pet);
updated = true;
updated = true; continue;
} }
tempMale.Push(pet);
} updated = true;
}
while (tempMale.Count > 0) tempFemale.Push(pet);
maleStack.Push(tempMale.Pop()); }
if (pet.Gender.ToLower() == "male")
F EXERCISE 2 (PET SYSTEM FLOWCHART)
F EXERCISE 2 (PET SYSTEM PSEUDOCODE)
case "2":
while (true)
Console.Write("Enter number to search: ");
{
string searchInput = Console.ReadLine();
Console.Clear();
int searchVal;
Console.WriteLine("\nChoose an operation:");
if (int.TryParse(searchInput, out searchVal))
Console.WriteLine("1 - Add");
{
Console.WriteLine("2 - Search");
bool found = bst.Search(searchVal);
Console.WriteLine("3 - Delete");
if (found)
Console.WriteLine("4 - Show In-Order Traversal");
Console.WriteLine("Found " + searchVal);
Console.WriteLine("0 - Exit");
else
Console.Write("Your choice: ");
Console.WriteLine(searchVal + " not found");
string choice = Console.ReadLine();
}
else
switch (choice)
{
{
Console.WriteLine("Invalid number.");
case "1":
}
Console.Write("How many nodes do you want to
add? "); Console.ReadKey();
string input = Console.ReadLine(); break;
int count;
if (int.TryParse(input, out count)) case "3":
{ Console.Write("Enter number to delete: ");
string deleteInput = Console.ReadLine(); bst.InOrder();
int deleteVal; Console.ReadKey();
if (int.TryParse(deleteInput, out deleteVal)) break;
{
bst.Delete(deleteVal); case "0":
Console.WriteLine("Deleted " + deleteVal + " (if it Console.WriteLine("Exiting...");
existed)");
return;
}
else
default:
{
Console.WriteLine("Invalid choice.");
Console.WriteLine("Invalid number.");
Console.ReadKey();
}
break;
Console.ReadKey();
}
break;
}
}
case "4":
}
Console.WriteLine("In-Order Traversal:");
BST Flowchart
BST Pseudocode
START RETURN SearchRec(root.Left, key)
ELSE
CLASS Node RETURN SearchRec(root.Right, key)
DECLARE Integer Key END FUNCTION
DECLARE Node Left, Right
FUNCTION Delete(key)
CONSTRUCTOR Node(item) SET Root = DeleteRec(Root, key)
SET Key = item END FUNCTION
SET Left = null
SET Right = null FUNCTION DeleteRec(root, key)
END CLASS IF root IS null THEN
RETURN root
CLASS BinarySearchTree END IF
DECLARE Node Root
IF key < root.Key THEN
FUNCTION Insert(key) SET root.Left = DeleteRec(root.Left, key)
CALL InsertRec(Root, key) AND ASSIGN TO Root ELSE IF key > root.Key THEN
END FUNCTION SET root.Right = DeleteRec(root.Right, key)
ELSE
FUNCTION InsertRec(root, key) IF root.Left IS null THEN
IF root IS null THEN RETURN root.Right
RETURN new Node(key) ELSE IF root.Right IS null THEN
END IF RETURN root.Left
AVL Flowchart
AVL Pseudocode
FUNCTION Main()
CREATE AVLTree tree
DECLARE INTEGER choice, value
DO
PRINT menu:
1. Add
2. Search
3. Delete
4. Show In-Order
5. Exit
SWITCH (choice)
CASE 1:
PROMPT for value
CALL tree.Add(value)
CASE 2:
PROMPT for value
CALL tree.SearchKey(value)
CASE 3:
PROMPT for value
CALL tree.Remove(value)
CASE 4:
PRINT "In-order Traversal: "
CALL tree.ShowInOrder()
CASE 5:
PRINT "Exiting..."
DEFAULT:
PRINT "Invalid choice!"
WHILE choice != 5
END FUNCTION
ADT Code
using System; }
switch (choice)
{
case 1:
Console.Write("Enter value to add: ");
value = Convert.ToInt32(Console.ReadLine());
tree.Add(value);
ADT Flowchart
ADT Pseudocode
Start
Create an empty RedBlackTree
Repeat
Display menu:
1. Add
2. Remove
3. Search
4. Show InOrder
0. Exit
Input choice
If choice = 1 then
Input value
Call Add(value)
Else if choice = 2 then
Input value
Call Remove(value)
Else if choice = 3 then
Input value
Call Search(value)
Else if choice = 4 then
Call InOrder()
Else if choice = 0 then
Display "Exiting..."
Else
Display "Invalid option"
Until choice = 0
End
Activity 3 graph code
using System; Console.WriteLine("Vertex " + vertex + " added.");
using System.Collections.Generic; }
}
class Program
{ static void AddEdge(int a, int b)
static Dictionary<int, List<int>> graph = new Dictionary<int, {
List<int>>();
if (!graph.ContainsKey(a)) AddVertex(a);
if (!graph.ContainsKey(b)) AddVertex(b);
static void Main()
{
graph[a].Add(b);
InitializeGraph();
graph[b].Add(a);
AddVertex(6);
Console.WriteLine("Edge added between " + a + " and " + b
AddEdge(5, 6); + ".");
RemoveEdge(1, 4); }
End
Activity 1 Graph Code
using System; visited.Add(neighbor);
using System.Collections.Generic; queue.Enqueue(neighbor);
}
class Program }
{ }
static Dictionary<string, List<string>> graph = new }
Dictionary<string, List<string>>();
Console.ReadLine();
}
var neighbors = new List<string>(graph[node]);
neighbors.Reverse();
static void BFS(string start)
{
foreach (var neighbor in neighbors)
var visited = new HashSet<string>();
{
var queue = new Queue<string>();
if (!visited.Contains(neighbor))
{
visited.Add(start);
stack.Push(neighbor);
queue.Enqueue(start);
}
}
while (queue.Count > 0)
}
{
}
string node = queue.Dequeue();
}
Console.Write(node + " ");
}
Start
Initialize graph:
graph["A"] ← ["B", "C"]
graph["B"] ← ["A", "C", "E"]
graph["C"] ← ["A", "B", "D"]
graph["D"] ← ["C", "E"]
graph["E"] ← ["B", "D"]
Console.ReadLine();
}
if (!visited.Contains(node))
{
visited.Add(node);
if (node == goal)
{
Console.WriteLine(string.Join(" -> ", path));
Activityreturn;
2 Graph Pseudocode
}
Start
Create an empty graph (dictionary of string to list of strings)
"Taguig" → ["Makati", "Pasig"]
Add connections:
"Manila" → ["Quezon", "Makati"] Display "BFS Path from Manila to Taguig:"
"Quezon" → ["Manila", "Pasig"] Call BFS("Manila", "Taguig")
"Makati" → ["Manila", "Pasig", "Taguig"] End
"Pasig" → ["Quezon", "Makati", "Taguig"]
Activity 2 Graph Flowchart