i need the help to fix thie problrm in this code ( The output is 0 0, which means both are not the same? The assignment has already stated the first 2 are considered the same.)
this is the question
••• E6.10Write a function
bool same_elements(int a[], int b[], int size)
that checks whether two arrays have the same elements in some order, with the same multiplicities. For example,
1 4 9 16 9 7 4 9 11
and
11 1 4 9 16 9 7 4 9
would be considered identical, but
1 4 9 16 9 7 4 9 11
and
11 11 7 9 16 4 1 4 9
would not. You will probably need one or more helper functions.
#include <iostream> using namespace std; bool same_elements(int a[], int b[], int size) { bool *temp = new bool[size], t; for (int i = 0; i < size; i++) { temp[i] = false; } for (int i = 0; i < size; i++) { t = false; for (int j = 0; j < size; j++) { if (a[i] == b[j]) { if (temp[j] == false) { temp[j] = true; t = true; } } } if (t == false) { return false; } } delete[] temp; return true; } int main() { int arr1[] = {1, 4, 9, 16, 9, 7, 4, 9, 11}; int arr2[] = {11, 1, 4, 9, 16, 9, 7, 4, 9}; int arr3[] = {11, 11, 7, 9, 16, 4, 1, 4, 9}; cout << same_elements(arr1, arr2, 9) << endl; cout << same_elements(arr1, arr3, 9) << endl; return 0; }
Thanks for the question.
Here is the completed code for this problem. Comments are included
so that you understand whats going on. Let me know if you have any
doubts or if you need anything to change.
Thank You !!
========================================================================================
#include <iostream>
using namespace std;
// create a copy of the array without altering the contents
of the original array
void populateArray(int *sorted, const int *actual, int size){
for(int i=0;i<size;i++)
*(sorted+i)=*(actual+i);
}
// sort the array using bubble sort() algorithm
void sortArray(int *sorted, int size){
int i, j;
for (i = 0; i < size-1; i++)
// Last i elements are already in place
for (j = 0; j < size-i-1; j++){
if (sorted[j] > sorted[j+1]) {
int temp = sorted[j];
sorted[j]=sorted[j+1];
sorted[j+1]=temp;
}
}
}
// temp function to print the arrays
void printArray(int *arr, int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<"
";
}
cout<<endl;
}
bool same_elements(int a[], int b[], int size) {
int *sorted_a= new int[size]; // create a temporary
array for a
int *sorted_b= new int[size]; // create a temporary
array for a
populateArray(sorted_a,a,size); // populate the new
array with elements in a
populateArray(sorted_b,b,size); // populate the new
array with elements in b
sortArray(sorted_a,size); // sort the array
sortArray(sorted_b,size); // sort the array
printArray(sorted_a,size);printArray(sorted_b,size);
// comment this line
// iterate each element in the two sorted arrays to
see if they match
for(int i=0;i<size;i++){
// if the values at a particular
index mistmatches return false immediately
if(*(sorted_a+i)!=*(sorted_b+i)){
return
false;
}
}
// if all check passes return true
return true;
}
int main()
{
int arr1[] = {1, 4, 9, 16, 9, 7, 4, 9, 11};
int arr2[] = {11, 1, 4, 9, 16, 9, 7, 4, 9};
int arr3[] = {11, 11, 7, 9, 16, 4, 1, 4, 9};
cout <<boolalpha<< same_elements(arr1, arr2, 9)
<< endl;
cout <<boolalpha<< same_elements(arr1, arr3, 9)
<< endl;
return 0;
}
========================================================================================
i need the help to fix thie problrm in this code ( The output is 0 0, which means both are not the same? The assignment has already stated the first 2 are considered the same.) t...
Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...
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[],...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() { const int n = 50; int *arr,...
The second picture that I attached is the input and output. I already did the code but I also have to add the partition number assigned to the job (if the job was allocated). Can you please do that for me? I copied and pasted my code below. #include <iostream> #include <string.h> #include <stdio.h> using std::cout; using std::cin; using std::endl; unsigned int memorySize; // Function prototypes unsigned int FirstFit(struct Partition *, int, struct Job *, int); unsigned int BestFit(struct Partition...
I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....
Need help in c++ programming to output the lines in my code: if word if found: cout << "'" << word << "' was found in the grid" << endl; cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...
I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...
C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) { // Add your code here. string line; getline(inFile, line); int sp = 0; for (int i = 0; i < line.size(); i++) { if (line[i] == ' ') sp++; } sp++; return sp; } int measureLines(ifstream& inFile) { // Add your code here. string line; int n = 0; while (!inFile.eof()) { getline(inFile,...
I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...