How do you link two arrays? - arrays

I'm in a basic programming class, and everything is done in pseudo code.
My question is this: How do you link two arrays?
I have a single-dimensional array that lists students names, and I have a two-dimensional array that lists the top eight scores of each student...this is all fine and dandy, but now I need to sort the arrays by the students name. I'm poked around online and read through the books chapter twice, it only briefly mentions linking two arrays but shows no examples.
If it's any help, we are using bubble-sorting, and that is what I am fairly familiar with...I can sort the names, that's the easy part, but I don't know how to sort the grades so they do not go out of order.
Thanks for the input!
Sidenote: I got it figured out! I ended up doing how Greg Hewgill had mentioned. As I put in my comment to his suggestion, I started randomly throwing in lines of code until that idea hit me...it doesn't look pretty (one module swapped the names, another to swap the grades, and a third even then to swap the individual students grades earlier on in a multidimensional array), but it indeed seemed to work...no way to test it in a language as I have no compiler nor have I enough knowledge to make the pseudo code into actual code if I were to download one, but it sounds really good on the paper I typed it out on!
As I also mentioned in the note, I do thank everyone for their speedy and helpful insight, I actually didn't even think I'd get a reply tonight, thank you everyone again for all your help!
Jeffrey

Define a simple Student class like this:
public class Student : IComparable<Student>
{
public string Name { get; set; }
public int[] Scores { get; set; }
#region IComparable<Student> Members
public int CompareTo(Student other)
{
// Assume Name cannot be null
return this.Name.CompareTo(other.Name);
}
#endregion
}
then even simpler
var students = new[] {
new Student(){ Name = "B", Scores = new [] { 1,2,3 } },
new Student(){ Name = "C", Scores = new [] { 3,4,5 } },
new Student(){ Name = "A", Scores = new [] { 5,6,7 } }
};
Array.Sort(students);
will do the work for you.

What you may want to do is the following: As you're sorting the names and you have to swap two positions, do the same swap in the array of scores. That way, all changes that you make to the names array will be reflected in the scores array. When you're done, the scores will be in the same sorted order as the names are.
There are more effective ways of doing this with different data structures, as other comments will show.

Your premise is wrong. You shouldn't have two array in the first place.
You should have one array of objects, each of which holds a student's name and his scores:
public class Record
{
public string Student;
public int[] Scores;
}

Two approaches: first, when sorting the names, each time you exchange two names, exchange the rows (or columns or whatever you want to call them) of scores in the same positions. At the end, the scores should still be in sync with the names.
Second, instead of sorting the names, create a third array that will contain the indexes into either of the other two arrays, initially 0 through n-1, but then sorted, comparing name[a] and name[b], instead of sorting the names array itself.

Related

Sorting a 2 dimensional array of objects in Kotlin

I have a static 2 dimensional array of objects in a Kotlin project:
class Tables {
companion object{
lateinit var finalTable: Array<Array<Any?>?>
}
}
It is a little clearer in Java:
public class Tables {
public static Object[][] finalTable;
}
The third element in one row of objects in the table, is a string boxed as an object. In other words: finalTable[*][2] is a string describing the item. When I add an item to the array in Kotlin, I want to sort the entire array in alphabetical order of the description.
In Java this is easy:
Arrays.sort(Tables.finalTable, Comparator.comparing(o -> (String) o[2]));
When I try to use Android Studio to translate the Java code into Kotlin, it produces the following:
Arrays.sort( Tables.finalTable, Comparator.comparing( Function { o: Array<Any?>? -> o[2] as String }) )
This does not work, you have change the String cast as follows:
Arrays.sort( Tables.finalTable, Comparator.comparing( Function { o: Array<Any?>? -> o[2].toString() }) )
This version will compile and run, but it totally messes up the sorting of the table, so it does not work. I have tried variations on this theme, without any success. To get my project to work, I had to create a Java class in the Kotlin project with the functional Java code listed above:
public class ArraySort {
public void sortArray(){
Arrays.sort(Tables.finalTable, Comparator.comparing(o -> (String) o[2]));
}
}
This sorts the table like a charm, but I would prefer to keep my project "pure Kotlin". Can anyone suggest a pure Kotlin method to sort such an array? Thanks!
Unless I'm missing something, you can just do this:
Tables.finalTable.sortBy { it[2] as String }
which sorts your array in place. sortedBy will produce a new copy of the original if that's what you want instead, and might be why the comment suggestions weren't working for you.
But this whole unstructured array situation isn't ideal, the solution is brittle because it would be easy to put the wrong type in that position for a row, or have a row without enough elements, etc. Creating a data structure (e.g. a data class) would allow you to have named parameters you can refer to (making the whole thing safer and more readable) and give you type checking too

Why does map function mutate array of objects when operating on each element's attribute?

I have an array of objects:
class Person
attr_accessor :email
def initialize(email)
#email = email
end
end
array = [
Person.new('hello#gmail.com'),
Person.new('world#gmail.com')
]
I created a clone from the original array to perform map function, and then I mapped over each element to make its email attribute become uppercase:
clone = array.clone
clone.map { |obj|
obj.email.upcase!
obj
}
puts array.inspect # why is the original being mutated
puts clone.inspect
It mutates the original array. I have experimented with both dup and clone. and I get the same result. Why does map mutate the objects when operating on each element's attribute?
You cloned the array containing Person references, but you did not change the array; you changed the Person instances themselves. clone is so-called "shallow clone", which copies only the receiver object, but none of the objects whose references it may contain.
In real-world logic: you took a piece of paper on which you wrote "Jenny, Timmy". Then you copied it to another piece of paper. You then took the first piece of paper, found the people it refered to, and gave them an apple. Then you took the second piece of paper, found the people on it, and wondered where their apples came from. But there's only one Timmy, only one Jenny: you give the first list's Jenny an apple, the second list's Jenny also has one.
If you want to clone something, clone Jenny.
array.map { |person|
person.clone.yield_self { |clone|
clone.email = clone.email.upcase
}
}
(Note that I didn't use clone.email.upcase!. The reason is the same reason all over again: if you clone an object, they will both use the same string for email. upcase! changes that string, which would uppercase both clone's email and the original's email. Thus, we make a new email string for the clone.)
Things like this are best understood by stepping through the visualisation using this tool. However, the tool runs Ruby 2.2, which doesn't know about yield_self; this code is equivalent:
array.map { |person|
clone = person.clone
clone.email = clone.email.upcase
clone
}
You could also write this, though it won't visualise as clearly:
array.map(&:clone).map { |clone|
clone.email = clone.email.upcase
}

Keep order details in arrays?

could you please help me.
1) Is it better to keep orders in the EA's arrays rather than querying the system with the Order.. commands in mql4? Keeping data in arrays means that you have to query the system less and that internet reliability may be less of an issue. However, the coding required to keep an accurate order book is quite cumbersome.
2) How do you keep track of orders that is on the same Symbol but has come from two different EA's?
Thank you very much
It depends on your needs and ideas, without that it could be quite difficult to tell anything.
you can keep an array of ticket numbers (or CArrayObj) but need to check that ticket exists before doing other operations (like trail). if you have problems with internet - change vps and do not try to solve it with coding.
Each ea keeps a book of its own deals.
Cannot imagine sence of keeping just numbers of tickets, but maybe it exists. If you need to store some data in addition to what can be achieved from Order...() then use classes or structures, some fields might be filled with osl,tp,oop,lot,magic, symbol etc once and do not call Order.() functions later except OrderProfit(),OrderClosePrice() and OrderCloseTime()-such functions would be called all the time.
Example of how to store data is below: instances of CTrade are added to CArrayObj
#include <Object.mqh>
#include <Arrays\ArrayObj.mqh>
class CTrade : public CObject
{
private:
int m_ticketId;
double m_oop,m_osl,m_otp,m_lot;//OrderOpenPrice() and sl, tp, lot-add more
public:
CTrade(const int ticket){
m_ticketId=ticket;
}
bool isTicketExist(){
if(OrderSelect(m_ticketId,SELECT_BY_TICKET))
return(OrderCloseTime()==0);
else return(false);//or GetLastError()!=4108
}
};
CArrayObj* listOfTrades=NULL;
int OnInit(void){
listOfTrades=new CArrayObj;
}
void OnDeinit(const int reason){
if(CheckPointer(listOfTrades)==POINTER_DYNAMIC)
delete(listOfTrades);
}
void OnTick(){
for(int i=listOfTrades.Total()-1;i>=0;i--){
CTrade *trade=listOfTrades.At(i);
if(!trade.isTicketExist())
{listOfTrades.Delete(i);continue;}
//do trail or what you need
} // - loop over the array when necessary but clean it first
}
listOfTrades.Add(new CTrade(ticket));// - way to add elements to the list

How to find which element failed comparison between arrays in Kotlin?

I'm writing automated tests for one site. There's a page with all items added to the cart. Maximum items is 58. Instead of verification of each element one by one I decided to create 2 arrays filled with strings: 1 with correct names : String and 1 with names : String I got from the site. Then I compare those 2 arrays with contentEquals.
If that comparison fails, how do I know which element exactly caused comparison fail?
Short simple of what I have now:
#Test
fun verifyNamesOfAddedItems () {
val getAllElementsNames = arrayOf(materials.text, element2.text,
element3.text...)
val correctElementsNames = arrayOf("name1", "name2", "name3"...)
val areArraysEqual = getAllElementsNames contentEquals correctElementsNames
if (!areArraysEqual) {
assert(false)
} else {
assert(true)
}
}
This test fails if 2 arrays are not the same but it doesn't show me the details, so is there a way to see more details of fail, e.g. element that failed comparison?
Thanks.
I recommend using a matcher library like Hamcrest or AssertJ in tests. They provide much better error messages for cases like this. In this case with Hamcrest it would be:
import org.hamcrest.Matchers.*
assertThat(getAllElementsNames, contains(*correctElementsNames))
// or just
assertThat(getAllElementsNames, contains("name1", "name2", "name3", ...))
There are also matcher libraries made specifically for Kotlin: https://github.com/kotlintest/kotlintest, https://yobriefca.se/expect.kt/, https://github.com/winterbe/expekt, https://github.com/MarkusAmshove/Kluent, probably more. Tests using them should be even more readable, but I haven't tried any of them. Look at their documentation and examples and pick the one you like.
You need to find the intersection between the two collections. Intersection will be the common elements. After than removing the intersection collection from the collection you want to perform the test will give you the complementary elements.
val intersection = getAllElementsNames.intersect(correctElementsNames)
getAllElementsNames.removeAll(intersection)

Select random item from an array with certain probabilities and add it to the stage

Its quite a big task but ill try to explain.
I have an array with a list of 200 strings and I want to be able to randomly select one and add it to the stage using code. I have movieclips exported for actionscript with the same class name as the strings in the array. Also, if it is possible, would I be able to select the strings with predictability such as the first has a 0.7 chance the second a 0.1 etc. Here is what i have currently
var nameList:Array=["Jimmy","Bob","Fred"]
var instance:DisplayObject = createRandom(nameList);
addChild(instance);
function createRandom(typeArray:Array):*
{
// Select random String from typeArray.
var selection:String = typeArray[ int(Math.random() * typeArray.length) ];
// Create instance of relevant class.
var Type:Class = getDefinitionByName(selection) as Class;
// Return created instance.
return new Type();
}
All this throws me this error
ReferenceError: Error #1065: Variable [class Jimmy] is not defined.
Ive searched for other threads similar but none combine the three specific tasks of randomisation, predictability and addChild().
I think that you've got two problems: a language problem and a logic problem. In the .fla connected to your code above, in the Library find each symbol representing a name and write into the 'AS linkage' column for that symbol the associated name -- e.g., 'Bob,' 'Fred' -- just the name, no punctuation.
Now getDefinitionByName() will find your 'Class'
If you put a different graphic into each MovieClip -- say, a piece of fruit or a picture of Bob,Jim, Fred -- and run your program you'll get a random something on stage each time.
That should solve your language problem. But the logic problem is a little harder, no?
That's why I pointed you to Mr. Kelly's solution (the first one, which for me is easier to grasp).

Resources