1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
#include <iostream>
#include <vector>
using namespace std;
void readInput(int*& dyArr, int& logicalSize); //function that reads positive integers as input from the user and
//prints all the line numbers in the sequence entered by the user that contain a number num selected by the user
int* expandArr(int* dyArr, int& physicalSize); //function that expands the physical size of a dynamic array
void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize); //function that searches an array for a number selected by the user
void printArray(int* dyArr, int arrSize); //function to print an array given its' size and elements
int main_arr();
int main_vector();
int main()
{
main_arr();
return 0;
}
int main_arr()
{
int* dyArr = NULL;
int logicalSize = 0;
int num;
int* searchNumArr = nullptr;
int searchNumArrSize = 0;
cout << "Please enter a sequence of positive integers, each in a separate line." << endl;
cout << "End your input by typing -1." << endl;
readInput(dyArr, logicalSize);
cout << "Please enter a number you want to search." << endl;
cin >> num;
searchNum(dyArr, logicalSize, num, searchNumArr, searchNumArrSize); //need to make sure I call this function properly
if (searchNumArrSize == 0) {
cout << "The number " << num << " does not show at all in the sequence.";
} else {
cout << num << " shows in lines ";
printArray(searchNumArr, searchNumArrSize);
}
delete [] searchNumArr;
delete [] dyArr;
// searchNumArr = nullptr;
return 0;
}
int main_vector() {
// vector<int> numberVector(int &len) ----numberVector does NOT need the length input;
//vector<int> .... whatever functions I decide to use for this case;
return 0;
}
void readInput(int*& dyArr, int& logicalSize) {
int physicalSize = 1;
dyArr = new int[physicalSize];
int posInt;
// int counter; //added two lines here
// counter = 0;
bool seenEndOfInput = false;
while (seenEndOfInput == false) {
cin >> posInt;
if (posInt == -1) {
seenEndOfInput = true;
} else {
if (logicalSize == physicalSize) {
dyArr = expandArr(dyArr, physicalSize);
}
dyArr[logicalSize++] = posInt;
// logicalSize++;
// counter++; //added this line
}
}
// logicalSize = counter;
}
int* expandArr(int* dyArr, int& physicalSize) {
int* tempArr = NULL;
tempArr = new int[physicalSize * 2];
for (int i = 0; i < physicalSize; i++) {
tempArr[i] = dyArr[i];
}
delete [] dyArr; // bye bye old array
// dyArr = tempArr; //to give dyArr the address of tempArr
// delete[] tempArr; //changed this from delete [] dyArr to delete [] tempArr
// tempArr = nullptr;
physicalSize = physicalSize * 2;
return tempArr; // hello new array
}
void searchNum(int* dyArr, int logicalSize, int num, int*& searchNumArr, int& searchNumArrSize) {
int index, i;
index = 0;
searchNumArr = new int[logicalSize]; //changed this from logicalSize
for (i = 0; i < logicalSize; i++) {
if (dyArr[i] == num) {
searchNumArr[index] = i + 1;
index++;
}
}
searchNumArrSize = index;
}
//function to print the results
void printArray(int* dyArr, int arrSize) {
int i;
for (i = 0; i < arrSize; i++) { //i < end of the array
if (i == arrSize - 1) {
cout << dyArr[i] << ".";
} else {
cout << dyArr[i] << ", ";
}
}
}
|