Question

My code is not doing what I want it to do and I can't seem to...

My code is not doing what I want it to do and I can't seem to figure out why it's not working. I commented some of the code to give some context to what I'm trying to do. Can someone check to see where my code is off. I'm assuming that I didn't use the counters properly or I'm using the pointers incorrectly. I can only use pointers to do this by the way.

Code:

#include <iostream>

using namespace std;

const int LEFT_SIZE = 8;
const int RIGHT_SIZE = 9;

void Union(int left[], int left_size, int right[], int right_size, int result[], int& result_size);
void test_Union();


int main()
{
test_Union();

return 0;
}

void Union(int left[], int left_size, int right[], int right_size, int result[], int& result_size)
{
int* walker1;
int* walker2;
int* walker3;
int count = 0; //counter to break while loop
int counter1 = 0; //counter1 counts up the size of the left array
int counter2 = 0; //counter2 counts up the size of the right array

walker1 = left; //points at left array
walker2 = right; //points at right array
walker3 = result; //points at result array (empty array gets filled up by left and right array)

while(count < left_size || count < right_size)
{
if(*walker1 < *walker2)
{
*walker3 = *walker1;
walker1++;
walker3++;
counter1++;
result_size = counter1; //result_size goes up by 1 when value in left array is smaller than right array
}
else if(*walker1 > *walker2)
{
*walker3 = *walker2;
walker2++;
walker3++;
counter2++;
result_size = counter2; //result_size goes up by 1 when value in right array is smaller than left array
}
else
{
*walker3 = *walker1;
walker1++;
walker2++;
walker3++;
result_size = counter1++; //result_size goes up by 1 when the left and right array have same number
}
count++;
}

while(counter1 < *(walker1 + counter1))
{
result_size = counter1++; //result_size is getting the remaining size of the left array
}

while(counter2 < *(walker2 + counter2))
{
result_size = counter2++; //result_size is getting the remaining size of the right array
}
}

void test_Union() //this function prints out the union array(the result array)
{
int* walker;
int left[] = {5, 10, 15, 20, 25, 30, 35, 40};
int right[] = {3, 10, 13, 20, 23, 28, 30, 33, 35};
int unionArr[0];
int totalSize = 0;

walker = unionArr;
Union(left, LEFT_SIZE, right, RIGHT_SIZE, unionArr, totalSize);
for(int i = 0; i < totalSize; i++)
{
cout << *walker << " ";
walker++;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream> using namespace std; const int LEFT_SIZE = 8; const int RIGHT_SIZE = 9; void Union(int left[], int left_s

+1 else if (*walker1 > *walker2) *walker3 = *walker2; walker2++; walker3++; counter2++; result_size++; //result_size goes up

void test_Union() //this function prints out the union array(the result array) int* walker; int left[] = { 5, 10, 15, 20, 25,

Code

#include <iostream>

using namespace std;

const int LEFT_SIZE = 8;
const int RIGHT_SIZE = 9;

void Union(int left[], int left_size, int right[], int right_size, int result[], int& result_size);
void test_Union();


int main()
{
   test_Union();

   return 0;
}

void Union(int left[], int left_size, int right[], int right_size, int result[], int& result_size)
{
   int* walker1;
   int* walker2;
   int* walker3;
   int count = 0; //counter to break while loop
   int counter1 = 0; //counter1 counts up the size of the left array
   int counter2 = 0; //counter2 counts up the size of the right array

   walker1 = left; //points at left array
   walker2 = right; //points at right array
   walker3 = result; //points at result array (empty array gets filled up by left and right array)

   // We exit this loop once no element is remaining in any array for comparison
   while (counter1 < left_size && counter2 < right_size)
   {
       if (*walker1 < *walker2)
       {
           *walker3 = *walker1;
           walker1++;
           walker3++;
           counter1++;
           result_size++; //result_size goes up by 1 when value in left array is smaller than right array
       }
       else if (*walker1 > *walker2)
       {
           *walker3 = *walker2;
           walker2++;
           walker3++;
           counter2++;
           result_size++; //result_size goes up by 1 when value in right array is smaller than left array
       }
       else
       {
           *walker3 = *walker1;
           walker1++;
           walker2++;
           walker3++;
           counter1++;
           counter2++;
           result_size++; //result_size goes up by 1 when the left and right array have same number
       }
   }

   while (counter1 < LEFT_SIZE) // Now remaining elements of First array
   {
       *walker3 = *walker1;
       counter1++;
       walker1++;
       walker3++;
       result_size++; //result_size is getting the remaining size of the left array
   }

   while (counter2 < RIGHT_SIZE) // Now remaining elements of First array
   {
       *walker3 = *walker2;
       counter2++;
       walker2++;
       walker3++;
       result_size++; //result_size is getting the remaining size of the right array
   }
}

void test_Union() //this function prints out the union array(the result array)
{
   int* walker;
   int left[] = { 5, 10, 15, 20, 25, 30, 35, 40 };
   int right[] = { 3, 10, 13, 20, 23, 28, 30, 33, 35 };

   // Maximum size of union array can be equal to sum of both array sizes if both have unique elements
   int *unionArr = (int*)malloc((LEFT_SIZE+RIGHT_SIZE)*sizeof(int));
   memset(unionArr, -1, LEFT_SIZE + RIGHT_SIZE); // initializing final array

   int totalSize = 0;

   walker = unionArr;
   Union(left, LEFT_SIZE, right, RIGHT_SIZE, unionArr, totalSize);
   for (int i = 0; i < totalSize; i++)
   {
       cout << *walker << " ";
       walker++;
   }
}

Output

B5 19 13 15 20 23 25 28 30 33 35 40

Add a comment
Know the answer?
Add Answer to:
My code is not doing what I want it to do and I can't seem to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • What is wrong with my code, when I pass in 4 It will not run, without...

    What is wrong with my code, when I pass in 4 It will not run, without the 4 it will run, but throw and error. I am getting the error   required: no arguments found: int reason: actual and formal argument lists differ in length where T is a type-variable: T extends Object declared in class LinkedDropOutStack public class Help { /** * Program entry point for drop-out stack testing. * @param args Argument list. */ public static void main(String[] args)...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • I want to compare the runtimes and swap operations times among quick Sort, selection Sort and...

    I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT