4-Arrays 2
4-Arrays 2
● Write a program that take an integer n that represent the size of two
array of char S,T. print the first char in S and the first char in T, then
print the second char in S, and second char in T, and so…
)without using string(
● Write a program that take an array of integer of size N, and print the
summation of the even numbers and the summation of odd numbers.
● Write a program that take an array of integer of size N, and print the
summation of the prime numbers.
Sort Function
● sort(); #include <iostream>
#include <algorithm>
- This function sorts an array in increasing order using namespace std;
- It generally takes two parameters : int main()
{
- First one is the point of the array from int n = 10; // size of the array
where the sorting needs to begin int arr[10]={1, 5, 8, 9, 6, 7, 3, 4, 2,0};
int main()
{
int n = 10; // size of the array
int arr[10] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
return 0;
}
1583467920
Reverse Function
● reverse(); #include <iostream>
- This function reverse the elements an array #include <algorithm>
- It generally takes two parameters : using namespace std;
- First one is the point of the array from where the int main()
reversing needs to begin {
int n = 10; // size of the array
- Second one is the length up to which we want int arr[10]={1, 5, 8, 9, 6, 7, 3, 4, 2,0};
the array to get reversed.
reverse(arr, arr + n);
● Library : algorithm
● Complexity O(N) cout << "Array after reversing :\n";
for (int i = 0; i < n; ++i)
● Try to reverse the array without using the cout << arr[i] << " ";
function return 0;
● Remember that you need an array whose size is equal to the value of the largest integer
in the original array. Which means that you can't use a frequency array if the values in the
original array can be up to 10^9 for example.
● You can use a frequency array to sort an array in O(M) time, where M is the value of the
largest integer in the array.
Frequency Array
Examples :
#include<iostream>
using namespace std;
Output :
int main () { 1:3
int Frequency[100] = {0}; // Initial all the array with 0
2:2
int numbers[8] = {1, 2, 3, 2, 5, 6, 1, 1}; 3:1
// Doing the Frequency operation in the array "numbers" 4:0
for(int i=0;i<8;i++){ 5:1
Frequency[ numbers[i] ] ++;
} 6:1
7:0
// Printing the number and it`s frequency
for(int i=1;i<=10;i++){ 8:0
cout << i << " : " << Frequency[i] << endl; 9:0
}
10 : 0
return 0;
}
Problems
* You are given a string consisting of
lowercase and uppercase Latin letters,
Check if all the characters of the
alphabet of this language appear in
it at least once
● In many cases, you don't need the original array after you build the prefix_sum
array. In these cases, it's better to "transform" your original array into a
prefix_sum array, instead of creating a separate array for the prefix sum.
Prefix Sum
Example :
#include<iostream>
using namespace std;
int main () {
int numbers[8] = {1, 2, 3, 2, 5, 6, 1, 1};
Example :
(Input)
Array = {1, 2, 3, 4, 5, 6, 7, 8}
(Output)
Even = {0, 2, 2, 6, 6, 12, 12, 20}
Odd = {1, 1, 4, 4, 9, 9, 16, 16}
Binary search
● Binary Search: search in a sorted array by repeatedly dividing the
search interval in half.
If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half.
Otherwise narrow it to the upper half.
Solution