Join 137,264 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,506 people online right now. Registration is fast and FREE... Join Now!
I got my project today, and I read over it, but I still am not sure about what to do, so I can't really start. Can anyone pseudo code or provide a general explanation of my task?
This post has been edited by aznballerlee: 4 Nov, 2006 - 02:10 PM
Hmm well I think the most important instruction line was this: Task: Your assignment is to produce a library that provides functions for many common manipulations of arrays of strings. For example, one function will find where a string occurs in an unordered array of strings. Another will reverse the order of strings in an array. 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). Here are the functions to use:
int tally(const string a[], int n, string target); Return the number of strings in the array that are equal to target
int findFirst(const string a[], int n, string target); Return the index of the first string in the array that is equal to target. Return -1 if there is no such string.
int indexOfMin(const string a[], int n); Return the index of a string in the array that is <= every string in the array. If there is more than one such string, return the smallest index of such a string. Return -1 if the array has no elements.
int insert(string a[], int n, string s, int pos, int max); Insert the string s into the array at index pos, moving the n minus pos existing elements starting at pos one place to the right to make room. Return the index of the element inserted.
int erase(string a[], int n, int pos); Eliminate the item at index pos by copying all elements after it one place to the left. Return the index of the item that was deleted
nt disagree(const string a1[], int n1, const string a2[], int n2); Return the index of the first corresponding elements of a1 and a2 that are not equal. n1 is the number of interesting elements in a1, and n2 is the number of interesting elements in a2. If the arrays are equal up to the point where one or both runs out, return the smaller of n1 and n2.
int removeDups(string a[], int n); For every sequence of consecutive identical items in a, remove all but one item of that sequence from a. Return the number of items left in a.
bool subsequence(const string a1[], int n1, const string a2[], int n2); If all n2 elements of a2 appear in a1, in the same order (though not necessarily consecutively), then return true. Return false if a1 does not contain a2 as a subsequence. (Of course, the empty sequence is a subsequence of any sequence.) Return false (instead of -1) if this function is passed any bad arguments.
int mingle(const string a1[], int n1, const string a2[], int n2, string result[], int max); If a1 has n1 elements in nondecreasing order, and a2 has n2 elements in nondecreasing order, place in result all the elements of a1 and a2, arranged in nondecreasing order, and return the number of elements so placed. Return -1 if the result would have more than max elements or if a1 and/or a2 are not in nondecreasing order.
int split(string a[], int n, string splitter); Rearrange the elements of the array 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 index of the first element that, after the rearrangement, is not < splitter, or n if there are none.
That's a whole lot of functions .. I don't even know the purpose of them, or the objective of using these functions ..
I read it over once again. Pretty much, there's lots of functions where I have to manipulate and carry out a task.
But still, my problem is understanding the task (switching the array statements? .. searching for positions?) What am I supposed to do as the final step? Or in other words, what is the objective of running all these functions??
Please help! It's due in a couple days and I want to get started!
I read it over once again. Pretty much, there's lots of functions where I have to manipulate and carry out a task.
But still, my problem is understanding the task (switching the array statements? .. searching for positions?) What am I supposed to do as the final step? Or in other words, what is the objective of running all these functions??
Please help! It's due in a couple days and I want to get started!
Azn
You will do better if you work on the individual blocks required. You will find if you start with findFirst you will get some ideas on the other..
Yes, gregoryH that is what I just tried. I just need some people to tell me if what I'm doing is right so far .. or what I need to fix .. I tried the first two functions (people say they're the easiest)
So here are my codes for those:
tally function
CODE
int tally(const string a[], int n, string target);
// Return the number of strings in the array that are equal to target.
if (n <= 0) return -1;
else result = 0; for (int i=0; i<n; i++) { if (target == [i] result += i;
else result;
}
return result;
findFirst function
CODE
int findFirst(const string a[], int n, string target);
// Return the index of the first string in the array that is equal to target. // Return -1 if there is no such string.
if (n < 0) return 1;
else int result; for (int i=0; i<n; i++) { if (target == a[i]) result = i; break;
else result = -1; continue; } return result;
This post has been edited by aznballerlee: 3 Nov, 2006 - 08:56 PM
Yes, gregoryH that is what I just tried. I just need some people to tell me if what I'm doing is right so far .. or what I need to fix .. I tried the first two functions (people say they're the easiest)
So here are my codes for those:
Everything I am about to state is based on this data in a[]
greg
azn
horace
greg
robert
john
steve
findFirst function
CODE
int findFirst(const string a[], int n, string target); // Return the index of the first string in the array that is equal to target. // Return -1 if there is no such string. if (n < 0) return 1;
else int result; for (int i=0; i<n; i++) { if (target == a[i]) result = i; break;
else result = -1; continue; } return result;
Using 'robert' as the target value,
CODE
i a[i] result 0 greg -1 1 azn -1 2 horace -1 3 greg -1 4 robert 4
A logic error in the test of n <0, you return +1, which may give the person using your code the impression that a mathcing string was found. The methodology used is a little sub-optimal, this outline should help you
CODE
int result = -1; if ( n < 0 ) return result; // no need for an else for ( int i = 0; i < n; i++) if ( source[i]==target) result = i;
return result;
tally function
CODE
int tally(const string a[], int n, string target);
// Return the number of strings in the array that are equal to target.
if (n <= 0) return -1;
else result = 0; for (int i=0; i<n; i++) { if (target == [i] result += i;
else result;
}
return result;
If you tabulate the for loot variable i along with the value of 'greg' as the match item:
CODE
i a[i] result 0 greg 0 1 azn 0 2 horace 0 3 greg 3 4 robert 3 5 john 3 6 steve 3
Based on your specification, you seem to have misunderstood the purpose. You need to just make the code count the number of matches and not do anything else.
There is a syntax error here if (target == [i], and a logical error here result += i; which you could detect by using the method I have used above (and without a compiler).
Keep reading the specification as you code, and try to think a little outside the square. For example, just create a main function where you can test each code block until you have it right, then copy the code to the final integrated version.
int findFirst(const string a[], int n, string target);
int findFirst(const string a[], int n, string target) { // Return the index of the first string in the array that is equal to target. // Return -1 if there is no such string.
int result = -1;
if (n <= 0) return result; else for (int i=0; i<n; i++) { if (a[i] == target) result = i;
I tried to run the findFirst function .. and it didn't compile correctly!
It said that the line "assert(findFirst(a,6,"meg") == 3);" wasn't true! What's up with that?
Hmmm
When you are telling us "compile" you are telling us something completely different to the story you now say.
If the program runs, it has successfully compiled and its now called run-time errors.
Assert works by checking that you actually have a true or non-NULL result. since "meg" is in position 3, it suggests that you are getting an incorrect result from the function.