working set code

This is a discussion on working set code within the C++ Programming forums, part of the General Programming Boards category; thanks, i tried to edit but whenever i post, it removes editing. anyway i attached a text file with code ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    working set code

    thanks,
    i tried to edit but whenever i post, it removes editing. anyway i attached a text file with code and explanation. hope that should work.

    please let me know if you still have question.
    thanks



    hello everybody,

    I am trying to implement working sets using STL vector... my code prints pages according to window size. I tried to print pages without repetition..but I could not do that....can anybody help me with correct loop which checks whether the page is already in the vector or not and then prints pages without repetition.

    my code is :
    Code:
    #include<stdio.h>
    #include<unistd.h>
    #include<vector>
    #include<iostream>
    using namespace std;
    class workset
            {
            public:
            void workingset(int,int,int*);
            };
    
    // n is the array size, w is the window size and pages is the list of pages.
    void workset::workingset(int n, int w, int pages[])
    {
            int j,count=0,k;
            int count2;
            int i;
            vector <int> v;
    
            printf("\nthe list is= ");//first prints the list
    
            for(k=0;k<n;k++)
                    printf("%d,", pages[k]);
            printf("\n");
            printf("Time \t Size \t Set\n");
    
    
            for (int time=w-1;time<n;time++)
                    {
                            count2=count;
                            for(i=0;i<w;i++)
                            {
                            v.push_back(pages[count2]);
                            count2++;
                            }
                            printf("%d \t %d \t ",time,v.size());
                            for (j=0;j<v.size();j++)
                            printf("%d ",v[j]);
                            printf("\n");
                            v.erase(v.begin(),v.end());
                            count++;
       }
      printf("\n");
    
    };
    int main()
    {
      workset w1,w2;
      int list1[] = {1,2,3,3,2,3,2,1,2,1,2,1,3,1,3,1,2,3,1};
      int list2[] = {1,2,3,3,4,4,5,3,3,2,1,2,1,2,3,4,5,3,2,4,2,4,2,2};
     
      w1.workingset(sizeof(list1)/sizeof(int),3,list1);
      w2.workingset(sizeof(list2)/sizeof(int),4,list2);
      return 0;
    };
    and my output is:

    the list is= 1,2,3,3,2,3,2,1,2,1,2,1,3,1,3,1,2,3,1,
    Time Size Set
    2 3 1 2 3
    3 3 2 3 3
    4 3 3 3 2
    5 3 3 2 3
    6 3 2 3 2
    7 3 3 2 1
    8 3 2 1 2
    9 3 1 2 1
    10 3 2 1 2
    11 3 1 2 1
    12 3 2 1 3
    13 3 1 3 1
    14 3 3 1 3
    15 3 1 3 1
    16 3 3 1 2
    17 3 1 2 3
    18 3 2 3 1


    the list is= 1,2,3,3,4,4,5,3,3,2,1,2,1,2,3,4,5,3,2,4,2,4,2,2,
    Time Size Set
    3 4 1 2 3 3
    4 4 2 3 3 4
    5 4 3 3 4 4
    6 4 3 4 4 5
    7 4 4 4 5 3
    8 4 4 5 3 3
    9 4 5 3 3 2
    10 4 3 3 2 1
    11 4 3 2 1 2
    12 4 2 1 2 1
    13 4 1 2 1 2
    14 4 2 1 2 3
    15 4 1 2 3 4
    16 4 2 3 4 5
    17 4 3 4 5 3
    18 4 4 5 3 2
    19 4 5 3 2 4
    20 4 3 2 4 2
    21 4 2 4 2 4
    22 4 4 2 4 2
    23 4 2 4 2 2


    but for example it should print

    the list is= 1,2,3,3,2,3,2,1,2,1,2,1,3,1,3,1,2,3,1,
    Time Size Set
    2 3 1 2 3
    3 2 2 3
    4 2 3 2
    5 3 3 2
    6 3 2 3
    and so on......
    I would appreciate any help...
    Attached Files Attached Files
    Last edited by chinu; 04-13-2008 at 11:33 AM. Reason: Editing problem

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Moved to C++ programming forum.

    Kindly edit your post to indent your code and post it within code bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I fail to see the conceptual difference between the example output and your actual output. Perhaps you can give us a high-level explanation of what the code should do and what the output means.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I agree, I have no idea what your code is supposed to do.

    But: if you don't want to add a number to a vector if it's already there, you have to loop through the vector to try and find it, and only add it if you don't.

    But but: there's already an STL container that does this, unless the point is to write your own.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Thanks,

    i tried to edit but whenever i post, it removes editing. anyway i attached a text file with code and explanation. hope that should work.

    please find that attachment in original post and let me know if you still have question.

    thanks

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, currently you're adding every element in your list into the supposed set v (see that v.push_back()? That's what adds the element). But: you don't want to add an element into v if it's already in there. Therefore, where you're doing the push_back, you need to add a check to see if the new guy is a repeat and, if so, skip adding it to the list.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    thanks everybody, I did it....I just needed to put a function
    to remove duplicate values in vector using
    vec.erase(std::unique(vec.begin(), vec.end()), vec.end());

    thanks a lot for your inputs.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  2. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  4. working set, virtual bytes and private bytes
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-10-2008, 01:39 AM
  5. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21