• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialize elements of a sub-array without overwritting the first ones

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello forum,
i was wondering if someone could give me a hand with this problem im having. the issue is that when i initiate the elements of a sub-array, i dont want to overwrite the first elements with later elements that also fit the condition.
Here is the snippet of code with some comments



what should be displayed is [[2], [3], [0, 3] , [1, 2]] however im getting [[2], [3], [3, 3] , [2, 2]] because i believe that the loop is checking whether there is a later student that fits the criteria and replacing the first element with the last element that fits the criteria. As in the numbers 1 and 0 do appear at some point throughout the process but are overwritten because of the way my code is.

I would greatly appreciate if someone could tell me how to initialize the elements of the sub-arrays in order without this overwriting issue.
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain exactly what you are trying to do. Only when you know what you are going to do can you work out how to do it.
Remember that array elements can always be re‑assigned; if somebody writes myArray[i] = xyz; that code will either run normally or cause an exception to be thrown. It is not possible to make an array element final.
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i would like to do is to create a 2D array called similarities using the method findSimilarAnswers. What it is supposed to do is to iterate through every student (with the first loop i) (in my test there were four students). it will initiate the length of the sub-arrays of similarities with the number of similar matches with other students, given a certain threshold. (in my particular test with a threshold of 1 similar wrong answer, student 0 had one match with student 2, student 1 had one match with student 3, student 2 had two matches (one with student 0 and one with student 3) and student 3 also had two matches with students 1 and 2.
Therefore the similarities array (with threshold 1) should be [[0],[0],[0,0],[0,0]]

Now for each student (iterated through the i loop) (except for himself, hence the if(i!=j) that you helped me with in my first post), we will check through all the other students answers (loop j) and see if the number of similar wrong answers is above or equal to the threshold. if so, we will initialize the element of the similarities sub-array with the index of the student with similarities to the student being observed in that iteration of the loop.

The problem i am getting is that given the code i have written, the first element of the sub-array of similarities gets overwritten with the last index of student with similar answers. so for instance, for student at index 2, the first element of that sub-array should be 0, however, given that student 3 also has similar answers, that first element (which i want to be 0) gets overwritten to element 3. Im not sure if i made myself clear... please let me know if i wasn't.

Also, i apologize for not explaining this process in the OP, i figured the comments in the code were sufficient. im sorry, im still new at this...
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sounds very complicated, and you are mixing your requirements with planned techniques. You need to get the requirements clear before you an write any code.
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By requirements do you mean "what do i have to do?" if so, the answer is "Create a 2D array where the element(s) of each sub-array is the index of the student(s) with similar answers"
so for example [[2],[3],[0,3],[1,2]]  --> this would mean that:
Student 0 has similar answers to student 2
Student 1 has similar answers to student 3
Student 2 has similar answers to student 0 and 3
Student 3 has similar answers to student 1 and 2

My problem is that the way my code is written, i get  [[2],[3],[3,3],[2,2]]
I believe this is because the way the loops are right now, the first element gets overwritten to the last student that has similar answers. Do you see what i mean?
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

What Campbell just said, is that you need to explain your task without using any technical terminology.
Pretend you are explaining that to your history teacher, so he/she could understand.

For example (explanation doesn't match your real requirements explanation):
I am given a bunch of answers. My main task is to find similar answers. Similar answers are those, which have... Also I need to define, among how many students those answers were spread...

 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for clarifying, i wasn't sure what was meant. I will try to be more concise in the future.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the help, Liutauras.
Once you have worked out the requirements, it will be easier to reate the necessary code
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you see my post right before Liutauras post? I believe it was what you were asking. Let me know if it clarifies things and if you can help me with my issue
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Guitard-Maraj wrote:Did you see my post right before Liutauras post?

Yes

I believe it was what you were asking. . . .

No, it showed an example of output. Imagine I am a history teacher, and know nothing about programming. Now explain what you are trying to do in the terms Liutauras used. No use of words like array, method, or similar. No examples of output. All words very small and simple . . .




. . . and I shall be asleep and shall look at it in the morning.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Guitard-Maraj wrote:I believe it was what you were asking. Let me know if it clarifies things and if you can help me with my issue


Were you told to use the techniques you are using now? What we are trying to say is, that possibly there are better ways to solve your problem, we just don’t know the problem yet, if we knew, we could discuss what these better ways might be.

In the current code you showed us you are mentioning student several times, but we fail to see Student class in there. So we got a suspicion that you dived too early to implementation details without analysing and understanding actual problem at first. This is what we were trying to tell you.

Now if you got specific technical instructions word by word what you need to do, well, we saw some of these in the past too, then you’ll have to stick to them probably, nobody just likes such tasks. So we are lacking some of understanding about the given task still. Please clarify.
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The goal of this assignment is to grade students in MC exams and try and determine whether they have cheated by looking at similarities between wrong answers. Each section has strict instructions to follow. The first section simply gives a percentage grade for each student. The second section involves finding the number of similar wrong answers between two students. The third section involves finding the number of students with whom a focal student has above a certain threshold of similar answers. The final section (the one i am having trouble with) involves assigning to each student, the other students that have similar answers above a certain threshold.

I appreciate your expertise, however i am not allow to used (complicated) techniques that we have not yet learned in class. The following code is about the extent of the techniques i am allowed to use. For the most part, the techniques (loops, arrays...etc) are the ones i am supposed to use. Here is the full code with, (hopefully, useful) comments :

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is exactly the reason why comments are considered as code smells. As I scan the code in the listing, my brain is trying its hardest to ignore the text in green and understand the actual code. My brain is not that good, apparently, as it is being foiled in its efforts to comprehend by code written by a student.

I think I get what you're trying to do, however, it will take a while to try to decipher your code and wade through all the noise that the copious amount of "helpful" comments you have placed as hurdles to comprehension creates.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benjamin Guitard-Maraj wrote:By requirements do you mean "what do i have to do?" . . .

No, it means, “What do you want to achieve?” That is slightly different.

Student 0 has similar answers to student 2
Student 1 has similar answers to student 3
Student 2 has similar answers to student 0 and 3
Student 3 has similar answers to student 1 and 2 . . .

Yes, but how are you going to put that sort of thing into an array? An array has a predetermined size, which you cannot change. So, if student 999 has a different answer from everybody else, how are you going to convert your array to a 0‑length array? If student 999 gives a different answer from everybody else, what is their array going to look like? This makes me wonder whether arrays are a suitable tool for this sort of exercise.

What follows is the sort of thing we are looking for:-
You are trying to collect the student numbers for the students who gave the same answer as a particular student. You want to repeat that procedure for all students.
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To speed up things for the OP, I've removed comments and reduced some line lengths which were too long + some add spaces around binary operators.

OP's code (with removed comments + minor formatting changes):
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:how are you going to put that sort of thing into an array? An array has a predetermined size, which you cannot change. So, if student 999 has a different answer from everybody else, how are you going to convert your array to a 0‑length array? If student 999 gives a different answer from everybody else, what is their array going to look like? This makes me wonder whether arrays are a suitable tool for this sort of exercise.



The requirements of the assignment strictly say to create an array. If a student has different answers than everyone else, you would get the equivalent result by have a similarities threshold that is too high. For example if we run findSimilarAnswers(responses, solution, 5) the resulting array that the assignment wants there to be is simply an empty array [[ ], [ ] , [ ] , [ ]].
the size of the array is created by the following code


the code after that is supposed to fill the array correctly, however that's what im having trouble with and was hoping one of you could help me.
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, im sorry about the supposed "useful" comments. i did not realize that they would be a hindrance to the comprehension of my code. Posting on this forum has been a learning experience for me and i appreciate all of your willingness to help me despite all these annoyances.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comments come in various grades of usefulness:-That is a good helpful comment, even better if you know what Bedzryczowski's algorithm is, or whether there is such a thing The following is better.That tells you that Bedzryzowski worked in Warsaw in those days when it was a world‑leader in maths, and where you can find more details of the algorithm. Also note that comment is now too long for a // comment, so it is necessary to use a block comment instead. You can see similarly useful comments like this:-Some people like // end comment, but many don't.Those end comments don't add anything that the indentation doesn't make clear, except possibly in a do loop:-A line with while ending with a semicolon is confusing at the best of times.
We often see code like this:-As Piet Souris pointed out a few weeks ago, those comments are useful reminders of what ode needs to be written, but after the code has been written, they don't help. Least of all when they say somebody has told somebody else to close a Scanner reading System.in
Another useful comment is like this:-. . . but of you really want to cause annoyance, try this sort of thing
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . if you really want to cause annoyance, try this sort of thing

Maybe this would be even better
 
Benjamin Guitard-Maraj
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thank you for the useful knowledge about comments. ill be sure to use it in the future.

As for the problem i was having, a friend of mine was able to help me with it. Basically the last y loop was starting from 0 every time which was the issue. Here is the modified code that yields the desired result:

 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done for figuring out.

Now, think how you could simplify this method. It is pretty hard to read and understand it. There is a deep nesting structure which is called Arrow Anti Pattern. So think how you could extract each nested block to its own method and see if you can get code easier understandable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic