Occurrences in 2D array from 2D Array - c++

Recently I've come into a wall, because I have no idea how to count the occurrences in one 2d array from another 2d array. For example, if I have
int winning_nums[3][6]; // random generated numbers array
and
int array[7][7]; // Sorted array from 1 to 49
I need to get all occurrences of each number in winning_nums to all the elements in array.For example: If I got 1 array of winning_nums that equals 6 size, 1 2 3 4 5 6 needs to be checked if they are occuring in array, like for each row in winning_nums.
I've tried to sort it out, like sort the winning_nums array, and then try to occur it with two additional for loops.
Any better ideas? Anything is welcomed.

Related

Decreasing value in vectors with SFML

I created 5 numbers using vector with SFML, but I want the second one to fall one by one at intervals of 1 second. But, they first three falling as one by one. I don't understand why something like this is happening. Can you help me?
if (second == 1)
{
random.at(2)-=1;
cout << random[2] << endl;
text.setString(to_string(random[2]));
text.setPosition(numbers[2].getPosition().x, numbers[2].getPosition().y);
numbers.push_back(text);
numbers.erase(numbers.begin() + 2);
clock.restart();
}
The program gif
Full code
I'll give you a hand.
Here's what's happening:
You create 5 numbers in the random array. You may not have noticed it, but they are numbered 0 to 4 (SFML is sitting on C++, and then it means that arrays starts at zero here).
Every second, you update the number stocked in the 3rd place of your random array.
Then it goes wrong: instead of updating the corresponding number in the numbers array, you cycle it with push_back and erase.
Understand me here: push_back create a new element at the end of the vector, while erase removes an element from the vector and then "sort out things" so there's not number gap in the index of the vector.
Effectively, you're handling random right, but when you try to update number you cycle through it. Like this:
seconds: 1 2 3 4 5 6
array content: 0 0 0 0 0 0
(vertical) 1 1 1 1 1 1
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
I'm not sure how clear I'm making this, but if you look at the array content, you'll see that by erasing and creating a new value at the end, you're cycling through the positions [2-4] of the array. That's why in your gif not all numbers are updated wrong, only 3 of them.
The obvious solutions would be to stop erasing and pushing back in the numbers array. You can update it the same way you updated the random array. It'll be fine.
Like this:
if (second == 1)
{
random.at(2)-=1;
cout << random[2] << endl;
numbers[2].setString(to_string(random[2]));
clock.restart();
}
Have fun.

How to select the nth position from a string vector?

How does one find, let's say, the 2nd position from a string vector?
Here's a string vector example:
1 2 3 4 Hi
7 8 9 0 Bye
2 2 5 6 World
If I use example.at(2), it gives me the whole row 2 2 5 6 World.
I just want to get 2 from the 1st row instead of getting the whole line of 2 2 5 6 World. How do I do that?
The return value of example.at(2) is the 3rd item in your vector, in this case, a std::string.
To access a specific character in your string, you can use the operator[]. So to select 2 from the first row, you would simply need to do the following:
example.at(0)[2];
So what you actually have is vector of string where string represents another dimension, so you have an table with both rows and columns, similar to an 2D array, in other to access a single cell you need 2 indexes, one index for position in vector and another for position in string.
So in your case it would be example[0][0] to get first char of a string in first row, and to get one you are looking for you would need to write example.at(0)[2];
This should work:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> strings;
strings.push_back("1234Hi");
strings.push_back("7890Bye");
std::cout << strings.at(0)[1] << std::endl; // prints 2
std::cout << strings.at(1)[1] << std::endl; // prints 8
}
It's sort of like a two-dimensional array: each string you push to the vector is analogous to the first dimension, and then each character of the string is analogous to the second dimension.
But as mentioned above, there may be better ways to do this, depending on what exactly you're trying to do.
Other answers show you how to access individual numbers in your strings, but they assume that the numbers are always 1 digit in length. If you ever need to support multi-digit numbers, use std::istringstream() or std::stoi() instead to parse the strings.

How to define 2D vector which rows are known c++ [duplicate]

This question already has answers here:
allocating vectors (or vectors of vectors) dynamically
(9 answers)
Closed 8 years ago.
what is the best way to define 2D vector, which rows are known? So basically, I will have something like this:
2 3 4
7 5 4 12 4
2 1 0 2
I will know how many rows there are (well actuall I will know it only after getting row variable), but each row collumn will vary. So what is the best way to do this? (I am planning on pushing back each row's columns when I will need, if it is possible)
Here is someone who had a similar question http://www.cplusplus.com/forum/beginner/12409/. Just Define a vector of vectors.
It wont let me comment down below, but in your code you have tiesiogiaiJungiasi[A.at(i)]
What type is A and if it is a vector then is that vector filled with integers if not A.at(i) is your problem. That will give you the element that is at that location not the location itself.
If you are trying to push the element index i from row B into the element index i of row A try something like this.
tiesiogiaiJungiasi[A].push_back(tiesiogiaiJungiasi[B][i]);
assuming there is an element in row B at the index location.
Another problem could be that you say that your rows may be of different sizes. Well you should look at your m parameter in the for loop then. If that number is larger than the number of columns (elements) in that row i has the possibility of walking off the array.
If i'm understanding correctly, this seems like what you want is an array of vectors. You can set an array of vectors like vector<string> myArray[3]; and then push values into myArray[0], myArray[1], etc.
If I understood correctly. You want a data structure that will allow you to append to the end of each row. A vector< vector < int > > will do.
vector< vector < int > > rows;
You have to populate this, if you know the max number of rows you could write:
vector< vector < int > > rows(MAX);
You append to the end of a row using rows[i].push_back(...);. Rows are numbered starting at 0 (add 1 to MAX if you want to start at 1).

Sorting an integer array of 100 elements having only 3 elements in it

Suppose I have an array of 100 numbers. The only distinct values in the array are 1, 2 and 3. The values are randomly ordered throughout the array. For instance, the array might be populated as:
int values[100];
for (int i = 0; i < 100; i++)
values[i] = 1 + rand() % 3;
How can I efficiently sort an array like this?
The fastest solution is not to "sort" at all:
Run through the array and count the number of occurrences of 1,2 and 3. These counts should hopefully fit in registers...
Fill the array with the right number of 1s, 2s and 3s, overwriting whatever is there already.
At the end you will have a fully sorted array.
In general, this can be a useful O(n) sorting algorithm when you have a very small range of possible values compared to the size of the array.
Dutch National flag algorithm is the commonly cited algorithm for this and is actually the partition step in one of the variants of quicksort (1 corresponds to less than, 2 to equal to and 3 to greater than). In that variant, you don't need to sort the middle portion.

Position of elements in vector

I have several elements in a vector type that are read from cin and then i perfrom some calculations on the vector and it's order of elements gets changed. The problem is that I need to print the positions of the vector elements after the calculations. I don't know how to explain this well that's why i'll give an example:
10 1 100 1000
and 10 is 1st element, 1 is 2nd, 100 is 3rd etc. After the calculations the vector changes in :
100 10 1 1000
so I should print
3 1 2 4
because 100 is the 3rd element of the input, 10 is the 1st etc. etc.
I tried with an array[1000] (because there aren't numbers larger than 1000 in the input), but it won't work because there can be multiple numbers with the same value, like:
10 10 10 100
and the output can be 1 2 3 4 or 2 3 1 4 or 3 1 2 4 etc. but here i need to output 1 2 3 4 because it's the 'smallest'.
I tried with array f[1001] and f[10] = 1, f[100] = 2, f[1] = 3 - if the numbers from the input are 10 100 1. But in case there are multiple numbers with the same value like 10 10 100, then my idea's not working. Please help me in any possible way.
Sounds like you need to store both the value and the initial position. You should be able to do this with an array of structs:
struct UserInput
{
unsigned int initialPosition;
int userInputValue;
};
int main()
{
userInput theUserInput[100];
// increment a counter, starting at 1, and place it in
// "initialPosition" in the struct as user input is read
}
I'll leave the rest up to you... as it is after all homework :) good luck.
Use an associative array if you know what it is.
Use linear search to determine the index if the number of input is limited.
Consider using log10 (or strlen) to transform the 1, 10, 100, 1000, etc. into 0, 1, 2, 3, etc.
From your description of such example:
10(3) 10(2) 10(1) 100(4)
What we have to output is 1 2 3 4, instead of 3 2 1 4.
So I don't think your requirement is just print the initial position directly. You've to make the position sequences as small as possible.
Following is my solution:
Use a direct-mapping hash table to store all the initial positions for specified element. All the initial positions for the same element is sorted. So if you want output the smallest position sequence, you only need to read the initial positions for this specified element from first to last.
The detailed implementation is left to you, since it's a homework.