
Apologies for the shouting but this is important.
When answering a question please:
 Read the question carefully
 Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
 If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
 If the question is inappropriate then click the 'vote to remove message' button
Insults, slapdowns and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Cofounder
Microsoft C++ MVP





For those new to message boards please try to follow a few simple rules when posting your question. Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
 Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
 Keep the subject line brief, but descriptive. eg "File Serialization problem"
 Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
 Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
 Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
 If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode "<" (and other HTML) characters when pasting" checkbox before pasting anything inside the PRE block, and make sure "Use HTML in this post" check box is checked.
 Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
 Please do not post links to your question into an unrelated forum such as the lounge. It will be deleted. Likewise, do not post the same question in more than one forum.
 Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
 If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
 No advertising or soliciting.
 We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Cofounder
Microsoft C++ MVP





NOAA's National Centers for Environmental Information collects global climate data and aggregates this data to provide information on climate trends and variability. One product they offer is a monthly regional analysis. The following table gives "anomaly" data by continent for January 2017. "Anomaly" means the value is the temperature difference from the average temperature from years 1910–2000.
Continent Anomaly (C)
North America 3.18
South America 1.36
Europe 0.12
Africa 0.53
Asia 1.92
Oceania 0.98
Assignment task:
Your task is to develop an algorithm that would sort data such as these from least to greatest. Specifically, given an unsorted set of N decimal values, your algorithm should sort them to give an answer of the sorted data. For this set of N = 6, your algorithm should produce:
0.12
0.53
0.98
1.36
1.92
3.18
Execute your algorithm for a different set of data, such as a subset of the given data, data you make up, or another month's climate data, such as February 2017: https://www.ncdc.noaa.gov/sotc/globalregions/201702
Does your algorithm work for any N? Have you thought of corner cases it might need to handle, such as N = 0 or N = 1?





Do you know the difference between "help" and "I did nothing; someone do it for me"?
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





Member 13567142 wrote: Your task ... should you decide to accept.





Your homework is set to test what you know, not how good you are at begging strangers on the internet to do your work for you.
Try it yourself, and you'll probably find it's not as hard as you think. After all, it will be based on the topics you have recently covered in your course.
If you genuinely don't know where to start, then talk to your teacher.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
 Homer





How can I find the number of permutation of different types of objects?Can anyone suggest the algorithm for me?thank you!
example: There is 5 walls. I have 5 cans of Yellow paint, 3 cans of Red paint and 2 cans of Blue paint.How many possible painting solutions do I have?







Hi everybody, i'm developing a Modified Compress Sparse Row Matrix Class (in C++, but i think this is not important for the algorithm) you can read a short explanation of the method here I wrote the constructor as follow :
template <typename T>
constexpr MCSRmatrix<T>::MCSRmatrix( std::initializer_list<std::initializer_list<T>> rows)
{
this>dim = rows.size();
auto _rows = *(rows.begin());
aa_.resize(dim+1);
ja_.resize(dim+1);
if(dim != _rows.size())
{
throw InvalidSizeException("Error in costructor! MCSR format require square matrix!");
}
itype w = 0 ;
ja_.at(w) = dim+2 ;
for(auto ii = rows.begin(), i=1; ii != rows.end() ; ++ii, i++)
{
for(auto ij = ii>begin(), j=1, elemCount = 0 ; ij != ii>end() ; ++ij, j++ )
{
if(i==j)
aa_[i1] = *ij ;
else if( i != j && *ij != 0 )
{
ja_.push_back(j);
aa_.push_back(*ij);
elemCount++ ;
}
ja_[i] = ja_[i1] + elemCount;
}
}
}
and it works well ! I also wrote a findIndex(i,j) method that return the value at i,j of the whole matrix and it works fine, so i'm able to print out the whole matrix using the operator overloading () who return 0 if in this position the element is zero otherwise the right value
template <typename T>
std::size_t constexpr MCSRmatrix<T>::findIndex(const itype row , const itype col) const noexcept
{
assert( row > 0 && row <= dim && col > 0 && col <= dim );
if(row == col)
{
return row1;
}
int i = 1;
for(int i = ja_.at(row1)1 ; i < ja_.at(row)1 ; i++ )
{
if( ja_.at(i) == col )
{
return i ;
}
}
return 1;
}
the operator is defined as :
template <typename T>
const T MCSRmatrix<T>::operator()(const itype r , const itype c) const noexcept
{
auto i = findIndex(r,c);
if( i != 1 && i < aa_.size() )
{
return aa_.at(i) ;
}
else
{
return 0.0 ;
}
}
Now I need to write an alghoritm that give 2 index of position is able to insert in the 2 vector the element in the right position could you help me about ?
I wrote this one but dosn't works fine !
template<typename T>
auto constexpr MCSRmatrix<T>::insertAt(itype row ,itype col , T elem) noexcept
{
if(elem == 0)
{
std::cerr << "zero element to insert ! Exit 1" << std::endl;
}
int index = findIndex(row,col);
if( index >= dim+1  row == col)
{
aa_.at(index) = elem ;
}
else if(index == 1)
{
for(auto i=row; i< dim+1 ; i++)
{
ja_.at(i) += 1 ;
}
if(ja_.at(row) >= aa_.size() )
{
aa_.push_back(elem);
ja_.push_back(col);
}
else if ( ja_.at(row) < aa_.size() )
{
ja_.insert(ja_.begin() + dim + row , col );
aa_.insert(aa_.begin() + dim + row , elem);
}
}
}
I hope you give me a way to write down the correct way to inert element ! thanks in advance
P.S. itype is a typedef of std::size_t !





I am developing a project that is meant to improve feature smoothness in heightmaps that represent terrain from multiple maps at different resolutions from satellite imagery.
I know how to use the data in the maps to detect the shorelines. What I want to do however is turn shorelines into edges (lines) that do not intersect at all so that I can smooth them from the blocky look they have outside the USA where satellite resolution is not as good. Most of the time this is fairly straightforward but the lack of good resolution on some of the height maps may result in islands touching the coastline. I want to make sure that the coast on the island follows the outline of the island while the main coastline stays the main coastline. Basically I believe this would boil down to making sure they don't intersect. This area of programming is completely new to me. I want to understand the best algorithm for 'untangling' intersections when more than one coast line happens to touch.





A question arose here at work where they would like to find datagaps in timeline datasets. This can be from database or from images. (eg each 6 hours there's an image).
Basically they want a "pseudocode" function that would work across technologies.
in: at least an array of datetime stamps
out: a list of gaps in the list.
This is what I have so far (it's C type language, but no language in particular):
function FindGaps(DateTime dts_start, DateTime dts_end, int amount, string interval, Array list2check){
Array listreference<DateTime, boolean>;
for(DateTime dts = dts_start; dts < dts_end; dts += amount (interval)){
listreference.Add(dts, false);
}
for(int i = 0; i < list2check.length; i++){
if(KeyExists(listrefence, list2check[i])){
listrefence[list2check[i]]) = true;
}
}
return listrefence;
}
and some of my own thoughts:
problems/remarks:
* You cannot uniformally convert months, years, ... eg to milliseconds (january has different amount of milliseconds than february, ...)
* not all datasets have uniform progression (timegap difference between points)
* datasets passed should not be too large
* the key check should be on similar interval (minutes = minutes eg. 00:00 = 00:00, but 00:00 != 00:00:01)
* adding the fill value can also mark the gap as true / false / invalid eg.
* should you "remove" values that are in between gaps? eg. there are 100 values in a row = false. Should we only keep first and last value?
* This will not be very fast: can we loop from start > end with amount (interval) and check if DateTime is in list2check? (how will we return?)
* better would be if there are points in between 2 points of the reference list, but how to do that practically? (could potentially fix point 2 to some extent)
Does anyone have experience in this? any tips or pointers?
(PS: I have a nice query that can do this in a (postgresql) database, but this does not fit file based datasets)
thanks!





I'm not sure I completely grok what you're trying to do, but what's wrong with comparing the difference between consecutive timestamps with your "required" interval?
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





Put all your "samples" in a queue.
Pop the first sample; that is your starting date/time.
"Predict" the next sample by adding the samplying rate.
"Peek" into the queue to see if your prediction was right: if not, add prediction to "missing", else pop the predicted value from the queue.
Repeat until queue exhausted.
(Memory streams can also be employed; keeping "positioning" in mind).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





In its "Prefix sum" section, Codility proposes the following problem. I don't see how to solve it within the constraints, that are O(N+M), and O(N) for space complexity. I know I should use the prefix sum technique, but I would need more hints. I'd like to not be given the solutions, but hints that would help me guide my thought process.

A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a nonempty string S = S[0]S[1]...S[N1] consisting of N characters. There are M queries, which are given in nonempty arrays P and Q, each consisting of M integers. The Kth query (0 ≤ K < M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4
P[1] = 5 Q[1] = 5
P[2] = 0 Q[2] = 6
The answers to these M = 3 queries are as follows:
The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide A whose impact factor is 1, so the answer is 1.
Write a function:
def solution(S, P, Q)
that, given a nonempty zeroindexed string S consisting of N characters and two nonempty zeroindexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
The sequence should be returned as:
a Results structure (in C), or
a vector of integers (in C++), or
a Results record (in Pascal), or
an array of integers (in any other programming language).
For example, given the string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4
P[1] = 5 Q[1] = 5
P[2] = 0 Q[2] = 6
the function should return the values [2, 4, 1], as explained above.
Assume that:
N is an integer within the range [1..100,000];
M is an integer within the range [1..50,000];
each element of arrays P, Q is an integer within the range [0..N − 1];
P[K] ≤ Q[K], where 0 ≤ K < M;
string S consists only of uppercase English letters A, C, G, T.
Complexity:
expected worstcase time complexity is O(N+M);
expected worstcase space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
modified 6Nov17 5:26am.





S is a string; or an array of characters; characters which "convert" to impact numbers.
P and Q are (3) "substring ranges" into S.
The subset / substring of S will contain one or more "MIN" impact numbers.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





hi,
im pretty rusty with algorithms and i was posed this question. how do I find the most amount of pages i can buy from a list of books within a certain budget. I got as far as sorting the list based on cost, then by no. pages. then i was thinking of creating a map with the cost as key and a list of books at each price. then sorting each keys list based on no of pages. I think im headed in the right direction but iam not quite sure how to complete the exercise? can someone help me of point me in the direction of the correct algorithms i should be using? cheers!





This is homework, and we don't do that for you  it's an exercise to get you thinking about problems, and working out how to solve them. If we give you the "correct algorithm" then that defeats the point of giving you the homework!
So instead, sit down with a pencil and paper, and work out how you would do it for yourself manually.
Hint: would knowing the price per page help you come up with a solution?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!





its not homework. its something im trying to implement in an android app im building! its an algorithm for an in built calculator to check how many pages i can buy from a list of comics from the marvel api.





You at first have to think about what is important for you :
 a special order
 lowest rest money
 maximum count of books
Dependent on this selection you would build different List's.
To be able to decide which List could be the best you must build different SubLists  example :
 you have 100 € and you could buy Book 1 (40 €), Book 2 (35 €), Book 3 (45 €), Book 4 (20 €)
 now one List could be Book 1,2,4  another one could be Book 2,3,4  or Book 1,3 ...
Now ... which List contains the best results for you ?





its based on most pages in a book that i can buy for a certain budget. think its the 0/1 knapsack problem.





Hi, I have 4 arrays 1, 2, .. N_{i} (i=14). N_{i} are fixed and very big. Every time I need to sample without replacement from each of them. The sample sizes (say M) are same for the 4 samples and are fixed and big as well (so N_{i} is bigger than M). I then calculate a single number (say X) using these 4 samples, which takes long time (normally 58 hrs). I need to find a 4sample set which minimizes X. What is the best search strategy? Thanks in advance.
modified 5Nov17 6:01am.





Hi folks, I've tried Googling for an answer to this but keep getting directed down network load balancing which, although related, is not my scenario.
My scenario: I have 3 products (x,y and z) which take 2000, 4000 and 5000 labour hours to assemble respectively. If I want to produce say 50 of product x, 40 of product y and 60 of product z in a year is there an algorithm for distributing the production (the loading) so that the labour hours are spread as evenly as possible throughout the year? I'm quite happy to be offered some additional reading / guidance or a general solution (teach a man to fish etc.).
That's always assuming there is an answer of course .
Andy B
modified 1Nov17 11:21am.





I am not sure whether this is what you want, but I really think you need to learn some cost/lost functions to decide how to manage the resources to minimize the cost and maximize the outputs. If I had paid any more attention to the class, I would have learnt how the maximization or cost reduction algorithms work, but... Cost function  Wikipedia.
Following resources will help you in understanding the overall algorithm, then write your own,
Cost Function  Coursera.org
Mathematical optimization  Wikipedia
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out  Eminem
~! Firewall !~





Thanks Afzaal, the cost function is certainly what I'd like to end up with but when I look into the articles it quickly gets to a level of mathematical notation that I'm not comfortable with, despite being an engineering graduate (long time ago though).
I think in this case I'm going to have to approximate something by working out each products weekly average, then phase shifting them by a number of days each and see if I can derive a function from this somehow. It won't be optimal but then realworld production usually isn't .
The links reference some fascinating material though, it's certainly an area I'd like to explore in more mathematical detail at some point.
Andy B





Or on the other hand you can consider looking for a library that lets you do this, I do not know of any but a quick Google search will yield a few results easily.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out  Eminem
~! Firewall !~






Thanks Gerry but in this instance I'll be programming in a nonMicrosoft environment, so making API calls will not be possible, or at least not easily. I have used the Excel solver function in the past for optimization exercises and it is very effective.





Don't get me wrong. I believe Codility proposes interesting problems. This tool might even be a good way of testing candidates' algorithmic skills (not programming skills). My frustration is elsewhere: it's hard, even when it pretends otherwise.
Problems can be one of three categories: "painless", "respectable", and "ambitious".
My experience however is that few of the painless ones actually are painless. Most of them will require a respectable amount of time to solve. A lot of the problems conceal some trick that you need to somehow discover, and you cannot get any hints. Solutions on the internet will instantaneously spoil the whole exercise instead of giving you hints progressively.
Now, even though you got the algorithm right. You are far from being done, as all you have so far isn't worth a penny. The implementation might actually take you more time than finding the algorithm itself. Because, indexes. You misplaced one incrementation, you forgot one, or whatever. The smallest mistake will quickly drop your score down to 0%, you failed, goodbye. You had the right idea though, it's a shame.
And the worst part is: Google won't find anybody that had or is having a similar experience. I'm alone, and it doesn't feel good.





One wrong "increment" can destroy a $$$ industrial process; or kill a patient.
Better on paper, than in the real world.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





.. or using the wrong units can prang some very expensive hardware a long long way from home.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012





Of course, but that's why you have more than a couple of hours to think about things, and you have discussions with your peers, and peer reviewing, ...
But that wasn't exactly my point.





Background:
I developed a 2d simulation that allows for the training of surgical technologists in the setup of surgical trays. The student is able to pick a tool from a toolbox and drag it onto the surface of the tray and orient it the way it needs to be for the surgery. The grading used a nearest neighbor algorithm using data from a tray set up in the graphical editor by an instructor or subject matter expert. I have available the coordinates of each image as well as the height and width and the orientation of the image. It works okay for that because we had a set of rules guiding the distance the tool can be away from a right answer and still be right.
Problem:
Now, I have to do a similar application for another purpose. The catch is that the grading is more "fuzzy". They don't want grading by the exact location of the user's answer as compared to the right answer. They just want the objects graded based on the approximate location of the object with regard to the other objects on the tray and they should be in the correct order. I have tried to figure this out but have come up short. I can always find that case that would throw the whole thing off. I was wondering if anyone here has any ideas?
Thanks in advance,
Joe





You said: "based on order".
There needs to be an "ideal order". Once that's established, you can determine what consitutes a "variance" from this "ideal"; how it's measured; etc.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal





The definition of a "right" answer seems to be reasonably simple, are they in the right order and do the come within a minimum distance of another instrument.
Never underestimate the power of human stupidity
RAH





In the following problem, which algorithm should be considered?
Having a finite set of variable height brikcs, how can I find the subset that can reach at least a given height, minimizing the total height of the chosen bricks?
In other words, which bricks should I use to reach a given height minimizing cost of material?
Thanks






Yes, I thought about that algorithm for my purpose.
But the two targets are different: in the knapsack problem you must maximize a value minimizing a cost; the value and the cost are two different quantities.
In this case the value and the cost are very related and you have to minimize the value constrained it is more than a given quantity.
Maybe I can apply the same algorithm, but I cannot see how to reduce my problem to the knapsack's one.





It may be overkill but you can easily write it as an integer linear program:
minimize h.x
st.
h.x >= minheight
for all i: x[i] is boolean
Where h are the heights of the brights, x are boolean decision variables deciding for each brick whether to take it or not, minheight is the minimum height that must be reached, and . is the dot product between two vectors.
With just a little coding effort, you can make solvers like GLPK or Gurobi solve that.
Of course it can be solved with DP, but it will be at least an O(height*#bricks) time algorithm where height is the final height, and realistically you'd have to go a bit higher up to some guessed upper bound.





correct
thanks for the pointer to GLPK and Gurobi





Hello !
Can you mathmaticiens, help me ! I'm stuck for weaks to understand this;(least squares conformal mapping)
In the file uvedit_parametrizer.c (in blender or any src code) this part of code :
/* angle based lscm formulation */
ratio = (sina3 == 0.0f) ? 1.0f : sina2 / sina3;
cosine = cosf(a1) * ratio;
sine = sina1 * ratio;
EIG_linear_solver_matrix_add(context, row, 2 * v1>u.id, cosine  1.0f);
EIG_linear_solver_matrix_add(context, row, 2 * v1>u.id + 1, sine);
EIG_linear_solver_matrix_add(context, row, 2 * v2>u.id, cosine);
EIG_linear_solver_matrix_add(context, row, 2 * v2>u.id + 1, sine);
EIG_linear_solver_matrix_add(context, row, 2 * v3>u.id, 1.0);
row++;
EIG_linear_solver_matrix_add(context, row, 2 * v1>u.id, sine);
EIG_linear_solver_matrix_add(context, row, 2 * v1>u.id + 1, cosine  1.0f);
EIG_linear_solver_matrix_add(context, row, 2 * v2>u.id, sine);
EIG_linear_solver_matrix_add(context, row, 2 * v2>u.id + 1, cosine);
EIG_linear_solver_matrix_add(context, row, 2 * v3>u.id + 1, 1.0);
row++;
(sparsematrix) prepares a matrix.
what kind of eqaution is getting solved, Thanks!





Find optimum rectangular size of the box to pack all the small rectangular boxes





Hi Andrew and thank you very much for your library !
I have a little question for your concerning the BlobCounter method, what is its principle ?
Thank for advance
Vincent





You should post your question as a comment to the article it refers to. It's unlikely the author of the article will come here and answer, as there will not be any notification of your question.
selfish adj. Defines someone who does not think of me.





I would like to share some interesting results of playing with the NQueens problem solver. The following plots represent distribution of the solutions number depending on the arrangement of a subset of queens. This distribution is built by iterating the possible permutations of such a subset and counting the number of all solutions containing the current permutation (i.e. by solving NQueens Completion Problem for each permutation).
In this particular case, subset consists of three queens that occupy the first three adjacent columns and only the permutations without overlaps (no one attacks each other) are enumerated. The subset length affects the resolution of the plot but not the general nature of the distribution.
plots_img
plotly_link





If you want to offer this sort of information then you should write a proper article. The forums are more for technical questions.





OK! I just thought that amount of information I have at the moment is not enough for a new article





You can always post it as a Tip if there is not enough for a full article. But either way, it will be more accessible there than in the forums.





I am having difficulty figuring out how to go about creating a dialog box which, when the user clicks the OK button, if that user hasn't answered all the questions in the dialog, then an error message should appear and reshow the dialog (either with or without the selections made previously).
My original solution was something similar to this:
show dialog box
while (!allAnswered) {
if (OK clicked) {
if all questions have answers then allAnswered=true
else
show error message
show dialog box again
end if
end if
loop
Each of the questions contain a dropdown box with the first choice being empty. So, if the dropdown has the empty string for the result, then I know that it hasn't been answered.
However, I wasn't sure if I was performing the if and while in the correct order because it seemed that it wasn't. Could someone please confirm whether I have the proper algorithm or what I need to do to fix it.
Chris





I suggest doing the checks within the dialog box so you won't need a loop at all. If it's WinForms then it could look like this:
In the "calling" Form:
private void ShowQuestionsDialog()
{
using (QuestionsForm form = new QuestionsForm())
{
if (form.ShowDialog() == DialogResult.OK)
{
}
else
{
}
}
}
In the "Questions Dialog":
private void OkButton_Click(object sender, EventArgs e)
{
if ()
{
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show("Please answer all questions.");
}
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson




