Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 16
There are 1 users currently browsing forums.
C and C++ Programming C and C++ are both robust and hugely popular programming languages that are used in multiple facets of programming, ranging from games to operating systems to simple text editors. To dicuss and seek advice, ask around here.

Reply
  #1  
Old 11-15-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
c++ noob in weeder classes::help(youguys?){return happy}

Hello everyone!
I am new to this site and am basically getting destroyed by the weeder classes. Right now I have a c and am in danger of not passing. My programs just don't seem to pass test cases and I really, really want to stay in this field.
Am I just not built for this? I am trying really hard (i.e. all nighters). Am I just a huge fail or is this something I can grow out of?
I have never programmed prior to this quarter in school.
Any general advice?
Reply With Quote
  #2  
Old 11-15-2009
vento's Avatar
Sexy monkey
 
Join Date: May 2009
Location: Lithuania
Age: 16
Posts: 185
Rep Power: 1
vento is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

I think that learning C as the first programming language might be difficult. But it's definitely possible. So first of all, as I understand you must learn past, otherwise you gonna be kicked out of university.

First question that comes to mind is Must you use C? Maybe you can use C++? C++ is OOP language (use Google for more info, but at least know that C++ has classes), which makes programmers life easier. At least for me.

I don't know how advanced you are and what kinds of programs do you have to code, but first, you must know basics of C++. Try watching those videos: here or reading some tutorials here
If you must use C, you can read some tutorials here.

If you want, you can read whatever you want, but the main suggestion is to read some tutorials or books about it. But I guess you have done so.

So I'm very interested what kind of programs or algorithms do you have to write to pass. Is it some kind of mathematical algorithm, game or other programs. I guess algorithms as you wrote about tests. So there might be several reasons why you can't manage to write a good program:
1. You don't know programming language you use enough.
2. You do not understand the problem (task) well enough.
3. You haven't got enough knowledge in maths.
First case is easy to fix, I think. Second one usually goes together with 3rd. And the 3rd one... That's the main problem for me too (or just for me, IDK). Do you know about dynamic programming? matrix? Graphs? Those are extremely useful in solving many problems. Especially dynamic programming. All programs using dynamic programming run really fast.

P.S. To sum up. You have to find the weakest part of your programming and then, only then, you can fix it. Try to write what part is the biggest problem. Do you have problems writing a program (program doesn't do what you thought it gonna do)? Having an idea of an effective way to solve the problem (task)?
P.S.S. Do you have any friend (well, you should have if you're in a university) who can help you. Maybe tell the way your program should work. And the most important. Do not use code of others. Write your own. Your goal is to learn, not to pass the test (it might look different for you, but that's the way I think).
P.S.S.S. IF you didn't get what I want, write what causes the most difficulties when you have to write code you're supposed to. Or just write the way you do given tasks and what problems do you face in each step.
__________________

How to set up portable C++ IDE (Dev-C++)

Writing in C or C++ is like running a chain saw with all the safety guards removed," — Bob Gray.

Last edited by vento; 11-15-2009 at 09:18 AM. Reason: Added P.S.S.S.
Reply With Quote
  #3  
Old 11-15-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Yes I use c++ and here is an example of a project spec and my solution to the problem, which got a 73%. It is kind of lengthy, but here goes.
The spec:

Your assignment is to produce a library that provides functions for many common manipulations of vectors of strings. For example, one function will find where a string occurs in an unordered vector of strings. Another will change the order of strings in a vector. For each function you must write, this specification will tell you its interface (what parameters it takes, what it returns, and what it must do). It's up to you to decide on the implementation (how it will do it).
Code:
void setAll(vector<string>& v, string value);
Set all elements of v to value. 

int lookup(const vector<string>& v, string target);
Return the position of the first string in v that is equal to target. Return −1 if there is no such string. As noted above, case matters: Do not consider "mOe" to be equal to "Moe". 

int positionOfMax(const vector<string>& v);
Return the position of a string in v such that that string is >= every string in v. If there is more than one such string, return the smallest position of such a string. Return −1 if v has no elements. For example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
int i = positionOfMax(family);  // returns 1, since  marge  is latest
                // in alphabetic order

void flip(vector<string>& v);
Reverse the order of the elements of v. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
flip(family);
    // family now contains:  "maggie"  "lisa"  "bart"  "marge"  "homer"
bool rotateLeft(vector<string>& v, int pos);
If pos is not a valid position in v, return false. Otherwise, eliminate the item at position pos of v by copying all elements after it one place to the left. Put the item that was thus eliminated into the last position of v, and return true. Here's an example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
bool b = rotateLeft(family, 1);  // returns true
    // family now contains:  "homer"  "bart"  "lisa"  "maggie"  "marge"

bool rotateRight(vector<string>& v, int pos);
If pos is not a valid position in v, return false. Otherwise, eliminate the item at position pos of v by copying all elements before it one place to the right. Put the item that was thus eliminated into the first position of v (i.e., position 0), and return true. Here's an example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
bool c = rotateRight(family, 2);  // returns true
    // family now contains:  "bart"  "homer"  "marge"  "lisa"  "maggie"

int disagree(const vector<string>& v1, const vector<string>& v2);
Return the position of the first corresponding elements of v1 and v2 that are not equal. If the vectors are equal up to the point where one or both runs out, return the smaller of their sizes. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> people;
people.push_back("homer");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // people contains:      "homer"
int j = disagree(family, people);  //  returns 1
people.push_back("marge");
people.push_back("moe");
people.push_back("flanders");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // people now contains:  "homer"  "marge"  "moe"  "flanders"
int k = disagree(family, people);  //  returns 2

int lookupAny(const vector<string>& v1, const vector<string>& v2);
Return the smallest position in v1 of an element that is equal to any of the elements in v2. Return −1 if no element of v1 is equal to any element of v2. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> group;
group.push_back("moe");
group.push_back("flanders");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group contains:       "moe"  "flanders"
int m = lookupAny(family, group);  // returns -1 (family has neither of those)
group.push_back("maggie");
group.push_back("smithers");
group.push_back("bart");
group.push_back("lisa");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group now contains:   "moe"  "flanders"  "maggie"  "smithers"  "bart"  "lisa"
int n = lookupAny(family, group);  // returns 2 (family has "bart" there)

int subsequence(const vector<string>& v1, const vector<string>& v2);
If all elements of v2 appear in v1, consecutively and in the same order, then return the position in v1 where that subsequence begins. If the subsequence appears more than once in v1, return the smallest such beginning position. (Consider the empty sequence to be a subsequence of every sequence (even an empty one), at position 0.) Return −1 if v1 does not contain v2 as a contiguous subsequence. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> group1;
group1.push_back("marge");
group1.push_back("bart");
group1.push_back("lisa");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group1 contains:      "marge"  "bart"  "lisa"
int p = subsequence(family, group1);  // returns 1
vector<string> group2;
group2.push_back("bart");
group2.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group2 contains:      "bart"  "maggie"
int q = subsequence(family, group2);  // returns -1

int split(vector<string>& v, string splitter);
Rearrange the elements of v so that all the elements whose value is < splitter come before all the other elements, and all the elements whose value is > splitter come after all the other elements. Return the position of the first element that, after the rearrangement, is not < splitter, or v.size() if there are none. For example,
vector<string> characters;
characters.push_back("marge");
characters.push_back("smithers");
characters.push_back("moe");
characters.push_back("lisa");
characters.push_back("homer");
characters.push_back("bart");
        // characters contains:  "marge"  "smithers"  "moe"  "lisa"  "homer"  "bart"
int r = split(characters, "milhouse");  //  returns 4
    // characters must now be
    //    "marge"  "bart"  "homer"  "lisa"  "moe"  "smithers"
    // or   "homer"  "lisa"  "bart"  "marge"  "smithers"  "moe"
    // or one of several other orderings, as long as:
    //  * All elements < "milhouse" (the first 4) come before all others.
    //  * All elements > "milhouse" (the last 2) come after all others.
vector<string> cast;
cast.push_back("marge");
cast.push_back("bart");
cast.push_back("homer");
cast.push_back("lisa");
        // cast contains:  "marge"  "bart"  "homer"  "lisa"
int s = split(cast, "homer");  //  returns 1 // cast must now be either
    //    "bart"  "homer"  "marge"  "lisa"
    // or    "bart"  "homer"  "lisa"  "marge"
    // All elements < "homer" (i.e., "bart") come before all others.
    // All elements > "homer" (i.e., "lisa" and "marge") come after all others.


Sorry I don't know how to present this info in a more effective way, but now here is my solution:

//Greg Rivera
//11-1-09
//My own vector library
//Project 4: Vector Inspector

#include"std_lib_facilities.h"

void setAll(vector<string>& v, string value);
int lookup(const vector<string>& v, string target);
int positionOfMax(const vector<string>& v); //fix me? TA?
void flip(vector<string>& v);
bool rotateLeft(vector<string>& v, int pos);
bool rotateRight(vector<string>& v, int pos);
int disagree(const vector<string>& v1, const vector<string>& v2);
int lookupAny(const vector<string>& v1, const vector<string>& v2);
int subsequence(const vector<string>& v1, const vector<string>& v2);
void swap(vector<string>& v, int i);
int square(int x);
int split(vector<string>& v, string splitter);

int main()
{
    //string target;
    //cin >> target;
    //int pos;
    //cin >> pos;
    vector<string>v;    
    vector<string>v1;
    v1.push_back("ones");
    v1.push_back("thes");
    v1.push_back("zhizs");
    v1.push_back("is");
    //v1.push_back("going");
    //v1.push_back("on");
    //v1.push_back("here");
    
    string a = "ones";
    string b = "thes";
    string c = "shiz";
    string d = "the";
    string e = "shiz";
    string f = "zone";
    string g = "is";
    //for (int i =0; i != v.size(); i++)
    //{cout <<v[i]<<endl;}
    vector<string>v2;
    v2.push_back(a);
    v2.push_back(b);
    v2.push_back(c);
    v2.push_back(d);
    v2.push_back(e);
    v2.push_back(f);
    v2.push_back(g);
    string h = "five";
           //for (int i =0; i != v2.size(); i++)
           //{cout <<v2[i]<<endl;}
    //int result;
    //result = lookup(v2, "one");
    //result = subsequence(v1, v2);    
    //cerr << endl << endl;   
    int result =0;
    result = split(v2, a);
              //      for (int i =0; i != v2.size(); i++)
          //{cout << endl << v2[i];}


   cerr << result;
    
    //assert(lookup(v, target) == -1 );
    //cout << v[0] <<  endl << v[1] << endl << v[2] << endl << v[3] << endl << v[4];
    //cerr << result;
    cerr << endl;
//if (rotateRight(v2,100) == false)
//cerr<<"hell yes";

    keep_window_open();   //get this out of here
}
//------------------------------------------------------------------------------
int split(vector<string>& v, string splitter)
{
    //if its less than the splitter leave it alone. if not place it at the end of the vector
    //if not, swap with adjacent, and keep swapping until you swap with sometihng greater 
    //if , then just crap it at then end
    //make a loop that looks for the first element that is greater than the splitter
    //the vector should be in order, so this is just an easy for loop
 int m = 0;
 double terminator = 0;
 terminator = (.5 * square(v.size()-1) + (.5 * v.size()-1));
 //this was supposed to work, but string comparisons are failing for some reason
 cerr << "terminator is " << terminator; 
 do{
          
 for (int i = 0; i != v.size()-1; i++)
 {
         if ((v[i] > splitter) || (v[i] > v[i+1]))
         {swap(v, i);
         //keep moving the same element until it is in order
         cerr << endl;
          for (int i =0; i != v.size(); i++)
    {cout <<  v[i] << endl;
}
    }
    
 }         
           m++;
} while (m < terminator);
//terminator value equal to the function found, but string comparions...
  
  for (int i = 0; i != v.size(); i++)
  {
      if ( v[i] > splitter)
      return i;
  }
}  //if there is nothing less than the splitter return size
  
//------------------------------------------------------------------------------

int subsequence(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int z = 0;
    int index6 = 0;
    int v = 0;
    int m = 0;
    
    do{
        if (v1[i] != v2[j])
        {
                  j++;
        }
        
        if(v1[i] == v2[j])
        {i++;
        j++;
        index6 = i;
        z++;
        }
    } while ((i != v1.size()) && (j != v2.size()));
    if (z = v1.size())    
    return v1.size() - i;
}    
    //} while (i < v1.size()-2 //check if its 2
//------------------------------------------------------------------------------
int lookupAny(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int index4 = 0;
    //initialize variables
                      do {
    for (int i = 0; i < v1.size(); i++)
    {
        if (v1[i] == v2[j])
        {//check for equality
        index4 = i;
        break;
        //stop when equal
        }
    }
    j++;
                         } while (j != v2.size());   
    if (index4 == 0)                        
    return -1;
 
    return index4;     
}
//------------------------------------------------------------------------------
int disagree(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int index3 = 0;
    for (i,j; i < v1.size(), j < v2.size(); i++ , j++)
    {
        if ( v1[i] != v2[j])
        {
             return i;
             //when they disagree break out of this function and return location
        }
    }
    if ( (v1.size() == 0) || (v2.size() == 0) )
    return 0;
    //the following test just in case we run out of elements to check    
    if (v1.size() < v2.size())
    return v1.size();
    
    if (v2.size() < v1.size())
    return v2.size();    
}
//------------------------------------------------------------------------------
bool rotateRight(vector<string>& v, int pos)
{
     if(v.size() == 0)
     return false;
     if (pos > v.size()-1)
     return false;
     //check for vector size 0
     string tempstringD = v[pos];
     for (int i = pos; i > 0; i--)
     {
         v[i] = v[i-1];
         //move things to the right
     }
     v[0] = tempstringD;
     //repair the first element
     return true;
}
//------------------------------------------------------------------------------
bool rotateLeft(vector<string>& v, int pos)
{
     if (pos > v.size()-1)
     return false;
     if (v.size() == 0)
     return false;
     //check for vector size 0
     string tempstringC = v[pos];
     for (int i = pos; i != v.size()-1; i++)
     {
         v[i] = v[i+1];
         //move it to the left
     }
     v[v.size()-1] = tempstringC;
     //repair the last element
     return true;    
}     
//------------------------------------------------------------------------------
void flip(vector<string>& v)
{
       //flip the elements like reverse
       string tempstringA;
       string tempstringB;
       //the tempstrings are necessary to avoid data loss
       int i = 0;
       int j = v.size()-1-i;
       for(i, j;   i==j, i<j; i++, j--)       
       {//the two conditions are necessary for odd and even number sizes
       int j = v.size()-1-i;
       tempstringA = v[i];
       tempstringB = v[j];
       v[i] = tempstringB;
       v[j] = tempstringA;
       //replace elements with one another without losing data
       }
}
//------------------------------------------------------------------------------
//this function returns the last element if size is bigger than 5
int positionOfMax(const vector<string>& v)                 
{
    int index2 = 0;
    if (v.size() == 0)
    {return -1;
    //size is zero check
    }   
    string max = v[0];
    for (int i = 0; i != v.size()-1; i++)
    {
        //check for the biggest one
        if (v[i] < v[i+1])
        {
        max = v[i+1];
        index2 = i+1;
        //if the element to v[i]'s right is bigger, it becomes the new max
        //cerr << index2;
        }    
    }
    //cerr << index2 << max;
    return index2;
}
//------------------------------------------------------------------------------
int lookup(const vector<string>& v, string target)
{
    int index = -1; 
    if (v.size() == 0)
    return -1;      //if the size is zero return -1
     
    if (v.size() > 0 )
    for (int i = 0; i != v.size() ; i++)
    {
        if (v[i] == target)
        index = i;
        
        //check to see if any elements match the target
    }
    if (index == -1)
    return -1;
    //if there is no match return -1
    else
    return index;
    //if there is a match return index
}       
//--------------------------------------------------------------------
void setAll(vector<string>& v, string value)
{
     if (v.size() >= 1)
     for (int i = 0; i != v.size() ; i++)
     {   v[i] = value;
     }
}
     
void swap(vector<string>& v, int i)
{
     //this function swaps an element in a vector with the one to its right
     string tempstringC;
     string tempstringD;
     tempstringC = v[i];
     tempstringD = v[i+1];
     //tempstrings necessary to avoid data loss
     v[i]   = tempstringD;
     v[i+1] = tempstringC;
}


int square(int x)
{
    return x*x;
}
Well this is the kind of projects that I have and I spend a lot of time working on them for mediocre grades. I have been reading the Bjarne Stroustrup book and will look at your suggested material. I have never programmed before in my life and my course has just finished covering classes.

I appreciate the advice and help you are giving me I am really thankful

Last edited by Umang; 11-16-2009 at 07:36 AM.
Reply With Quote
  #4  
Old 11-15-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

I sent an example of one of my projects and my proposed solution (which got a grade of 73) a while ago but it never appeared. Anyways I had to create ten functions that manipulated vectors, and I thought I had the correct concepts.
My source code compiled and my tests seemed fine. But I guess not.
I have been reading the stroustrup book and have been looking at online tutorials and am writing down the mistakes i have made so as to not make them again. I have never programmed before and am using c++.
Reply With Quote
  #5  
Old 11-15-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Here is the project info:
Here are the functions you must implement:

void setAll(vector<string>& v, string value);
Set all elements of v to value.

int lookup(const vector<string>& v, string target);
Return the position of the first string in v that is equal to target. Return −1 if there is no such string. As noted above, case matters: Do not consider "mOe" to be equal to "Moe".
Code:
int positionOfMax(const vector<string>& v);
Return the position of a string in v such that that string is >= every string in v. If there is more than one such string, return the smallest position of such a string. Return −1 if v has no elements. For example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"

int i = positionOfMax(family);  // returns 1, since  marge  is latest
                // in alphabetic order

void flip(vector<string>& v);
Reverse the order of the elements of v. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
flip(family);
    // family now contains:  "maggie"  "lisa"  "bart"  "marge"  "homer"

bool rotateLeft(vector<string>& v, int pos);
If pos is not a valid position in v, return false. Otherwise, eliminate the item at position pos of v by copying all elements after it one place to the left. Put the item that was thus eliminated into the last position of v, and return true. Here's an example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
bool b = rotateLeft(family, 1);  // returns true
    // family now contains:  "homer"  "bart"  "lisa"  "maggie"  "marge"

bool rotateRight(vector<string>& v, int pos);
If pos is not a valid position in v, return false. Otherwise, eliminate the item at position pos of v by copying all elements before it one place to the right. Put the item that was thus eliminated into the first position of v (i.e., position 0), and return true. Here's an example:
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
bool c = rotateRight(family, 2);  // returns true
    // family now contains:  "bart"  "homer"  "marge"  "lisa"  "maggie"

int disagree(const vector<string>& v1, const vector<string>& v2);
Return the position of the first corresponding elements of v1 and v2 that are not equal. If the vectors are equal up to the point where one or both runs out, return the smaller of their sizes. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> people;
people.push_back("homer");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // people contains:      "homer"
int j = disagree(family, people);  //  returns 1
people.push_back("marge");
people.push_back("moe");
people.push_back("flanders");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // people now contains:  "homer"  "marge"  "moe"  "flanders"
int k = disagree(family, people);  //  returns 2

int lookupAny(const vector<string>& v1, const vector<string>& v2);
Return the smallest position in v1 of an element that is equal to any of the elements in v2. Return −1 if no element of v1 is equal to any element of v2. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> group;
group.push_back("moe");
group.push_back("flanders");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group contains:       "moe"  "flanders"
int m = lookupAny(family, group);  // returns -1 (family has neither of those)
group.push_back("maggie");
group.push_back("smithers");
group.push_back("bart");
group.push_back("lisa");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group now contains:   "moe"  "flanders"  "maggie"  "smithers"  "bart"  "lisa"
int n = lookupAny(family, group);  // returns 2 (family has "bart" there)

int subsequence(const vector<string>& v1, const vector<string>& v2);
If all elements of v2 appear in v1, consecutively and in the same order, then return the position in v1 where that subsequence begins. If the subsequence appears more than once in v1, return the smallest such beginning position. (Consider the empty sequence to be a subsequence of every sequence (even an empty one), at position 0.) Return −1 if v1 does not contain v2 as a contiguous subsequence. For example,
vector<string> family;
family.push_back("homer");
family.push_back("marge");
family.push_back("bart");
family.push_back("lisa");
family.push_back("maggie");
vector<string> group1;
group1.push_back("marge");
group1.push_back("bart");
group1.push_back("lisa");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group1 contains:      "marge"  "bart"  "lisa"
int p = subsequence(family, group1);  // returns 1
vector<string> group2;
group2.push_back("bart");
group2.push_back("maggie");
    // family contains:      "homer"  "marge"  "bart"  "lisa"  "maggie"
    // group2 contains:      "bart"  "maggie"
int q = subsequence(family, group2);  // returns -1

int split(vector<string>& v, string splitter);
Rearrange the elements of v so that all the elements whose value is < splitter come before all the other elements, and all the elements whose value is > splitter come after all the other elements. Return the position of the first element that, after the rearrangement, is not < splitter, or v.size() if there are none. For example,
vector<string> characters;
characters.push_back("marge");
characters.push_back("smithers");
characters.push_back("moe");
characters.push_back("lisa");
characters.push_back("homer");
characters.push_back("bart");
        // characters contains:  "marge"  "smithers"  "moe"  "lisa"  "homer"  "bart"
int r = split(characters, "milhouse");  //  returns 4
    // characters must now be
    //    "marge"  "bart"  "homer"  "lisa"  "moe"  "smithers"
    // or   "homer"  "lisa"  "bart"  "marge"  "smithers"  "moe"
    // or one of several other orderings, as long as:
    //  * All elements < "milhouse" (the first 4) come before all others.
    //  * All elements > "milhouse" (the last 2) come after all others.
vector<string> cast;
cast.push_back("marge");
cast.push_back("bart");
cast.push_back("homer");
cast.push_back("lisa");
        // cast contains:  "marge"  "bart"  "homer"  "lisa"
int s = split(cast, "homer");  //  returns 1 // cast must now be either
    //    "bart"  "homer"  "marge"  "lisa"
    // or    "bart"  "homer"  "lisa"  "marge"
    // All elements < "homer" (i.e., "bart") come before all others.
    // All elements > "homer" (i.e., "lisa" and "marge") come after all others.

And my proposed solution was:
//Greg R
//My own vector library
//Project 4: Vector Inspector

#include"std_lib_facilities.h"

void setAll(vector<string>& v, string value);
int lookup(const vector<string>& v, string target);
int positionOfMax(const vector<string>& v); //fix me? TA?
void flip(vector<string>& v);
bool rotateLeft(vector<string>& v, int pos);
bool rotateRight(vector<string>& v, int pos);
int disagree(const vector<string>& v1, const vector<string>& v2);
int lookupAny(const vector<string>& v1, const vector<string>& v2);
int subsequence(const vector<string>& v1, const vector<string>& v2);
void swap(vector<string>& v, int i);
int square(int x);
int split(vector<string>& v, string splitter);

int main()
{
   a main routine was not required for this project, since they appended their own main to our source
}
//------------------------------------------------------------------------------
int split(vector<string>& v, string splitter)
{
    //if its less than the splitter leave it alone. if not place it at the end of the vector
    //if not, swap with adjacent, and keep swapping until you swap with sometihng greater 
    //if , then just crap it at then end
    //make a loop that looks for the first element that is greater than the splitter
    //the vector should be in order, so this is just an easy for loop
 int m = 0;
 double terminator = 0;
 terminator = (.5 * square(v.size()-1) + (.5 * v.size()-1));
 //this was supposed to work, but string comparisons are failing for some reason
 cerr << "terminator is " << terminator; 
 do{
          
 for (int i = 0; i != v.size()-1; i++)
 {
         if ((v[i] > splitter) || (v[i] > v[i+1]))
         {swap(v, i);
         //keep moving the same element until it is in order
         cerr << endl;
          for (int i =0; i != v.size(); i++)
    {cout <<  v[i] << endl;
}
    }
    
 }         
           m++;
} while (m < terminator);
//terminator value equal to the function found, but string comparions...
  
  for (int i = 0; i != v.size(); i++)
  {
      if ( v[i] > splitter)
      return i;
  }
}  //if there is nothing less than the splitter return size
  
//------------------------------------------------------------------------------

int subsequence(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int z = 0;
    int index6 = 0;
    int v = 0;
    int m = 0;
    
    do{
        if (v1[i] != v2[j])
        {
                  j++;
        }
        
        if(v1[i] == v2[j])
        {i++;
        j++;
        index6 = i;
        z++;
        }
    } while ((i != v1.size()) && (j != v2.size()));
    if (z = v1.size())    
    return v1.size() - i;
}    
    //} while (i < v1.size()-2 //check if its 2
//------------------------------------------------------------------------------
int lookupAny(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int index4 = 0;
    //initialize variables
                      do {
    for (int i = 0; i < v1.size(); i++)
    {
        if (v1[i] == v2[j])
        {//check for equality
        index4 = i;
        break;
        //stop when equal
        }
    }
    j++;
                         } while (j != v2.size());   
    if (index4 == 0)                        
    return -1;
 
    return index4;     
}
//------------------------------------------------------------------------------
int disagree(const vector<string>& v1, const vector<string>& v2)
{
    int i = 0;
    int j = 0;
    int index3 = 0;
    for (i,j; i < v1.size(), j < v2.size(); i++ , j++)
    {
        if ( v1[i] != v2[j])
        {
             return i;
             //when they disagree break out of this function and return location
        }
    }
    if ( (v1.size() == 0) || (v2.size() == 0) )
    return 0;
    //the following test just in case we run out of elements to check    
    if (v1.size() < v2.size())
    return v1.size();
    
    if (v2.size() < v1.size())
    return v2.size();    
}
//------------------------------------------------------------------------------
bool rotateRight(vector<string>& v, int pos)
{
     if(v.size() == 0)
     return false;
     if (pos > v.size()-1)
     return false;
     //check for vector size 0
     string tempstringD = v[pos];
     for (int i = pos; i > 0; i--)
     {
         v[i] = v[i-1];
         //move things to the right
     }
     v[0] = tempstringD;
     //repair the first element
     return true;
}
//------------------------------------------------------------------------------
bool rotateLeft(vector<string>& v, int pos)
{
     if (pos > v.size()-1)
     return false;
     if (v.size() == 0)
     return false;
     //check for vector size 0
     string tempstringC = v[pos];
     for (int i = pos; i != v.size()-1; i++)
     {
         v[i] = v[i+1];
         //move it to the left
     }
     v[v.size()-1] = tempstringC;
     //repair the last element
     return true;    
}     
//------------------------------------------------------------------------------
void flip(vector<string>& v)
{
       //flip the elements like reverse
       string tempstringA;
       string tempstringB;
       //the tempstrings are necessary to avoid data loss
       int i = 0;
       int j = v.size()-1-i;
       for(i, j;   i==j, i<j; i++, j--)       
       {//the two conditions are necessary for odd and even number sizes
       int j = v.size()-1-i;
       tempstringA = v[i];
       tempstringB = v[j];
       v[i] = tempstringB;
       v[j] = tempstringA;
       //replace elements with one another without losing data
       }
}
//------------------------------------------------------------------------------
//this function returns the last element if size is bigger than 5
int positionOfMax(const vector<string>& v)                 
{
    int index2 = 0;
    if (v.size() == 0)
    {return -1;
    //size is zero check
    }   
    string max = v[0];
    for (int i = 0; i != v.size()-1; i++)
    {
        //check for the biggest one
        if (v[i] < v[i+1])
        {
        max = v[i+1];
        index2 = i+1;
        //if the element to v[i]'s right is bigger, it becomes the new max
        //cerr << index2;
        }    
    }
    //cerr << index2 << max;
    return index2;
}
//------------------------------------------------------------------------------
int lookup(const vector<string>& v, string target)
{
    int index = -1; 
    if (v.size() == 0)
    return -1;      //if the size is zero return -1
     
    if (v.size() > 0 )
    for (int i = 0; i != v.size() ; i++)
    {
        if (v[i] == target)
        index = i;
        
        //check to see if any elements match the target
    }
    if (index == -1)
    return -1;
    //if there is no match return -1
    else
    return index;
    //if there is a match return index
}       
//--------------------------------------------------------------------
void setAll(vector<string>& v, string value)
{
     if (v.size() >= 1)
     for (int i = 0; i != v.size() ; i++)
     {   v[i] = value;
     }
}
     
void swap(vector<string>& v, int i)
{
     //this function swaps an element in a vector with the one to its right
     string tempstringC;
     string tempstringD;
     tempstringC = v[i];
     tempstringD = v[i+1];
     //tempstrings necessary to avoid data loss
     v[i]   = tempstringD;
     v[i+1] = tempstringC;
}


int square(int x)
{
    return x*x;
}

Last edited by Umang; 11-16-2009 at 07:37 AM.
Reply With Quote
  #6  
Old 11-16-2009
vento's Avatar
Sexy monkey
 
Join Date: May 2009
Location: Lithuania
Age: 16
Posts: 185
Rep Power: 1
vento is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Well, there are lot's of functions, but to check the m all would take lots of time and I guess I'll not get any better idea, than you, If you have any exact problem making exact function (too slow, takes too much memory or doesn't work), then it's possible to check it and try to find the problem. You can try writing another program, that would check the function. It would generate random vector with random characters and test it. That's not so hard.

I took a look at one function - Flip(). If a human is testing, I think that he/she wouldn't give you all points for style. I'm not sure if my solution would get (I'll paste it), but yours - I don't believe it would. Well, it looks like it should work, but I don't get why do you use for loop for that and 2 strings. no need. For loops looks overcrowded and hard to read because of it. Try to make your code lighter, simpler and it will be even easier to check/find bugs/fix it.

I guess you know C++ enough, so it's about testing/getting ideas (your problem). I haven't learned to test well so far, I will, but I have no clue how to do so, so I have no suggestions about that.

Here is my version of Flip():
Code:
void reverse( std::vector< std::string > &v )
{
    std::string temp;
    int i = 0;
    int j = v.size()-1;
    
    while( i < j ){
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
        i++;
        j--;
    }
}
And I think that there is no need to copy vector's variable when i == j. It won't move anywhere else anyway.
Compared to your code, it saves 1 string and, in my opinion, is more readable.

P.S. It doesn't mean you have to use while loop everywhere. For loop better in some cases. Actually in many cases, but you shouldn't use it if it becomes overcrowded.
__________________

How to set up portable C++ IDE (Dev-C++)

Writing in C or C++ is like running a chain saw with all the safety guards removed," — Bob Gray.

Last edited by vento; 11-16-2009 at 12:14 PM. Reason: I wrote j++ instead of j--
Reply With Quote
  #7  
Old 11-17-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

I am not really sure why I used it well it was the first thing I came up with and I thought it worked. Well basically what I do is I write things on a white board and try to come up with some algorithms that seem to work. But more often than not, my code gets broken down. I am hoping that more experience will eventually make me write more reliable code.

I thought the i == j check would check for an even number for the size of the vector. Or at least I thought it would hehe.

So are you a professional programmer? Your status says child lol?
Reply With Quote
  #8  
Old 11-17-2009
vento's Avatar
Sexy monkey
 
Join Date: May 2009
Location: Lithuania
Age: 16
Posts: 185
Rep Power: 1
vento is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Yeah, I've noticed that Child yesterday too Well, I'm not a proffesional programmer. Actually I'm still in a high school, but I'll study Software Engineering (hopefully) in University and work as a programmer. I've always wished to work in game development as I find it more enjoyable to write game engine than anything else, but... You know, money talks and I'll go wherever I get paid more.

I'm learning about algorithms in a Saturday school I go. The main goal of it is to train for Olympiads, but I guess it's helpful anyway.

As I said, I'm not a professional programmer, but I know something about programming style As we get additional points for that (in competitions).

So you might ignore me, if you want to. Some people do not want to talk with under-age "children".
__________________

How to set up portable C++ IDE (Dev-C++)

Writing in C or C++ is like running a chain saw with all the safety guards removed," — Bob Gray.
Reply With Quote
  #9  
Old 11-17-2009
Toddler
 
Join Date: Nov 2009
Posts: 16
Rep Power: 0
gregr is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Lol I am not one of those people I enjoy and appreciate the advice you give me. I am a freshman at ucla and I guess the programming is very challenging to me because I have never been exposed to it before. My course is moving ridiculously fast (surprise, surprise). I think its great that you are learning to code before the university level since you won't encounter any problems that I have.

As for me I don't know what specific field I want to be in but I know for sure that I want to write code. I am a computer science major (without the engineering, I have never been a fan of hardware). I want to try to familiarize myself with hardware to see if I like it but I am not so interested I really want to write code for things that people will use everyday.

I have just noticed that I am a toddler hehe.
Reply With Quote
  #10  
Old 11-17-2009
vento's Avatar
Sexy monkey
 
Join Date: May 2009
Location: Lithuania
Age: 16
Posts: 185
Rep Power: 1
vento is on a distinguished road
Re: c++ noob in weeder classes::help(youguys?){return happy}

Hehe I think you should ask friends from university to help you. For example ask for contacts and later ask if they have any good idea about one or another function you're having a problem.

P.S. Try reading about C/C++ style (here). It will need it later for sure. You will have to learn it if you wanted to participate in bigger projects. Programming style is not an official thing that is protocoled (at least I don't know any such protocol), but it's made by programmers so the code is easier to read for everyone.
__________________

How to set up portable C++ IDE (Dev-C++)

Writing in C or C++ is like running a chain saw with all the safety guards removed," — Bob Gray.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Money Make You Happy? tossy Chit Chat and Hangout 17 11-11-2008 08:46 AM
Happy 4th of July. Webdomain.com Chit Chat and Hangout 11 07-06-2007 01:17 PM
Happy new year! YoungCoder Chit Chat and Hangout 2 12-31-2004 07:01 PM