This code returns:
array size is: 3
1 is present in the array
1 2 3
1 0
I want the code to return this:
array size is: 3
1 is present in the array
1 2 3
1 3
I've been messing with it for over an hour now and I can't figure out what is wrong exactly or how to fix it. I'm not allowed to use vectors or anything preprogrammed function to do it. I can't change the header or the main file either. So it has to be something with how I've done the loop in the removeNumber function. Any suggestions or help is appreciated!
Header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef VARARRAY_H_
#define VARARRAY_H_
class varArray{
public:
varArray(); // void constructor
int arraySize() const { return size; } // returns the size of the array
int check(int number); // returns index of element containing "number" or -1 if none
void addNumber(int); // adds number to the array
void removeNumber(int); // deletes the number from the array
void output(); // prints the values of the array
private:
int *dArray; // pointer to the dynamically allocated array
int size; // array size
};
#endif /* VARARRAY_H_ */
#include <iostream>
#include "vararray.h"
using std::cout; using std::endl;
varArray::varArray() {
// void constructor
size = 0;
dArray = newint[size];
}
int varArray::check(int number){
// returns index of element containg "number" or -1 if none
for (int i = 0; i < size; i++) {
if (dArray[i] == number) {
return i;
}
}
return -1;
}
void varArray::addNumber(int number){
// adds number to the array
int a = check(number);
if (a == -1) {
int *p = newint[size + 1];
for (int i = 0; i < size; ++i) {
p[i] = dArray[i];
}
delete[] dArray;
p[size] = number;
size = size + 1;
dArray = p;
}
}
void varArray::removeNumber(int number){
// deletes the number from the array
bool found = false;
for (int i = 0; i < size; i++) {
if (dArray[i] == number) {
found = true;
}
}
if (found == true) {
int b = check(number);
size = size - 1;
int *p = newint[size];
for (int i = 0; i < size; i++) {
if (i != b) {
p[i] = dArray[i];
}
}
delete[] dArray;
dArray = p;
}
}
void varArray::output(){
// prints the values of the array
for (int i = 0; i < size; i++) {
cout << dArray[i] << " ";
}
cout << "\n";
}
#include <iostream>
#include "vararray.h"
using std::cout; using std::endl;
int main(){
varArray a1;
// testing regular member functions
a1.addNumber(1);
a1.addNumber(2);
a1.addNumber(3);
a1.addNumber(3); // trying to add duplicate, should not add it
cout << "array size is: " << a1.arraySize() << endl;
if (a1.check(1) != -1) // check() returns -1 if number not present
cout << "1 is present in the array" << endl;
if (a1.check(5) != -1)
cout << "5 is present in the array" << endl;
a1.output();
a1.removeNumber(2);
a1.output();
}
@keskiverto For your question regarding line 47, I used the boolean found to prevent that from happening. It only does that if found returns true.
For line 48 I don't know what forgetting the size of the current array does in regards to my problem.
For line 52 I have tried doing this here before and it doesn't work either.
1 2 3 4 5 6 7 8
for (int i = 0; i < size; i++) {
if (i < b) {
p[i] = dArray[i];
}
if (i > b) {
p[i-1] = dArray[i];
}
}
size = size - 1;
i = 0;
{
if (i < b) {
p[i] = dArray[i];
}
if (i > b) {
p[i-1] = dArray[i];
}
}
i = 1;
{
if (i < b) {
p[i] = dArray[i];
}
if (i > b) {
p[i-1] = dArray[i];
}
}
i = 2;
Do you now see why the new size never considers the last element of dArray?
line 47, I used the boolean found
I see. You loop the array first to see whether it contains the number and then you loop the same array again to find the same number again in order to store the index too. That is unnecessary code repetition and execution.
1 2 3 4 5 6
void varArray::removeNumber( int number ) {
constauto b = check( number );
if ( -1 < b ) {
// remove element
}
}
The i<b and i>b are mutually exclusive. Furthermore, 0<=b<size.
1. Loop range [0..b[ -- i<b is true
2. Loop range ]b..size[ -- b<i is true
3. --size;