0% found this document useful (0 votes)
4 views

ITC_Lab Week 11

Uploaded by

za326584
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ITC_Lab Week 11

Uploaded by

za326584
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Computing – Lab

Faculty of Information Technology & Computer Science

Topics: Arrays

Instructions:
• Create separate C++ source files for each task, named as “task1.cpp”, “task2.cpp”, and so on
depending on the task number.

• After completing all tasks, place all .cpp files into a folder. Name the folder with your
university registration number (e.g., L1F20BSCS0999). Compress the folder into a .zip file
and upload it on the portal.

• Ensure that the work you submit is entirely your own. Avoid copying from peers, online
sources, or any other unauthorized material. Plagiarism will not be tolerated.

• If you encounter difficulties, feel free to reach out to the instructor. Collaboration and
discussion are encouraged, but the final implementation should be your own work.

• Write clean and well-structured code. Use comments to explain key sections of your code to
make it easier for others (and yourself) to understand.

• These tasks are designed to help you strengthen your logical thinking and problem-solving
skills. Think through each problem carefully before starting to code. The aim is to develop a
deep understanding of the problem and to devise solutions independently.

• Learning programming is about practice and perseverance—genuine effort in solving the


problems will contribute significantly to your learning.
Arrays
An array is a collection of elements of the same data type stored in contiguous memory
locations. Arrays are useful for storing multiple values and processing them with ease. In C++,
an array's size must be specified at the time of declaration and cannot be changed afterward.

Declaring and Initializing an Integer Array

Accessing and Modifying Individual Elements


Array elements are accessed using their index (starting from 0).

Array Traversal
You can use loops like for or while to iterate over the array.
Partial Initialization
Uninitialized elements are automatically set to 0.

Out of Bound Index


Accessing an index outside the array's size results in undefined behavior.

Size of an Array
To get the size of an int array in C++, divide the total size of the array by the size of an int:
Task 1
Write a program to declare an integer array of size 5, initialize it with values {10, 20, 30, 40, 50},
and use a loop to print all the elements.

• Expected Output: 10, 20, 30, 40, 50

Task 2

Write a program to declare an integer array of size 5 with values {5, 10, 15, 20, 25}, calculate
the sum of its elements, and find their average.

• Expected Output: Sum of elements: 75, Average: 15

Task 3
Write a program to declare an integer array of size 5 with values {3, 8, 1, 6, 2}. Find and print
the largest and smallest elements in the array.

• Expected Output: Largest Element: 8, Smallest Element: 1

Task 4
Write a program to declare an integer array of size 5 with values {1, 2, 3, 4, 5} and reverse its
elements.

• Expected Output: 5, 4, 3, 2, 1

Task 5
Write a program to declare an integer array of size 5 with values {1, 2, 3, 4, 5}. Count and print
how many even and odd numbers the array contains.

• Expected Output: The array contains 2 even numbers and 3 odd numbers.
Task 6
Write a program to declare an integer array of size 5 with values {10, 20, 30, 40, 50} and shift all
its elements to the left by one position. The first element should move to the end.

• Expected Output: 20, 30, 40, 50, 10

Task 7
Write a program to declare two integer arrays of size 5. Initialize the first array with values {1, 2,
3, 4, 5} and copy its elements into the second array.

• Expected Output: Original Array: 10, 20, 30, 40, 50, Copied Array: 10, 20, 30, 40, 50

Task 8
Write a program to declare an integer array of size 5 with values {7, 5, 9, 2, 8} and find the
second largest element.

• Expected Output: Second Largest Element: 8

Task 9
Write a program to declare an integer array of size 5 with values {3, 6, 9, 12, 15}. Check if a
specific number exists in the array.

• Expected Output: Number to check: 9, The number 9 is found in the array.

Task 10
Write a program to declare two integer arrays of size 3 with values {1, 2, 3} and {4, 5, 6}. Merge
them into a third array and print the result.

• Expected Output: New Array: 1, 2, 3, 4, 5, 6

You might also like