How Do I Get a subvector from a vector? - c++

How do i access 1,2,10 from the below vector?
D = [[1, 2, 10], [2, 3, 20], [2, 5, 25]]
int i=B[i][0]-1;
int j <= B[i][1]-1;
Was Solving a Problem, I Had to use them in a for loop to check for the answer. Can Anyone help me out

Are you looking for this?
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<int>> d = { {1, 2, 10}, {2, 3, 20}, {2, 5, 25} };
auto subvector = d[0];
for (auto& value : subvector)
{
std::cout << value << ' ';
}
std::cout << "\n";
// or (poor solution)
for (int i = 0; i < 3; i++)
{
std::cout << subvector[i] << ' ';
}
std::cout << "\n";
// or even
for (int i = 0; i < 3; i++)
{
std::cout << d[0][i] << ' ';
}
std::cout << "\n";
}

Related

How to compare an element of array with all the elements of another array

I have 2 arrays : a [ ] = {1,2,3,4,5,6} and b [ ] = {1,2,6}. How can I compare all elements from array a with all from array b. For example, I compare the first element from a with all elements from b, and if they are not equal, it's displayed and continue to check. So after all I need to get c [ ] = {3,4,5}.
Please help me.
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
if(sf[i].r != temp[j].r)
{
cout<<sf[i].r<<" ";
}
}
}
Where sf[ ] .r = {1,2,2,2,3,5,6,6,7,8,8} and temp[ ].r = { 1,3,5,7} . Output must be {2,2,2,6,6,8,8}.
Just use a std::vector<int> to build up your results, something like:
std::vector<int> set_difference;
for (int elem_a : a)
{
if (std::find(std::begin(b), std::end(b), elem_a) == std::end(b))
{
set_difference.push_back(elem_a);
}
}
int a[] = { 1, 2, 3, 4, 5, 6 };
int b[] = { 1, 3, 6, 2, 5, 9 };
std::vector<int> c;
for (int i = 0; i < sizeof(a); i++)
{
for(int j = 0; j < sizeof(b); j++)
{
if (a[i] == b[j])
std::cout << a[i] << " equals " << b[j] << std::endl;
else
{
std::cout << a[i] << "not equals " << b[j] << std::endl;
c.push_back(a[i]);
}
}
}

how to compute value of intersection of two arrays?

I was able to write a function that prints out the intersection of two arrays. However, I am trying to obtain the value of the members of the intersection.
If our final intersection is {6, 12} I should return 2 as the value because I have 2 members. My return value is 1 and I don't know what I am doing wrong.
int main()
{
int data1[] = { 3, 6, 9, 12};
int data2[] = { 2, 4, 6, 8, 10, 12 };
int result[] = {};
size_t length1 = sizeof(data1)/sizeof(int);
size_t length2 = sizeof(data2)/sizeof(int);
size_t resultMax= 0;
int i =0;
int j =0;
while (i < length1 && j < length2)
{
if (data1[i] < data2[j])
{
i++;
}
else if (data2[j] < data1[i])
{
j++;
}
else if (data1[i] == data2[j])
{
result[i] = data1[i];
cout << "valor : " << result[i] << endl; // output is 6 and 12
i++;
j++;
resultMax = sizeof(result[i])/sizeof(int);
}
}
cout << "Final Size: "<< resultMax; //output is 0
return resultMax;
}
Use std::vector instead of array.
# include<iostream>
# include<vector>
using namespace std;
int main()
{
vector<int> data1 = { 3, 6, 9, 12}, data2 = { 2, 4, 6, 8, 10, 12 }, result;
int i = 0, j = 0, length1 = data1.size(), length2 = data2.size();
while (i < length1 && j < length2)
{
if (data1[i] < data2[j])
i++;
else if (data2[j] < data1[i])
j++;
else if (data1[i] == data2[j])
{
result.push_back(data1[i]);
cout << "valor : " << data1[i] << endl;
i++;
j++;
}
}
cout << "Final Size: "<< result.size();
return 0;
}
Live Code
Use the algorithm library, specifically std::set_intersection.
The following is a slightly adapted example from the above linked cppreference page:
int main()
{
std::vector<int> v1{3, 6, 9, 12};
std::vector<int> v2{2, 4, 6, 8, 10, 12 };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::vector<int> v_intersection;
std::set_intersection(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v_intersection));
std::cout << "Final Size: "<< v_intersection.size();
}

Find largest Max list in an array

I need to find the first Max list of an array and find it's middle. In other words, having for example this array {2, 8, 8, 8, 8, 8, 6, 3, 8, 8, 8} I need to have as a result the index 3, which is the middle of the first max list. I did try but my C++ code is still missing something. Can you please help.
Thanks
The following code is just a sample what I'm working on is an 90 elements array.
#include <iostream>
using namespace std;
int main()
{
int array[] = {2, 8, 8, 8, 8, 8, 6, 8, 0};
int Max = 0;
int StartMax = 0, EndMax = 0;
for (int m = 0 ; m < 9 ; m++){
if(array[m] > Max){
Max = array[m];
StartMax = m;
EndMax = m;
cout << "array[m] > Max " << Max << endl;
}
else if(array[m] < Max){
cout << "array[m] < Max " << Max << endl;
}
else {
int a = array[m] - array[m-1];
cout << "a = " << a << endl;
if (a == 0){
cout << "a = " << a << endl;
EndMax = m;
}
}
}
cout << "Index of Max : " << ((StartMax+EndMax)/2) << endl;
}
The problem
Your code work for this example but it won't work when you have a second "max list" which has more than 2 elements.
Indeed with array[] = {2, 8, 8, 8, 8, 8, 6, 8, 8}; (notice the last 8)
We get the result : middle=4 instead of middle3 because you enter this branch condition when you encounter 8 again :
else {
int a = array[m] - array[m-1];
And you enter the branch if (a==0) and you set EndMax to the end of the array !
StartMax = 1 and Endmax = 8 thus middle = 4
This is not what you want !
Live Code
Solution
I would suggest to use a boolean tracker to manage that instead :
size_t give_middle_max_list(const std::vector<int>& v) {
size_t idx_start_max = 0;
size_t idx_end_max = 0;
int max_val = v[0];
bool should_continue = false;
for(size_t i = 1; i < v.size(); i ++) {
if(v[i] > max_val) {
max_val = v[i];
idx_start_max = i;
idx_end_max = i;
should_continue = true;
}
else {
if (v[i] == max_val && should_continue == true) {
idx_end_max = i; // I am still in the first max list
}
else {
should_continue = false; // I am not in the first max list anymore !
}
}
}
std::cout << idx_start_max << ";" << idx_end_max << std::endl;
return (idx_end_max + idx_start_max) / 2;
}
Live code

How do I store numbers that matches in an array?

I want to add the index of arrayOfNumbers that matched to numToMatch to an array. Not sure how to go about this. I'm new to C++. Any help would be appreciated ty.
int numToMatch[4] = {1,2,3,4};
int arrayOfNumbers[7] = {0, 1, 2, 3, 4, 5, 6};
int IndexThatMatched[4]; // Want to add to this Array.
int main()
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 7; j++)
{
if(numToMatch[i] == arrayOfNumbers[j])
{
cout << "Num " << numToMatch[i] << " ";
cout << "matches " << arrayOfNumbers[j];
cout << endl;
}
}
}
}
Added only two lines.
int numToMatch[4] = {1,2,3,4};
int arrayOfNumbers[7] = {0, 1, 2, 3, 4, 5, 6};
int IndexThatMatched[4]; // Want to add to this Array.
int main()
{
for(int i = 0; i < 4; i++)
{
IndexThatMatched[i] = -1;
for(int j = 0; j < 7; j++)
{
if(numToMatch[i] == arrayOfNumbers[j])
{
cout << "Num " << numToMatch[i] << " ";
cout << "matches " << arrayOfNumbers[j];
cout << endl;
IndexThatMatched[i] = j;
}
}
}
}
If there was no match for numToMatch[k] then IndexThatMatched[k] will be -1.
In your example IndexThatMatched will be: {1, 2, 3, 4}.
That's because numToMatch's elements are positioned in indices 1-4 of arrayOfNumbers.
Moreover, after that run, for any k in the safe range:
numToMatch[k] == arrayOfNumbers[IndexThatMatched[k]]
If you really want to learn C++ ditch the C-style arrays and start practising with containers. If you combine them with the functions in <algorithm> you can write something like this:
std::vector<int> MatchNums{1,2,3,4};
std::vector<int> NumsToMatch{0, 1, 2, 3, 4, 5, 6};
std::vector<int> MatchedNums;
for ( auto Number:NumsToMatch )
{
auto Match = std::find( MatchNums.begin(), MatchNums.end(), Number );
if ( Match != MatchNums.end() )
{
MatchedNums.push_back( *Match );
}
}
for ( auto i:MatchedNums )
{ //Print out the found numbers:
std::cout << i << std::endl;
}
There's a lot more ways of doing this by combining different algorithms and I encourage you to try and think of as many ways as you can and try them all out.

Split array into sub blocks

What I am trying to achieve is this:
I have an image and I need to split it into sub blocks of 16x16 and I am working on the algorithm for this. For testing purposes though, I am using a small matrix:
A = {1, 2, 3, 4}
Now what I want to end up is this: 2 blocks containing:
A[1] = {1 2};
A[2] = {3, 4};
I have tried to use the following:
double matrix[4] = {1, 2, 3, 4};
for(int i = 0; (i < 4); i++)
{
for(unsigned j=i; (j < 2); j +=2)
{
std::cout << j << ' ';
}
std::cout << std::endl;
}
My thought process was to loop through the entire array (4) and then increment by 2 each time to create the 1x2 block. This did not work however.
Where am I going wrong here?
Something like that? (Does both output and assignment)
int LEN = 4;
int INNER = 2;
int OUTER_LEN = LEN/INNER_LEN;
double matrix[LEN] = {1, 2, 3, 4};
double* matrix2[OUTER_LEN];
for(int i = 0; i < OUTER_LEN; i++)
{
matrix2[i] = &matrix[i*INNER_LEN];
for(unsigned j=0; j < INNER_LEN; j++)
{
std::cout << matrix[i*INNER_LEN+j] << ' ';
}
std::cout << std::endl;
}
Just for output you could do something like that:
#include <iostream>
int main(){
const size_t SIZE = 4;
const size_t PART_SIZE = 2;
double matrix[4] = {1, 2, 3, 4};
for(int i = 0; (i < SIZE); i += PART_SIZE)
{
for(size_t j = i; (j < i + PART_SIZE) && j < SIZE; j += 1)
{
std::cout << matrix[j] << ' ';
}
std::cout << std::endl;
}
}
To add another matrix:
#include <iostream>
int main(){
const size_t SIZE = 4;
const size_t PART_SIZE = 2;
size_t partsNumber = SIZE / PART_SIZE; // Beware of SIZE that is not divisible by PART_SIZE - partsNumber will be too small
double matrix[4] = { 1, 2, 3, 4 };
// To do it properly I should make it dynamic array with size of partsNumber instead of the 2 literals
double parts_matrix[2][PART_SIZE];
for (int i = 0; (i < SIZE); i += PART_SIZE) {
for (size_t j = i; (j < i + PART_SIZE) && j < SIZE; j += 1) {
std::cout << matrix[j] << ' ';
parts_matrix[j / partsNumber][j % PART_SIZE] = matrix[j];
}
std::cout << std::endl;
}
std::cout << parts_matrix[0][0] << " " << parts_matrix[0][1] << std::endl << parts_matrix[1][0] << " " << parts_matrix[1][1]; // Check if it works
}
The following is a demo of how to do the splitting for custom block size (rough cut though, corner cases and input verification are ommited) using boost range and the boost::slice functionality (here "output creation" is presented)
#include <iterator>
#include <iostream>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/algorithm/copy.hpp>
using namespace std;
using namespace boost::adaptors;
template<typename T, size_t N>
void split(T (&input)[N], size_t block_size)
{
for (size_t i(0); i <= N-block_size; i += block_size)
{
cout << "{ ";
boost::copy(input | sliced(i, i+block_size),
std::ostream_iterator<int>(std::cout, " "));
cout << "}\n";
}
}
int main()
{
int A[] = {1, 2, 3, 4};
split(A, 2);
}
Demo
Output
{ 1 2 }
{ 3 4 }
What if I don't want to do output
To some the following may look more readable
template<typename T, size_t N>
void split(T (&input)[N], size_t block_size)
{
for (size_t i(0); i <= N-block_size; i += block_size)
{
cout << "{ ";
// do whatever with the i slice (again I'm showing output)
for (auto k : (input | sliced(i, i+block_size))) cout << k << " ";
cout << "}\n";
}
}