Lab06 - Arrays
Lab06 - Arrays
LAB 06
Objective:
Activities:
● Introduction to Arrays.
● Creating flowcharts to demonstrate loop usage to initialize and traverse arrays.
● Exercises on solving real-world problems by using arrays.
No. TASK
1 Create a flowchart to calculate and print the sum of the first N natural numbers using
an Array.
2 Create a flowchart to find the Maximum Value in an Array.
3 Create a flowchart to input and process student names and grades in an array.
4 Create a flowchart for the Voting System to Count Votes for Candidates.
5 Create a flowchart for Library Book Search.
6 Create a flowchart to Remove Duplicates from an Array.
7 BONUS: 5 Real World Scenarios
Breaks & Submission
Submission Instructions:
Flowgorithm Files:
● Save your Flowgorithm flowchart files for each task for Task01.fprg
● File format should be .fprg (Flowgorithm’s native file format).
Submission Format:
● Combine all Flowgorithm file into a single compressed folder (ZIP file).
● Name the folder using the format: YourID_ITC_Lab3.zip
Submit Via:
4. Initialization: Initialize arrays with specific values or take input for them.
5. Loops with Arrays: Use loops to traverse and manipulate arrays (e.g., summing all
elements, and finding the maximum value).
Create a flowchart to calculate and print the sum of the first N natural numbers using an
array.
Steps:
Steps:
1. Declare an array numbers[size] to store the randomly generated numbers (size can
be 5, 10, etc.).
2. Declare max to store the maximum value.
3. Input Array Size: Ask the user for the number of elements (size).
4. Generate Random Numbers: Use a loop to populate the array with random numbers
(e.g., between 1 and 100). Each random number can be generated using random()
5. Find Maximum: Set max to the first element of the array (numbers[0]).
6. Use a loop to iterate through the array from index 1 to size – 1. Compare each
element with max: If numbers[i] > max, update max = numbers[i].
7. Output Maximum: Display the maximum value stored in max.
Create a flowchart to input and process student names and grades in an array.
Input the names and grades of N students. Calculate the average grade. Display each
student's name, grade, and whether they passed (grade ≥ 50) or failed (grade < 50). Display
the average grade.
Steps:
Declare N (number of students), names[N] (to store student names) & grades[N] (to store
student grades), sum (to calculate the total grades, initialize to 0), average (to store the
average grade), i (loop counter), Prompt the user to input the number of students (N).
Output Results
Declare Variables: votes[10] to store the votes from each voter (index corresponds to
candidate numbers 1-5), counts[5] to store the count of votes for each candidate, i (loop
counter), maxVotes (to track the maximum number of votes), winner (to store the winning
candidate).
Input Votes: Use a loop (i from 0 to 9) to prompt each voter for their vote (value between 1
and 5). Increment the corresponding candidate's count in the counts array:
Example: If the voter selects candidate 3, increment counts[2] (array index starts at 0).
Output Results:
Steps:
Declare Variables: books[5] to store the titles of the books, searchTitle (to store the title
entered by the user), found (a Boolean flag, initialized to false), position (to store the index if
the book is found), i (loop counter).
Populate the books array with predefined titles: Example: books[0] = "Harry Potter",
books[1] = "The Hobbit", etc.
Prompt the user to enter the book title they are searching for. Store it in searchTitle.
Use a loop (i from 0 to 4) to compare searchTitle with each element in the books array:
o If books[i] == searchTitle:
Set found = true.
Set position = i.
Exit the loop.
Output the Result: If found = true, display searchTitle and position. Otherwise, display "Book
not found."
You are given an array of integers, some of which might be duplicates. Your task is to
remove the duplicates from the array, such that each number appears only once, and then
output the modified array.
Steps:
Declare Variables: numbers[10] to store the array of 10 integers, i, j (loop counters), n (size
of the array).
Remove Duplicates:
Use two nested loops to compare each element with all subsequent elements:
o Outer loop: Iterate over the array (i from 0 to n-1).
o Inner loop: Compare each element (numbers[i]) with the rest of the
elements (numbers[j] for j > i).
o If a duplicate is found (numbers[i] == numbers[j]):
Shift elements left: All elements from numbers[j+1] to numbers[n-1]
will shift one position to the left.
Decrement n: Reduce the array size by 1 to show unique elements.
Output the Modified Array: After the loops finish, output the modified array, displaying only
the first n elements (unique elements).
Let's shift back to real-world scenario tasks. Here are a few more complex real-life tasks
where algorithms like arrays, loops, and searching/sorting are used. These tasks can help us
connect the theoretical concepts to practical applications.
Problem: Imagine you are managing an inventory for a small retail store. The store sells
different products, each with a unique product code, name, quantity in stock, and price.
Your task is to maintain an inventory list that allows you to:
Stores need to manage products efficiently, track sales, reorder stock, and ensure
they don't run out of popular items.
Retailers use such systems to keep the business running smoothly by knowing what
products are in high demand or running low on stock.
Solution Outline:
Example of Workflow:
Input product data: Name = "Laptop", Code = "P123", Price = 500, Quantity = 10.
Update stock: Add 5 more laptops (Quantity = 15).
Search for a product by its name or code.
Problem: A teacher needs to record and manage student exam results. The goal is to:
Solution Outline:
1. Array of Students: Store student names and their scores as objects or arrays.
2. Calculate Average: Use the formula to calculate each student’s average score.
3. Find Highest Score: Use Linear Search to find the student with the highest score.
Example of Workflow:
Problem: A flight booking system needs to handle bookings for flights, with features such as:
Search for available flights based on departure city, destination, and date.
Book a flight by selecting the cheapest available option.
Track booked flights and display customer details.
This type of system is used by airlines, travel agencies, and booking platforms.
Customers want to find the best prices for their flights, while agencies want to
manage flights efficiently.
Solution Outline:
1. Array of Flights: Store information like departure city, destination, flight time, and
price.
2. Search Flights: Implement a Linear Search based on the search criteria (e.g.,
departure city or destination).
3. Booking: Select the cheapest flight by comparing prices and book it.
Example of Workflow:
Solution Outline:
1. Array of Cart Items: Each item has a name, price, and quantity.
2. Add/Remove Items: Use array manipulation to add or remove items.
3. Calculate Total Price: Iterate through the array, multiplying item prices by quantities.
4. Apply Discount: If the total exceeds a certain threshold, apply a discount.
5. Output Final Price.
Example of Workflow:
Conclusion:
These real-world scenarios involve handling arrays, loops, and sorting/searching algorithms
to solve common problems like managing inventory, tracking student grades, booking flights,
managing online shopping carts, and ranking social media posts. These tasks are practical
and can help us see how their programming skills apply to real-world challenges.