I am getting 2 errors when I run my c++ program. The first from IAP Test 2 which seems to be missing zeros on the first and the second line and a zero in the middle for the 5th line. The other error is from test 4 which seems to be missing a -1 on line 5 at the end. How can I fix these 2 errors?
I am getting 2 errors when I run my c++ program. The first from IAP Test 2 which seems to be missing zeros on the first and the second line and a zero in the middle for the 5th line. The other error is from test 4 which seems to be missing a -1 on line 5 at the end. How can I fix these 2 errors?
My code
#include <iostream>
using namespace std;
//defining constant integer
const int CAPACITY=20;
//function prototype declaration
void displayArray(int array[], int numElements);
void fillArray(int array[],int& numElements);
bool removeElement(int array[],int& numberElements,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.
bool insertElement(int array[],int& numberElements,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 element
// @returns index of element or -1 if not found.
int searchElement(int array[],int numberOfElements,int element);
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 NumArray[CAPACITY]; // an int array with a given CAPACITY
int NumArrayElems=0; // the array is initially empty, i.e., contains 0 elements
int position,target;
int search;
int element;
// 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 of up to 20 integers or -1 to end the list"<<endl;
fillArray(NumArray,NumArrayElems);
displayArray(NumArray,NumArrayElems);
// 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 value and position to insert: ";
cin>>target>>position;
insertElement( NumArray, NumArrayElems, position , target);
displayArray(NumArray,NumArrayElems);
// 3. ToDo: Read in a value and call your searchElement function.
// 4. if the value is found, delete it from the array using your deleteElement function
// if the value not found, print "Value not found!"
// Display the contents of the array afterwards
cout<<"Enter value to delete from the array: ";
cin>>search;
int index = searchElement( NumArray, NumArrayElems, search);
if(index != -1){
removeElement( NumArray, NumArrayElems,index);
displayArray(NumArray,NumArrayElems);//modified display call
}
else{
cout<<"Value not found!"<<endl;
}
// 5. TODO: Read in a value and call your insertElement function to append
// a value to the end of the array
// Display the contents of the array afterwards
cout<<"Enter value to append: ";
cin>>element;
insertElement(NumArray,NumArrayElems,NumArrayElems,element);
displayArray(NumArray,NumArrayElems);
return 0;
}
//TODO: Implement all functions declared above.
//Don't forget to put precondition/postcondition comments under or over the function header.
//fillArray: it is used to fill the elements in the array
//precondition: array[] and numelements
//postcondition: inserts elements into the array
void fillArray(int array[],int& numElements){
int ele;
do{
cin>>ele;
if(ele==-1){
break;
}
array[numElements++]=ele;
}while(numElements<CAPACITY);
}
//insertElement - insert elements into the array at a particular postion
// precondition: int array[] is an unordered array of numElems integers postiton and target .
// postcondition: it return true if the element is inserted else false
bool insertElement(int array[],int& numberElements,int position,int target){
if(position<0 or position>20){
return false;
}
else{
int i;
numberElements++;
for (i = numberElements; i >= position; i--)
array[i] = array[i - 1];
array[position ] = target;
}
return true;
}
bool removeElement(int array[],int& numberElements,int position){
for(int i=position;i<numberElements;i++){
array[i]=array[i+1];
}
numberElements--;
return true;
}
//searchElement: serches an element in the array.
//precondition: array[] and numberofElements and element
//postcondition: return the index of the search element
int searchElement(int array[],int numberOfElements,int element){
for(int i=0;i<numberOfElements;i++){
if(array[i]==element){
return i;
}
}
return -1;
}
// postcondition: array is displayed on the console on a single line separated by blanks.
void displayArray(int array[], int numElem)
{
for (int i = 0; i < numElem; i++)
cout << array[i] << " ";
cout << endl;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps