Problem: I\'m not sure how to make my void function below remove the item from t
ID: 3822821 • Letter: P
Question
Problem: I'm not sure how to make my void function below remove the item from the array Miami that i've created. The array is miami_array, I've put in bold that void function that I'm having trouble with. Once the code runs the list should sort itself out with the item removed, but that's not happening for me.
My Code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
void selectionSort (int a[ ], int size);
void binsearch (const int a[], int len, int item, int& index, bool& found);
void remove (int a[], int& len, int item);
int main ()
{
int * chicago_array;
int * miami_array;
int j;
ifstream fin;
const int test = 10;
int index = 0;
int file_size;
int file_size2;
int product_number;
fin.open ("chicago.txt");
fin >> file_size;
const int FILESIZE = file_size;
chicago_array = new int [FILESIZE];
fin >> chicago_array[0];
for (j = 1; j < FILESIZE; j++)
{
fin >> chicago_array[j];
}
fin.close();
fin.open ("miami.txt");
fin >> file_size2;
const int FILESIZE2 = file_size2;
miami_array = new int [FILESIZE2];
fin >> miami_array[0];
for (j = 1; j < FILESIZE2; j++)
{
fin >> miami_array[j];
}
fin.close();
selectionSort (chicago_array, file_size);
selectionSort (miami_array, file_size2);
int x=0;
int y=0;
for (j=100; j < 120; j++)
{
if (chicago_array[x] == j)
{
cout << j << " Chicago ";
bool found;
binsearch (miami_array, file_size2, j, index, found);
cout << endl;
x++;
}
if (miami_array[y] == j)
{
cout << j << " Miami " << endl;
y++;
}
}
cout << "Enter the product to purchase (-1 to quit): ";
cin >> product_number;
if (product_number = -1)
{
cout << "quit";
}
else
{
remove (miami_array, file_size2, product_number);
}
selectionSort (chicago_array, file_size);
selectionSort (miami_array, file_size2);
int t=0;
int u=0;
for (j=100; j < 120; j++)
{
if (chicago_array[t] == j)
{
cout << j << " Chicago ";
bool found;
binsearch (miami_array, file_size2, j, index, found);
cout << endl;
t++;
}
if (miami_array[u] == j)
{
cout << j << " Miami " << endl;
u++;
}
}
for (j = 0; j < FILESIZE; j++)
{
cout << chicago_array[j];
}
cout << endl << endl;
for (j = 0; j < FILESIZE2; j++)
{
cout << miami_array[j];
}
system ("pause");
return 0;
}
void selectionSort (int a[ ], int size)
// PRE: size is array size
// POST: values in array are sorted in ascending order
{ int mindex; // index of current minimum value
for (int ct1=0; ct1 < size-1; ct1++) // repeat this loop 1 less than number of items in array
{ mindex = ct1; // record current minimum this pass
for (int ct2=ct1+1; ct2 < size; ct2++) // loop through remaining array values
if (a[ct2] < a[mindex]) mindex = ct2; // record index of current minimum
swap(a[mindex],a[ct1]); // swap current minimum into place
}
}
void binsearch (const int a[], int len, int item, int& index, bool& found)
// PRE: 0 < len <= array size
// POST: found is true if item is found in array; else false
// index is the index of the first occurrence of item if found, else insert index
{
int first = 0; // initialize first to index 0
int last = len-1; // initialize last to last index
found = false; // initialize found to false
while (!found && last >= first) // loop while more items and not found
{
index = (first + last) / 2; // compute middle index
if (item == a[index]) found = true; // check if found
else if (item < a[index]) last = index - 1; // else prepare to look at left side
else first = index + 1; // else prepare to look at right side
}
if (item == a[index]) cout << "Miami"; // where the item should be inserted
}
int j;
void remove (int a[], int& len, int item)
{
for (j = 0; j < len; j++)
{
if (a[j] = item)
{
a[j] = 0;
}
}
}
Explanation / Answer
Hi,
As per your remove function, if the element is equal to item you make it to zero.
To check if the element is equal to item use ==(you have choosen =). I am adding the updated function. Correction is marked in bold.
void remove (int a[], int& len, int item)
{
for (j = 0; j < len; j++)
{
if (a[j] == item) // you need to compare, for comparison use ==
{
a[j] = 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.