0% found this document useful (0 votes)
19 views10 pages

Lab06 - Arrays

Uploaded by

Haider S. Ahad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Lab06 - Arrays

Uploaded by

Haider S. Ahad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

University of Central Punjab (UCP)

FOIT&CS - Dept. Of Software Engineering

CP103 - Introduction to Computing (ITC) - M9

LAB 06

Flowgorithm – Introduction to Arrays

Objective:

Implement Arrays in Flowcharts

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:

 Upload the ZIP file to the Lab course’s Horizon Portal.


 Submission is only allowed during the Lab hours and within the Lab.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ①
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering
Key Concepts to Introduce Arrays

1. Definition: An array is a collection of variables of the same type, stored in


contiguous memory locations and accessed using indices.
Example: A list of 5 test scores.

2. Example in Flowgorithm: Declare scores[5] as Integer


This creates an array to store 5 integers.

3. Accessing Elements: Access individual elements using their index.


Example: scores[0] refers to the first element.

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:

1. Declare N (highest number) to get input from the user.


2. Declare an array numbers[N] and a variable sum initialized to 0.
3. Use a loop to input values into the array.
4. Use another loop to add each array element to sum.
5. Display sum.

Create a flowchart to find the Maximum Value in an Array of randomly generated


numbers.

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.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ②
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

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).

Use a loop (i from 0 to N-1):

o Prompt the user to input names[i] (name of the student).


o Prompt the user to input grades[i] (grade of the student).
o Add grades[i] to sum.

Output Results

 Use another loop (i from 0 to N-1):


o Display names[i] and grades[i].
o If grades[i] >= 50, display "Passed". Otherwise, display "Failed".
 Display the average grade. (average = sum / N)

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ③
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

Create a flowchart for Voting System to Count Votes for Candidates.

 A class election is held with 5 candidates.


 Input the votes cast by 10 voters (each voter selects one candidate).
 Count the total votes for each candidate.
 Display the number of votes for each candidate and the winner.

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).

Find the Winner:

 Set maxVotes = counts[0] and winner = 1.


 Use a loop to traverse the counts array:
o If counts[i] > maxVotes, update maxVotes and winner.

Output Results:

 Use a loop to display the vote count for each candidate.


 Display the winning candidate (winner) and their total votes (maxVotes).

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ④
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

Create a flowchart for Library Book Search.

 A library has 5 books, and their titles are stored in an array.


 Allow the user to input a book title to search for.
 If the book is found, display its position in the library (array index).
 If not, display a "Book not found" message.

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."

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑤
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

Create a flowchart to Remove Duplicates from an Array.

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.

1. Scenario: Inventory Management System

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:

 Add new products to the inventory.


 Update stock levels when new products arrive.
 Search for a product by name or code.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑥
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering
Real-world Use Case:

 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:

1. Array of Products: Each product is represented by an object that contains the


product's name, code, quantity, and price.
2. Search: Implement Linear Search to find products by name or product code.
3. Update: Allow users to increase or decrease the stock by updating the quantity.
4. Add Products: Input new product details and store them in the array.

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.

2. Scenario: Student Exam Results

Problem: A teacher needs to record and manage student exam results. The goal is to:

 Input student names and their scores.


 Calculate average score for each student.
 Find the student with the highest score.

Real-world Use Case:

 Schools, colleges, and online courses maintain records of students’ scores.


 Analyzing student performance, such as finding top performers or calculating class
averages, is a common task.

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.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑦
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

Example of Workflow:

 Input: Student Name = "John", Score = 85.


 Calculate Average: Average = 85.
 Find Highest: Identify the student with the highest score (e.g., 95).

3. Scenario: Flight Booking System

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.

Real-world Use Case:

 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:

 Input: Departure City = "New York", Destination = "London", Date = "2024-12-15".


 Search: Find flights matching the criteria.
 Book: Select the cheapest flight and confirm booking.

4. Scenario: Online Shopping Cart

Problem: An online shopping cart needs to:

 Add items to the cart.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑧
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering
 Remove items from the cart.
 Calculate the total price of all items.
 Apply discounts based on the total price (e.g., 10% off for purchases over $100).
 Display the final price.

Real-world Use Case:

 E-commerce websites like Amazon or eBay allow customers to manage their


shopping cart and calculate discounts before checking out.
 This involves arrays to track items and their prices, loops to calculate totals, and
conditional checks to apply discounts.

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:

 Add Items: Item = "Shoes", Price = $50, Quantity = 2.


 Calculate Total: Total = (2 * 50) = $100.
 Apply Discount: If total > $100, apply 10% off.
 Final Price: Output the total price after discount.

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.

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑨
University of Central Punjab (UCP)
FOIT&CS - Dept. Of Software Engineering

CP103 - Introduction to Computing (ITC) - M9


Course Instructor: Mr. Haider S. Ahad & Lab Instructor: Ms. Iqra Javed ⑩

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy