#include <iostream>
#include <iomanip>
#include <cstdlib>
const int ARRAYSIZE = 40;
// the function prototypes
void printArr (int[], int, int=5);
int readArr (int[], int);
int searchArr (int[], int, int);
int searchArr2 (int[], int, int);
int searchArr3 (int[], int, int, int=0);
main()
{
int size; // contains how many values are actually
// in the array
int arr[ARRAYSIZE];
int i;
int temp;
int pos;
int target;
cout << "Welcome" << endl << endl;
cout << "Fill in the array with values" << endl;
for (i = 0 ; i < ARRAYSIZE; i++)
arr[i] = rand() % 1000;
arr[18] = 10;
arr[31] = 10;
// call a function to print an array
cout << endl << "5 per line" << endl;
printArr(arr, ARRAYSIZE);
// call a function to search for a value in my array
target = 20;
pos = searchArr(arr, ARRAYSIZE, target);
if (pos == -1)
{
cout << "The value of " << target
<< " does not exist in the array." << endl;
}
else
{
cout << "The value of " << target
<< " is at position " << pos << " in the array." << endl;
}
// call a function to search for a value in my array
target = 10;
pos = searchArr(arr, ARRAYSIZE, target);
if (pos == -1)
{
cout << "The value of " << target
<< " does not exist in the array." << endl;
}
else
{
cout << "The value of " << target
<< " is at position " << pos << " in the array." << endl;
}
cout << endl << "Try with searchArr3()" << endl;
// call a function to search for a value in my array
pos = 0;
while (pos != -1)
{
target = 10;
pos = searchArr3 (arr, ARRAYSIZE, target, pos);
if (pos == -1)
{
cout << "The value of " << target
<< " does not exist in the array." << endl;
}
else
{
cout << "The value of " << target
<< " is at position " << pos << " in the array." << endl;
pos++;
}
}
cout << endl << "Goodbye" << endl;
}
// The following function will search for a value in an array
// The function will return -1 if the value is not in the array.
// The function will return the position of the value
// if the value is in the array.
int searchArr (int arr[], int sz, int tar)
{
int i;
for (i = 0; i < sz; i++)
if (tar == arr[i])
return i;
// return -1 because the value was not found
return -1;
}
int searchArr2 (int arr[], int sz, int tar)
{
int i;
int fpos = -1;
for (i = 0; i < sz; i++)
if (tar == arr[i])
fpos = i;
// return the value in fpos
return fpos;
}
int searchArr3 (int arr[], int sz, int tar, int startPos = 0)
{
int i;
for (i = startPos; i < sz; i++)
if (tar == arr[i])
return i;
// return -1 because the value was not found
return -1;
}
// The following function will read in values and store them
// into an array.
//
// Parameter 1: the array
// 2: the maximum size of the array
// Return: the number of values actually stored in the array.
int readArr(int arr[], int max)
{
int i = 0;
int fileSize;
int temp;
// read values into the array (feof)
while (cin >> temp)
{
// verify the number of values read
if (i >= max)
{
cout << "Error: attempted to overfill the array of size "
<< max << endl;
fileSize = i + 1;
while (cin >> temp)
fileSize++;
cout << "File contains " << fileSize << " values." << endl;
return i;
}
arr[i] = temp;
i++;
}
return i;
}
// The following function will print an array with 5 values
// per line as a default. If a third parameter is given, this
// parameter will specify the number of value per line.
//
// Parameter 1: The array
// 2: The size of the array
// 3: number of value per line (5 by default)
// Returns nothing
void printArr(int arr[], int sz, int perLine = 5)
{
int i;
// Display the values in the array
for (i = 0; i < sz; i++)
{
// cout.width(5);
cout << setw (5) << arr[i] << " ";
//cout << arr[i] << " ";
if ((i+1) % perLine == 0)
cout << endl;
}
}