Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9179289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:45:39+00:00 2026-06-17T17:45:39+00:00

There is something wrong with my code. My goal is to translate the pseudocode

  • 0

There is something wrong with my code.
My goal is to translate the pseudocode into java code
Yes my coding is an assignment, I don’t want any answer just to show me where the problem is

What I have to do is to compute the size of the intersection between two unsorted list of students containing no duplicates.

I will show the pseudocode and my java code corresponding to this pseudocode.

Pseudo code:

inter <-- 0

Array C[m+n]

for i <-- 0 to m-1  do  C[i] <-- A[i]

for i <-- 0 to n-1  do  C[i+m] <-- B[i]

C <-- sort(C, m+n);

pointer <-- 0

while (pointer < m+n-1) do{

if(C[pointer]=C[pointer+1]){

inter <-- inter+1

pointer <-- pointer+2

}

else pointer <-- pointer+1

}

return inter

Java Code:

public static int intersectionSizeMergeAndSort(studentList L1, studentList L2) {
/* Write your code for question 4 here */
  int intersectionSize = 0;
  int[] C = new int[L1.studentID.length+L2.studentID.length];
  for(int i = 0; i<L1.studentID.length; i++){
  C[i] = L1.studentID[i];
  }
  for(int i = 0; i<L2.studentID.length; i++){
  C[i+L1.studentID.length] = L2.studentID[i];
  }
  Arrays.sort(C);
  int pointer = 0;
  while(pointer<((C.length-1))){
    if(C[pointer] == C[pointer+1]){
    intersectionSize = intersectionSize + 1;
    pointer = pointer + 2;
    }
    else {
      pointer = pointer + 1;
  }
 return intersectionSize;

}
return 0;
}

My main method:

public static void main(String args[]) throws Exception {

studentList firstList;
studentList secondList;

// This is how to read lists from files. Useful for debugging.

// firstList=new studentList("COMP250.txt", "COMP250 - Introduction to Computer Science");
// secondList=new studentList("MATH240.txt", "MATH240 - Discrete Mathematics");

// get the time before starting the intersections
long startTime = System.currentTimeMillis();

// repeat the process a certain number of times, to make more accurate average     measurements.
 for (int rep=0;rep<1000;rep++) {

 // This is how to generate lists of random IDs. 
 // For firstList, we generate 16000 IDs
 // For secondList, we generate 16000 IDs

 firstList=new studentList(2 , "COMP250 - Introduction to Computer Science");
 secondList=new studentList(2 , "MATH240 - Discrete Mathematics");


 // run the intersection method
 int intersection=studentList.intersectionSizeMergeAndSort(firstList,secondList);
 System.out.println("The intersection size is: "+intersection);
 }

// get the time after the intersection
long endTime = System.currentTimeMillis();


System.out.println("Running time: "+ (endTime-startTime) + " milliseconds");
 }

}

Note: L1 and L2 have been declared previously
But I am not getting the results I am aiming for.
Can someone point out what is wrong ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:45:40+00:00Added an answer on June 17, 2026 at 5:45 pm

    Off the top of my head, it appears to me that your return intersectionSize; statement occurs within your while loop, so that you loop never gets beyond the first iteration and does not properly compute the intersectionSize. I would delete that statement and replace your return 0; with return intersectionSize; like so…

    public static int intersectionSizeMergeAndSort(studentList L1, studentList L2) {
       /* Write your code for question 4 here */
       int intersectionSize = 0;
       int[] C = new int[L1.studentID.length + L2.studentID.length];
       for (int i = 0; i < L1.studentID.length; i++) {
          C[i] = L1.studentID[i];
       }
       for (int i = 0; i < L2.studentID.length; i++) {
          C[i + L1.studentID.length] = L2.studentID[i];
       }
       Arrays.sort(C);
       int pointer = 0;
       while (pointer < (C.length - 1)) {
           if (C[pointer] == C[pointer + 1]) {
              intersectionSize = intersectionSize + 1;
              pointer = pointer + 2;
           } else {
              pointer = pointer + 1;
           }
        }
        return intersectionSize;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is something wrong with my code. I am teaching myself c# and one
My program/process exited abnormally? I doubt something is wrong with the following code: There
There must be something wrong. I don't know what? see this on jsfiddle .
Is there something wrong with the .dll file? Shouldn't it just consider the code
There's something wrong with this code, but I can't find what's causing it. bool
Is there something wrong with this code because when i press the button to
Is there something wrong with the code? I can fetch nothing using this predicate.
is there something wrong in the code? its not actually updating all textinput but
Is there something wrong with this code? I'm trying to set up some buttons
Can anyone see if there is something wrong in my matlab code? My objective

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.