getting an error for the code below when attempting to compile . Not fully sure what the error means and confused why I am getting the error. I know there are a of other logical errors but im just looking for the solution to this specific one right now.
/tmp/ccq2rdty.o: In function `main':
IntArrayPlay.cpp:(.text+0x42): undefined reference to
`fillArray(int*, int&)'
IntArrayPlay.cpp:(.text+0x55): undefined reference to
`displayArray(int*, int&)'
IntArrayPlay.cpp:(.text+0x114): undefined reference to
`displayArray(int*, int&)'
IntArrayPlay.cpp:(.text+0x15f): undefined reference to
`displayArray(int*, int&)'
collect2: error: ld returned 1 exit status
/* Working with arrays and functions
* Author:
* Last modified on:
* Known bug
*/
#include <iostream>
using namespace std;
const int CAPACITY = 20;
// displayArray - display the array on a single line separated
by blanks.
void displayArray(int array[], int& numElems);
// @param: int array[] is an unordered array of integers
// @param: int numElems
// [Already implemented]
//ToDo: Declare a function fillArray that fills an int array
with values entered
void fillArray(int array[], int& numElems);
// 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.
//ToDo: Declare a function that removes (i.e., deletes) the
element
void deleteElement(int array[], int& numElems, int&
target);
// 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
//ToDo: Delcare a function that inserts the element in the given
position
void insertElement(int array[], int& numElems, int& pos,
int& target);
// 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
//ToDo: Declare a function that searches for an element in the
given array
bool searchElement(int array[], int& numElems, int
searchval);
// 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 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
int searchval = 0;
int pos = 0;
int target = 0;
// 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
fillArray(array, numElems);
// and the number of elements in NumArrayElems.
// Display the contents of the array afterwards
displayArray(array, numElems);
// 2. ToDo: Read in a value and position from the user. Call your
insertElement function
cout << "Enter a value and a position to insert: ";
cin >> target >> pos;
// to insert the given value into the given position of the
array
insertElement(array, numElems, pos, target);
// Display the contents of the array afterwards
// 3. ToDo: Read in a value and call your searchElement
function
cout << "Please enter the value you would like to search:
";
cin >> searchval;
//searchElement(array, numElems, searchval);
// if the value is found, delete it from the array using your
function
if (searchElement(array, numElems, searchval) == true)
deleteElement(array, numElems, pos);
else
cout << "Value not found!";
// if the value not found, print "Value not found in array"
// Display the contents of the array afterwards
displayArray(array, numElems);
// 5. TODO: Read in a value and call your insertElement function to
append
cout << "Enter a value to append: ";
cin >> target;
insertElement(array, numElems, pos, target);
// a value to the end of the array
// Display the contents of the array afterwards
displayArray(array, numElems);
return 0;
}
//TODO: Implement all functions declared above.
//Don't forget to put precondition/postcondition comments under or
over the function header.
// displayArray - displays the array
// precondition: int array[] is an unordered array of numElems
integers.
// postcondition: array is displayed on the console on a single
line separated by blanks.
void fillArray(int array[], double& numElems)
{
cout << "Please enter up to 20 non-negative whole numbers
separated by space.\n" << "Mark the end of the input list
with a negative number: \n";
int num;
int index;
cin >> num;
while (num >= 0 && index < CAPACITY)
{
array[index] = num;
index++;
cin >> num;
numElems = index;
}
}
void displayArray(int array[], int numElems)
{
for (int i = 0; i < numElems; i++)
cout << array[i] << " ";
cout << endl;
}
bool searchElement(int array[], int& numElems, int
searchval)
{
bool result = false;
cout << "Enter a value to delete from the array: ";
cin >> searchval;
for(int i = 0; i < numElems; i++)
{
if (array[i] == searchval)
result = true;
}
return result;
}
void insertElement(int array[], int& numElems, int& pos,
int& target)
{
for(int i = pos; i < numElems + 1; i++)
{
array[i+1] = array[i];
array[pos] = target;
}
}
void deleteElement(int array[], int& numElems, int&
target)
{
int index = 0;
for(int i = 0; i < numElems; i++)
{
if(array[i] == target)
{
index = i;
for(int j = index; j < numElems; j++)
array[j] = array[j+1];
}
}
}
If you have any doubts, please give me comment...
/* Working with arrays and functions
* Author:
* Last modified on:
* Known bug
*/
#include <iostream>
using namespace std;
const int CAPACITY = 20;
// displayArray - display the array on a single line separated by blanks.
void displayArray(int array[], int numElems);
// @param: int array[] is an unordered array of integers
// @param: int numElems
// [Already implemented]
//ToDo: Declare a function fillArray that fills an int array with values entered
void fillArray(int array[], int &numElems);
// 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.
//ToDo: Declare a function that removes (i.e., deletes) the element
void deleteElement(int array[], int &numElems, int target);
// 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
//ToDo: Delcare a function that inserts the element in the given position
void insertElement(int array[], int &numElems, int pos, int target);
// 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
//ToDo: Declare a function that searches for an element in the given array
bool searchElement(int array[], int numElems, int searchval);
// 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 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
int searchval = 0;
int pos = 0;
int target = 0;
// 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
fillArray(array, numElems);
// and the number of elements in NumArrayElems.
// Display the contents of the array afterwards
displayArray(array, numElems);
// 2. ToDo: Read in a value and position from the user. Call your insertElement function
cout << "Enter a value and a position to insert: ";
cin >> target >> pos;
// to insert the given value into the given position of the array
insertElement(array, numElems, pos, target);
// Display the contents of the array afterwards
// 3. ToDo: Read in a value and call your searchElement function
cout << "Please enter the value you would like to search and delete: ";
cin >> searchval;
//searchElement(array, numElems, searchval);
// if the value is found, delete it from the array using your function
if (searchElement(array, numElems, searchval) == true)
deleteElement(array, numElems, searchval);
else
cout << "Value not found!";
// if the value not found, print "Value not found in array"
// Display the contents of the array afterwards
displayArray(array, numElems);
// 5. TODO: Read in a value and call your insertElement function to append
cout << "Enter a value to append: ";
cin >> target;
pos = numElems;
insertElement(array, numElems, pos, target);
// a value to the end of the array
// Display the contents of the array afterwards
displayArray(array, numElems);
return 0;
}
//TODO: Implement all functions declared above.
//Don't forget to put precondition/postcondition comments under or over the function header.
// displayArray - displays the array
// precondition: int array[] is an unordered array of numElems integers.
// postcondition: array is displayed on the console on a single line separated by blanks.
void fillArray(int array[], int &numElems)
{
cout << "Please enter up to 20 non-negative whole numbers separated by space.\n"
<< "Mark the end of the input list with a negative number: \n";
int num;
int index;
cin >> num;
while (num >= 0 && index < CAPACITY)
{
array[index] = num;
index++;
cin >> num;
numElems = index;
}
}
void displayArray(int array[], int numElems)
{
for (int i = 0; i < numElems; i++)
cout << array[i] << " ";
cout << endl;
}
bool searchElement(int array[], int numElems, int searchval)
{
bool result = false;
for (int i = 0; i < numElems; i++)
{
if (array[i] == searchval)
result = true;
}
return result;
}
void insertElement(int array[], int &numElems, int pos, int target)
{
for (int i = numElems; i>pos; i--)
{
array[i] = array[i-1];
}
array[pos] = target;
numElems++;
}
void deleteElement(int array[], int &numElems, int target)
{
int index = 0;
for (int i = 0; i < numElems; i++)
{
if (array[i] == target)
{
index = i;
for (int j = index; j < numElems-1; j++)
array[j] = array[j + 1];
numElems--;
}
}
}
Get Answers For Free
Most questions answered within 1 hours.