CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 28 of 28
  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why is this code crashing?

    So take that data you posted -- what should the output be? Even better, show us with a smaller sample.

    Regards,

    Paul McKenzie

  2. #17
    Join Date
    Jul 2013
    Posts
    161

    Re: why is this code crashing?

    and you are right Paul! i need to use the STL more! but is not my fault..am in third year computer engineering...should be taking my degree in a couple of months...we were only taught how to program in C...yes thats it... soon we will do software engineering in java...the rest is all physics and maths, chemistry and statistics and bla bla bla and bla and bla...soo i need some time..bare with me

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: why is this code crashing?

    go at the solution in another way..
    Not withstanding the points made by Paul re the program design, you can access a vector in a loop which includes a push_back - just not via an iterator.
    This code is valid
    Code:
    for (int e = 0; e < cont3.size(); ++e)
    {
         if (*chk1 == cont3[e])
         {
                //process
         }
         else 
               vll = *chk1;
    
         cont3.push_back(vll);
    }
    As it uses element access rather than an iterator and obtains the size of the vector each time round the loop, this won't cause a crash. If you don't want to process the newly added vector elements, just obtain the vector size before the for loop. As you are using push_back which makes the vector larger and are not removing any elements, the vector size can't become any smaller than the initial size.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #19
    Join Date
    Jul 2013
    Posts
    161

    Re: why is this code crashing?

    given this
    4,34,32,34,56,
    76,4,98,3,4,
    3,5,3,4,5,
    6,7,55,67,65,
    67,19,57,12,45,
    37,59,67,57,4,
    38,33,45,67,87,
    98,31,45,65,23,
    12,19,32,35,76,
    87,45,82,98,78};
    the out put should be 4,34,76,32,3,5,6,7,67,19,37,59,38,33,98,31,12,55,87,45

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why is this code crashing?

    For example, here is a breakdown of cont1 and cont2, according to your description:
    Code:
    cont1 = {4,34} {76,4} {3,5} {6,7} {67,19} {37,59} {38,33} {98,31} {12,19} {87,45}
    cont2 = {32} {98} {3} {55} {57} {67} {45} {45} {32} {82}
    Now, given this data, what is cont3 going to have at the end of this? Also, what are columns 4 and 5 in your data used for?

    Regards,

    Paul McKenzie

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why is this code crashing?

    Just for the heck of it, I coded this up. I won't go into how it works, since this is supposed to be homework.

    However, I posted it to illustrate what can be accomplished using the various container classes.
    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <set>
    #include <algorithm>
    #include <iterator>
    
    const int arr[50] = { 4, 34, 32, 34, 56, 76, 4, 98, 3, 4, 3, 5, 3, 4, 5, 6, 7, 55, 67, 65,
                          67, 19, 57, 12, 45,37, 59, 67, 57, 4,38, 33, 45, 67, 87,98, 31, 45, 65, 23,
                          12, 19, 32, 35, 76,87, 45, 82, 98, 78, };
    
    std::vector <int> pairData;
    std::vector <int> replaceValues;
    
    void load_values();
    std::vector<int> eliminate_repeat(const std::vector<int>& pairdata, std::vector<int>& replacement);
    
    using namespace std;
    
    int main()
    {
        load_values();
        std::vector<int> cont3 = eliminate_repeat(pairData, replaceValues);
        copy(cont3.begin(), cont3.end(), ostream_iterator<int>(cout, " "));
        return 0;
    }
    
    void load_values()
    {
        for (int x = 0, y = 1, z = 2; x <= 45, y <= 46, z <= 47; x += 5, y += 5, z += 5)
        {
            pairData.push_back(arr[x]);
            pairData.push_back(arr[y]);
            replaceValues.push_back(arr[z]);
        }
    }
    
    struct DataGatherer
    {
        std::set<int>* pIntSet;
        std::vector<int>* pReplacer;
        DataGatherer(std::set<int>* p, std::vector<int>* replacer) : pIntSet(p), pReplacer(replacer) {}
        int operator()(int n)
        {
            // assume that value is ok
            int valToUse = n;
    
            // now test if value is duplicate
            if (pIntSet->find(n) != pIntSet->end())
            {
                // the value is duplicate, so fix up our replacement vector
                pReplacer->erase(std::remove_if(pReplacer->begin(), pReplacer->end(),
                    [&](int n) { return pIntSet->find(n) != pIntSet->end(); }), pReplacer->end());
                // take new value from front of replacement values
                valToUse = pReplacer->front();
            }
            // insert our value in the set 
            pIntSet->insert(valToUse);
            // add this value to our resulting set of values
            return valToUse;
        }
    };
    
    std::vector<int> eliminate_repeat(const std::vector<int>& pairdata, std::vector<int>& replacement)
    {
        std::vector<int> retData;
        std::set<int> IntSet;
        DataGatherer dg(&IntSet, &replacement);
        transform(pairData.begin(), pairData.end(), back_inserter(retData), dg);
        return retData;
    }
    Note the miminum number of loops. The only loop is the reading in of the original data. Everything else is an algorithm function.

    The code uses C++11 syntax, so you need a C++ 11 compiler.

    Now, to a novice, it looks daunting. But to an experienced C++ programmer, they know exactly what the algorithm functions do, and the idea of a function object. They know what a set is, they know what transform does, if they're versed in C++ 11, they know what a lambda expression is, etc.

    So in reality, this code above is more clearly understood to an experienced programmer than your code, even though your code uses "easier looking" syntax.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 13th, 2014 at 02:12 PM.

  7. #22
    Join Date
    Jul 2013
    Posts
    161

    Re: why is this code crashing?

    thanks paul! anyway is not homework...is me wanting the machine to do some daunting repetitive task for me...i got this part time job to put some numbers in order...and am trying to find a way to make the machine do the job for me in seconds while i get payed for 3 hours :P so don't worry about the home work issue...we're long passed those times... thanks a lot though

  8. #23
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: why is this code crashing?

    Quote Originally Posted by TheLionKing View Post
    ...i got this part time job to put some numbers in order...and am trying to find a way to make the machine do the job for me in seconds while i get payed for 3 hours
    Where can one get such a job? What does it pay? How do you get the data - in the mail? How to you provide the results?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #24
    Join Date
    Jul 2013
    Posts
    161

    Re: why is this code crashing?

    vladimirF that was by the way..we're here to talk messed up code.
    Paul mckenzie..I find your code too heavy though..i didn't even know you could call a function in a struct like that..like how you did up there..anyway yes..am still learning
    but just going through a pdf i found on the next on STL gave me the idea that i could do this thing simple with a list! since with a list i can eliminate left and write and everything will be ok...and i think everything will me more simple! thank you very much though..anytime am having such a problem before coming here i will just look in the STL and see if there is a library which handles the data the way i want it..thanks to every one..

  10. #25
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why is this code crashing?

    Quote Originally Posted by TheLionKing View Post
    vladimirF that was by the way..we're here to talk messed up code.
    Paul mckenzie..I find your code too heavy though..i didn't even know you could call a function in a struct like that..like how you did up there..anyway yes..am still learning
    One way to learn is to take the code I posted, compile it, and step through it with a debugger. You will see it gives you the correct results, all without loops. It almost uses no logic, except for the operator() in the struct.

    Just that alone should get you interested on how such a small piece of code accomplishes what you were after. Since it uses just a small bit of logic (the operator()), you have a very good chance of understanding what is being done. The functions, classes, and techniques are documented all over the web and in any good C++ book.

    So, it isn't really heavy -- what I did was thought about the problem on a conceptual level, not concentrating on low-level for-loops as most beginner programmers would have done, and use the C++ library containers and algorithms to implement those concepts.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 13th, 2014 at 09:45 PM.

  11. #26
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: why is this code crashing?

    You might find this code a little easier to understand
    Code:
    #include <set>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    typedef set<int> si;
    typedef vector<int> vi;
    
    const int arr[][5] = {	{ 4, 34, 32, 34, 56 },
    				{ 76, 4, 98, 3, 4 },
    				{ 3, 5, 3, 4, 5 },
    				{ 6, 7, 55, 67, 65 },
    				{ 67, 19, 57, 12, 45 },
    				{ 37, 59, 67, 57, 4 },
    				{ 38, 33, 45, 67, 87 },
    				{ 98, 31, 45, 65, 23 },
    				{ 12, 19, 32, 35, 76 },
    				{ 87, 45, 82, 98, 78 } };
    
    int main()
    {
    si unique;
    vi final;
    const int norow = sizeof(arr) / sizeof(arr[0]);
    int third = 0;
    
    	for (int r = 0; r < norow; ++r)
    		for (int c = 0; c < 2; ++c) {
    			 const int &no = arr[r][c];
    
    			if (unique.find(no) == unique.end()) {
    				unique.insert(no);
    				final.push_back(no);
    			} else {
    				for (; (third < norow) && (unique.find(arr[third][2]) != unique.end()); ++third);
    
    				if (third < norow) {
    					unique.insert(arr[third][2]);
    					final.push_back(arr[third++][2]);
    				} else {
    					cout << "Third column exhausted!" << endl;
    					return 1;
    				}
    			}
    		}
    
    	for (vi::iterator ci = final.begin(); ci != final.end(); ++ci)
    		cout << *ci << " ";
    }
    The output is
    Code:
    4 34 76 32 3 5 6 7 67 19 37 59 38 33 98 31 12 55 87 45
    Note that if you want the output in sorted order than you can dispense with the vector and just output from the set.
    Code:
    for (si::iterator ci = unique.begin(); ci != unique.end(); ++ci)
    		cout << *ci << " ";
    gives an output of
    Code:
    3 4 5 6 7 12 19 31 32 33 34 37 38 45 55 59 67 76 87 98
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #27
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: why is this code crashing?

    Quote Originally Posted by TheLionKing View Post
    vladimirF that was by the way..
    Sure, but I thought it was more of a BS than BTW
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  13. #28
    Join Date
    Jul 2013
    Posts
    161

    Re: why is this code crashing?

    just don't mind

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured