Arrays: Warm-up
Hello coders : )
Studying and learning the concepts is an excellent way to have a grasp on them but sometimes or I would say most of the time, practicing problems on those topics helps you to understand the topics way beyond what you expected and even helps us to remember them more efficiently.
So in this module, we are going to see and solve a variety of problems on arrays and I assure you that you will become way more confident in this topic by the time you end it : )
If you have solved previous practice modules then you might already know the key to practice in programming: Dry run
Dry run is like a rehearsal before a performance. It's like going through the script in your head, making sure everything makes sense and works well. This way, when the actual code runs, it's more likely to do its job without any surprises or mistakes.
Let's solve now 🚀
Problem 1: Find min & max element of an array
Write a C++ program to input elements in an array from the user, and find the maximum and minimum elements in the array.
Input
Input size of array: 5
Input array elements: 10, 50, 12, 16, 2
Output
Maximum = 50
Minimum = 2
Intuition
We know how many numbers are there in total, and we store this count in a variable called size
. Now, we want to find the biggest (maximum) and smallest (minimum) numbers in this array. To do this, we create two variables called max
and min
. For initialization, assume that the first number in the array is both the maximum and the minimum. So, you say, max = arr[0]
and min = arr[0]
.
But how can we update these values and make them finally equal to their actual values i.e. the maximum and minimum value out of array elements? Now, here's where the loop comes in. You decide to go through each number in the array, one by one, to see if any of them is larger than your assumed maximum (max
) or smaller than your assumed minimum (min
). Your loop starts with the first number (i = 0
), and then goes till you reach the end of the array i.e. (i < size
).
- For each number in the array, you check if it's larger than your assumed maximum (
max
). If it is, you update your assumption, saying, "Hey, I found a bigger number, let's makemax
equal to this number." - Similarly, you check if the number is smaller than your assumed minimum (
min
). If it is, you say, "Oh, this one is smaller. Let's makemin
equal to this number."
After going through all the numbers in the array, you've checked and updated your assumptions. At this point, your max
holds the largest number in the array, and your min
holds the smallest. Now simply print them : )
Logic
- Input size and element in array, store it in some variable say
size
andarr
. - Declare two variables
max
andmin
to store maximum and minimum. Assume first array element as maximum and minimum both, saymax = arr[0]
andmin = arr[0]
. - Iterate through array to find maximum and minimum element in array. Run loop from first to last array element i.e. 0 to
size - 1
. Loop structure should look likefor(i=0; i<size; i++)
. - Inside loop for each array element check for maximum and minimum. Assign current array element to
max
, if(arr[i] > max)
. Assign current array element tomin
if it is less thanmin
i.e. performmin = arr[i]
if(arr[i] < min)
.
Solution
#include <iostream>
using namespace std;
int main() {
int i, max, min, size;
/* Input size of the array */
cout << "Enter size of the array: " << endl;
cin >> size;
/* Create array */
int arr[size];
/* Input array elements */
cout << "Enter elements in the array: " << endl;
for(i = 0; i < size; i++) {
cin >> arr[i];
}
/* Assume first element as maximum and minimum */
max = arr[0];
min = arr[0];
/* Find maximum and minimum in all array elements. */
for(i = 1; i < size; i++) {
/* If current element is greater than max */
if(arr[i] > max) {
max = arr[i];
}
/* If current element is smaller than min */
if(arr[i] < min) {
min = arr[i];
}
}
/* Print maximum and minimum element */
cout << "Maximum element = " << max << endl;
cout << "Minimum element = " << min << endl;
return 0;
}
Problem 2: Count odd & even elements in an array
Write a C++ program to input elements in array from user and count even and odd elements in array.
Input
Input size of array: 9
Input array: 1 2 3 4 5 6 7 8 9
Output
Total even elements: 4
Total odd elements: 5
Intuition
We know how many numbers are there in total, and we store this count in a variable called size
. Now, we want to know how many of these numbers are even and how many are odd. To do this, we will create two variables called even
and odd
to store the no. of even and odd elements. Initialize them both to zero (even = 0
and odd = 0
) as currently we know 0 odd and even elements.
Now we'll iterate over the array and check if the current element is even or odd.
- If the element is odd then we increase the count of odd numbers (
odd
) by 1. - If the element is even then we increase the count of even numbers (
even
) by 1.
After going through all the numbers in the array, we would have checked and updated all the even and odd counts. At this point, even
holds the count of even numbers, and your odd
holds the count of odd numbers. Now simply print their values : )
Logic
- Input size and elements in array from user. Store it in some variable say
size
andarr
. - Declare and initialize two variables with zero to store even and odd count. Say
even = 0
andodd = 0
. - Iterate through each array element. Run a loop from 0 to
size - 1
. Loop structure should look likefor(i=0; i<size; i++)
. - Inside loop increment
even
count by 1 if current array element is even. Otherwise increment theodd
count.
Solution
#include <iostream>
using namespace std;
int main() {
int i, size, even, odd;
/* Input size of the array */
cout << "Enter size of the array: " << endl;
cin >> size;
/* Create array */
int arr[size];
/* Input array elements */
cout << "Enter " << size << " elements in array: " << endl;
for (i = 0; i < size; i++) {
cin >> arr[i];
}
/* Assuming that there are 0 even and odd elements */
even = 0;
odd = 0;
for (i = 0; i < size; i++) {
/* If the current element of the array is even then increment even count */
if (arr[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
cout << "Total even elements: " << even << endl;
cout << "Total odd elements: " << odd << endl;
return 0;
}
Problem 3: Print all negative elements of an array
Write a C++ program to input elements in array and print all negative elements.
Input
Input size of array: 10
Input array elements: 10, -50, -12, 16, 2, -9, 4, 7, -1, 0
Output
Negative elements: -50, -12, -9, -1
Intuition
This problem is very similar to the last problem. The only difference is that instead of checking for odd and even numbers, you just have to check if the number is negative or not.
Hence, simply run a loop on the array elements and print those elements, whose values are less than 0.
Logic
- Declare and input elements in array.
- Run a loop from 0 to
N-1
(where N is array size). The loop structure should look likefor(i=0; i<N; i++)
. - For each element in array, if current element is negative i.e.
if(array[i] < 0)
then print it.
Solution
#include <iostream>
using namespace std;
int main() {
int N;
/* Input size of the array */
cout << "Enter size of the array: " << endl;
cin >> N;
// Declare the array after taking the input for the size
int arr[N];
/* Input elements in the array */
cout << "Enter elements in array: " << endl;
for (int i = 0; i < N; i++) {
cin >> arr[i];
}
cout << "All negative elements in array are: ";
for (int i = 0; i < N; i++) {
/* If the current array element is negative */
if (arr[i] < 0) {
cout << arr[i] << " ";
}
}
return 0;
}
Problem 4: Copy elements from an array to another array
Write a C++ program to input elements in array and copy all elements of first array into second array.
Input
Input size of array: 10
Input array1 elements: 10 1 95 30 45 12 60 89 40 -4
Output
Array1: 10 1 95 30 45 12 60 89 40 -4
Array2: 10 1 95 30 45 12 60 89 40 -4
Intuition
We start by knowing how many elements you want in your array (size
), and we input the actual elements into an array called source
. Now I want to create a copy of this array named dest
. So I'll create another array of same size
named dest
. Now how can I copy my elements from source
to dest
? I'll use loops again!
I'll iterate over source
and for each element in the source
array, I'll assign its value to the corresponding position in the dest
array something like dest[i] = source[i]
, meaning "copy the current element of source
to the same position in dest
."
At the end, simply print both the arrays : )
Logic
- Input size and elements in array, store it in some variable say
size
andsource
. - Declare another array
dest
to store copy ofsource
. - Now, to copy all elements from
source
todest
array, you just need to iterate through each element ofsource
.Run a loop from 0 tosize
. The loop structure should look likefor(i=0; i<size; i++)
. - Inside loop assign current array element of
source
todest
i.e.dest[i] = source[i]
.
Solution
#include <iostream>
using namespace std;
#define MAX_SIZE 100
int main()
{
int i, size;
/* Input size of the array */
cout << "Enter the size of the array: " << endl;
cin >> size;
/* Create source and dest arrays */
int source[size], dest[size];
/* Input array elements */
cout << "Enter elements of source array: " << endl;
for (i = 0; i < size; i++)
{
cin >> source[i];
}
/*
* Copy all elements from source array to dest array
*/
for (i = 0; i < size; i++)
{
dest[i] = source[i];
}
/* Print all elements of source array */
cout << "Elements of source array are: ";
for (i = 0; i < size; i++)
{
cout << source[i] << " ";
}
/* Print all elements of dest array */
cout << endl << "Elements of dest array are: ";
for (i = 0; i < size; i++)
{
cout << dest[i] << " ";
}
return 0;
}
Problem 5: Print array elements in reverse order
Write a C++ program to input elements in array and print the elements in reverse order.
Input
Input size of array: 5
Input array elements: 10, 5, 16, 35, 500
Output
Array elements after reverse: 500, 35, 16, 5, 10
Intuition
Remember printing numbers in reverse while studying loops? We simply reversed our loop iteration from 1 to N
to N to 1
and voila! The numbers got printed in reverse. Still confused? No worries! Let's see how we use that concept in this very example.
Till now we iterated from 0th index of array and went up till size-1
of arrays. Now we'll start fromsize-1
and go till the 0th index to print the array elements in reverse order. Something like:
for (int i = size - 1; i >= 0; i--)
and keep printing the arr[i]
value.
Logic
- Input size and elements in array from user. Store it in some variable say
size
andarr
. - Run a loop from
size - 1
to 0 in decremented style. The loop structure should look likefor(i=size-1; i>=0; i--)
. - Inside loop print current array element i.e.
arr[i]
.
Solution
#include <iostream>
using namespace std;
int main()
{
int size, i;
/* Input size of array */
cout << "Enter size of the array: " << endl;
cin >> size;
/* Create array */
int arr[size];
/* Input array elements */
cout << "Enter elements in array: " << endl;
for (i = 0; i < size; i++)
{
cin >> arr[i];
}
/*
* Print array in reversed order
*/
cout << "Array in reverse order: ";
for (i = size - 1; i >= 0; i--)
{
cout << arr[i] << " ";
}
return 0;
}
It was fun, wasn't it!? Let's solve some more in the next module to get some more practice : )