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

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Array Element
Click For Summary
SUMMARY

The forum discussion addresses a programming issue related to deleting elements from an array in C++. The user’s code fails to handle the case when the index is zero, causing the program to terminate unexpectedly. The solution provided by user sandy.bridge includes changing the return value for the case when no deletion is requested, modifying the condition for error messages, and adjusting the loop termination condition. Implementing these changes ensures the program functions correctly when attempting to delete elements from the beginning of the array.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with arrays and indexing in C++
  • Knowledge of control flow statements (if, while) in C++
  • Basic debugging techniques in C++ programming
NEXT STEPS
  • Review C++ array manipulation techniques
  • Learn about error handling in C++ programs
  • Explore C++ functions and return values
  • Study best practices for user input validation in C++
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting array manipulation issues in their code.

sandy.bridge
Messages
797
Reaction score
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)
{
	count << "Array: " << '{';
	int i = 0;
	while (i < size)
	{
		count << theArray[i];
		if (i < size-1){count << ',';}
		i++;
	}
	count << '}' << endl << "Want to delete something (y/n)? ";
	char ansr;
	cin >> ansr;
	if (ansr == 'y')
	{
		count << "Offset to delete? ";
		int offset;
		cin >> offset;
		return offset;
	}
	else if (ansr == 'n')
	{
		count << "Goodbye!";

	}
	return 0;
}

int	arrayB(int theArray2[], int size, int offset)
{
	if ((offset >= size)||(offset < 0)){ count << "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
sandy.bridge: To repair your program, do the following.

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

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

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K