Question

In: Computer Science

since firstN is a string it can't compare to an ArrayList * list so how am i suppose to write these parts comparing the string to the list[mid]

c++ question:

int ArrayList::binarySearchID(string firstN) const
{
   int first = 0;      
   int last = length - 1;      
   int mid = (first + last) / 2;
   bool found = false;
   int index;
   while (!found && last >= first)
   {
       if (firstN == list[mid])
       {
           found = true;
       }
       else if (firstN > list[mid])
       {
           first = mid - 1;
       }
       else if (firstN < list[mid])
       {
           last = mid - 1;
           mid = (first + last) / 2;
       }
   }
   if (found) {
       cout << "Index" << list[index] << " in array list." << endl;
       return mid;
   }
   else
   {
       return -1;
   }
}


Q: since firstN is a string it can't compare to an ArrayList * list so how am i suppose to write these parts comparing the string to the list[mid]?

"if (firstN == list[mid])"

Q:How am i suppose to print the index?

( cout << "Index" << list[index] << " in array list." << endl; )

Solutions

Expert Solution

int ArrayList::binarySearchID(string firstN) const
{
   int first = 0;      
   int last = length - 1;      
   int mid = (first + last) / 2;
   bool found = false;
   int index;
   while (!found && last >= first)
   {
       if (firstN == list[mid])
       {
           found = true;
                   index = mid;
       }
       else if (firstN > list[mid])
       {
           first = mid - 1;
       }
       else if (firstN < list[mid])
       {
           last = mid - 1;
           mid = (first + last) / 2;
       }
   }
   if (found) {
       cout  << list[index] << " is found on index " << index << " in array list." << endl;
       return index;
   }
   else
   {
       return -1;
   }
}

Since you have not shared the code for arraylist class, i am not sure how you are storing your objects/elements in arraylist.. Ideally for an arraylist, you will be having an array of string, inside which you must be putting your elements..
Then using that array if you say arr[i], then it will give you string value on index i.. I am assuming you have array with name list in class, hence when you say list[mid], it gives you the string value stored on index mid in the array..


Related Solutions

So I am suppose to write a short essayon this and I found some things online,...
So I am suppose to write a short essayon this and I found some things online, but I am having trouble putting it into my own words. Can I have help with this please and thank you! Macropoland is currently experiencing a recession--consumption and investment are very sluggish, and unemployment is quite high at 9%. Currently, inflation is very low at 0.4% (the historical average rate of inflation is about 2%). The Macropolish President has just hired you as her...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
I am trying to write the code for an 8 bit adder in VHDL so that...
I am trying to write the code for an 8 bit adder in VHDL so that I can program it onto my Elbert V2 Spartan 3A FPGA Development Board, but I keep getting errors. Any ideas what I am doing wrong? library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity adder8bit is Port ( a : in STD_LOGIC_VECTOR(7 downto 0); b : in STD_LOGIC_VECTOR(7 downto 0); cin : in STD_LOGIC; o : out STD_LOGIC_VECTOR(7 downto 0); cout : out STD_LOGIC); end adder8bit; architecture Behavioral...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find an example. I need a step by step guide. Who can help me write it please and Thank you
How am I supposed to implement a c_str method that returns a c string representation of...
How am I supposed to implement a c_str method that returns a c string representation of a String object? I need to return a csting representation of a string object. I am confused on what cstrings are and where to go. method signature is as follows: char* c_str();
How would I get rid of punctuation that's in a file so that I am only...
How would I get rid of punctuation that's in a file so that I am only left with words after turning them into a list in Python. So if the file contained "and in the dream, I am flying.", how would I get a list that contained ['and', 'in', 'that', 'dream', 'I', 'am', 'flying'] not ['and', 'in', 'that', 'dream,', 'I', 'am', 'flying.']
So, I have 2 Window Server 2016 Virtual Machines and I am not sure how to...
So, I have 2 Window Server 2016 Virtual Machines and I am not sure how to create 2 NICs for each server. Can someone explain how to create it?
PYTHON: How do I split up the binary string into chunks of 8 so that they...
PYTHON: How do I split up the binary string into chunks of 8 so that they are converted from binary to ASCII characters? For example "0011101000101001" would be eventually converted to ":)" . The code I have below is what I currently have. It currently only works for binary strings of 8. argv[1] is the text file it is reading from and argv[2] is basically just 8. import sys filename=sys.argv[1] length = int(sys.argv[2]) message_data = open(filename, "r") message_text = message_data.read()...
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT