How can I fix my program to properly delete elements from an array?

In summary, in order to fix a problem with a code that is supposed to delete an offset and shift the elements after it to the left, the expert suggests changing the first return statement to return a different value, adding a condition to the cout statement for when the offset is -999, changing the condition for the do-while loop to also check for -999, and moving the goodbye message to the end of the main function.
  • #1
sandy.bridge
798
1

Homework Statement


Hey all. I have code that is supposed to delete the offset determined by the user and shift the elements after that offset to the left. My code works fine except for one problem: if the index is zero it terminates. I will post my code, and if anyone can see why it is giving me this problem, I would greatly appreciate it!

The Attempt at a Solution



PHP:
#include <iostream>
using namespace std;

int arrayA(int theArray[],int size)
{
	cout << "Array: " << '{';
	int i = 0;
	while (i < size)
	{
		cout << theArray[i];
		if (i < size-1){cout << ',';}
		i++;
	}
	cout << '}' << endl << "Want to delete something (y/n)? ";
	char ansr;
	cin >> ansr;
	if (ansr == 'y')
	{
		cout << "Offset to delete? ";
		int offset;
		cin >> offset;
		return offset;
	}
	else if (ansr == 'n')
	{
		cout << "Goodbye!";

	}
	return 0;
}

int	arrayB(int theArray2[], int size, int offset)
{
	if ((offset >= size)||(offset < 0)){ cout << "Sorry, cannot delete that." << endl; return size;}
	for (int m = offset + 1; m < size; m++)
	{
		theArray2[m-1]=theArray2[m];
	}
    size--;

	return size;
}
int main() {
	int N = 10;
	int y;
	int fling[]={1,2,3,4,5,6,7,8,9,10};

	do{
	y = arrayA(fling, N);
	N = arrayB(fling, N, y);

	}while ((N > 0)&&(y != 0));
	return 0;
}
 
Physics news on Phys.org
  • #2
sandy.bridge: To repair your program, do the following.

(1) Change your first "return 0;" to "return -999;".
(2) Change 'cout << "Sorry, cannot delete that." << endl;' to 'if (offset != -999){cout << "Sorry, cannot delete that." << endl;}'.
(3) Change "}while ((N > 0)&&(y != 0));" to "}while ((N > 0)&&(y != -999));".
(4) Move 'cout << "Goodbye!\n";' to the end of main(), immediately before "return 0;".

Try it, and see if it now works.
 
Last edited:

What is the purpose of deleting an element from an array?

Deleting an element from an array is used to remove a specific value from the array. This can help to keep the array organized and relevant to the data being stored.

How can I delete an element from an array in JavaScript?

To delete an element from an array in JavaScript, you can use the splice() method. This method takes in two parameters - the index of the element to be deleted and the number of elements to be deleted. It then modifies the original array by removing the specified element.

Can I delete multiple elements from an array at once?

Yes, it is possible to delete multiple elements from an array at once using the splice() method. You can specify the index of the first element to be deleted and the number of elements to be deleted as parameters, and the method will remove all the specified elements from the array.

What happens to the length of an array when an element is deleted?

When an element is deleted from an array, the length of the array changes to reflect the new number of elements. For example, if an array has 5 elements and one element is deleted, its length will become 4. This can be useful for keeping track of the number of elements in an array after deletion.

Is it possible to delete an element from an array without modifying the original array?

Yes, it is possible to delete an element from an array without modifying the original array by using the slice() method. This method creates a new array with the specified element removed, leaving the original array unchanged. However, this method does not work for arrays with nested elements or objects.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
749
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
2
Views
932
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top