Проблема переполнения массива C++
Пожалуйста, помогите! Код не работает, когда пользователь вводит цифры больше 20. Я должен вернуть false, если пользователь вводит более 20, но я думаю, что мой код неверен.
Вот мой код для проверки массива:
if(numElems + 1 >= CAPACITY) // inside the inserElement function return false; if((position<0) || (position>numElems)) //inside the removeElement function return false;
Что я уже пробовал:
#include <iostream> using namespace std; const int CAPACITY = 20; // displayArray - display the array on a single line separated by blanks. // @param: int array[] is an unordered array of integers // @param: int numberOfElements // [Already implemented] //[Already implemented] void displayArray(int array[], int numElems); //ToDo: Declare a function fillArray that fills an int array with values entered // by the user. Stop reading when the user inputs -1 or you reach CAPACITY. // @param: int array[] is an unordered array of integers when leaving this function // @param: int& numberElements is the number of Elements in the array after function // @returns void. void fill_array (int array[], int& numElems); //ToDo: Declare a function that removes (i.e., deletes) the element // removeElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position of element to delete // @returns: true if delete was successful, false otherwise bool removeElement (int array[], int& numElems, int position); //ToDo: Delcare a function that inserts the element in the given position // insertElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position to insert into // @param: int target to insert. // @returns: true if insert was successful, false otherwise bool insertElement (int array[], int& numElems, int position, int target); //ToDo: Declare a funcxtion that searches for an element in the given array // searchElement - searches for the element in the given array. // @param int array[] is an unordered array of integers // @param int numberOfElements // @param int target // @returns index of element or -1 if not found. int searchElement(int array[], int numElems, int target); int main() { // The NumArray can be partially filled, we use variable NumArrayElems to keep track of how many numbers // have been stored in the array. int array[CAPACITY]; // an int array with a given CAPACITY int numElems=0; // the array is initially empty, i.e., contains 0 elements int position, target; int value; // 1. ToDo: Call your fillArray function to read in a sequence of integer values, // separated by space, and ending with -1. Store the values in the NumArray array // and the number of elements in NumArrayElems. // Display the contents of the array afterwards cout << "Enter a list up to 20 integers or -1 to end the list" << endl; fill_array (array, numElems); displayArray(array, numElems); // 2. ToDo: Read in a value and position from the user. Call your insertElement function // to insert the given value into the given position of the array // Display the contents of the array afterwards cout << "Enter a value and a position to insert: "; cin >> target >> position; insertElement (array, numElems, position, target); displayArray(array, numElems); // 3. ToDo: Read in a value and call your searchElement function. // if the value is found, delete it from the array using your function // if the value not found, print "Value not found in array" // Display the contents of the array afterwards cout << "Enter a value to delete from the array: "; cin >> target; position = searchElement(array, numElems, target); if (position != -1) removeElement (array, numElems, position); else cout << "Value not found!"; displayArray(array, numElems); cout << "Enter a value to append: "; cin >> value; insertElement (array, numElems, numElems, value); displayArray(array, numElems); return 0; } void fill_array (int array[], int& numElems) { //cout << "Enter a list up to 20 integers or -1 to end the list" << endl; for (numElems=0; numElems<CAPACITY;numElems++) { cin >> array[numElems]; if (array[numElems] == -1 ) break; } } int searchElement(int array[], int numElems, int target) { int i; for (i = 0; i < numElems; i++) { if(array[i] == target) { return i; } } return -1; } bool insertElement (int array[], int& numElems, int position, int target) { for(int i = numElems-1; i >= position; i--) { array[i+1] = array[i]; } array[position] = target; numElems = numElems + 1; return true; if(numElems + 1 >= CAPACITY) return false; } bool removeElement (int array[], int& numElems, int position) { for(int j=position; j<(numElems-1); j++) { array[j]=array[j+1]; } numElems--; return true; if((position<0) || (position>numElems)) return false; } void displayArray(int array[], int numElems) { for (int i = 0; i < numElems; i++) cout << array[i] << " "; cout << endl; }
YSEUM
функции insertElement и removeElement всегда возвращают true;
C++ выполняет код в основном сверху вниз.
Если функция возвращает значение, не выполняйте оставшийся код ниже.(в функции)