Posted on

This is study resource 10 for the 24-week cohort during the summer of 2019. There are 3 questions in it.

Do not just copy and paste the answers, you must understand the content in order to do well in the NYU Bridge To Tandon program. Good luck!.

Click here to see all the study resources.

Question #1

Question #1

Implement the function:

string* createWordsArray(string sentence, int& outWordsArrSize)

This function gets a string sentence containing a sentence. When called, it should create and return a new array (of strings), that contains all the words in sentence. The function should also update the output parameter, outWordsArrSize, with the logical size of the new array that was created.

Note: Assume that the words in the sentence are separated by a single space.

For example, if sentence=”You can do it”, after calling createWordsArray(sentence, outWordsArrSize), the function should create and return an array that contains [”You” , ”can” , ”do” , ”it”], and update the value in outWordsArrSize to be 4.

Implementation requirements:

1. You may want to use some of the string methods, such as find, substr, etc.

2. Your function should run in linear time. That is, if sentence contains n characters, your function should run in 𝜃(𝑛).

3. Write a main() program that tests this function..

/*

Implement the function:
string* createWordsArray(string sentence, int& outWordsArrSize)

This function gets a string sentence containing a sentence.
When called, it should create and return a new array (of strings), that contains all the
words in sentence. The function should also update the output parameter,
outWordsArrSize, with the logical size of the new array that was created.

Note: Assume that the words in the sentence are separated by a single space.
For example, if sentence=”You can do it”, after calling
createWordsArray(sentence, outWordsArrSize), the function should create
and return an array that contains [”You” , ”can” , ”do” , ”it”], and update the
value in outWordsArrSize to be 4.

Implementation requirements:
1. You may want to use some of the string methods, such as find, substr, etc.
2. Your function should run in linear time. That is, if sentence contains n characters,
your function should run in O(n).
3. Write a main() program that tests this function..

*/

#include <iostream>
#include <string>
using namespace std;

string* createWordsArray(string sentence, int& outWordsArrSize);
void printArrElem(string* arr, int arrSize);


int main() {

    string sentenceEntered;
    int outWordsArrSize;
    string* stringArr;

    cout << "Enter a sentence: ";
    getline(cin, sentenceEntered);

    stringArr = createWordsArray(sentenceEntered, outWordsArrSize);

    printArrElem(stringArr, outWordsArrSize);

    delete [] stringArr;
}

string* createWordsArray(string sentence, int& outWordsArrSize){

    string* wordsArray = new string [1];

    int arrPhysicalSize = 1;
    int i = 0;
    string word;

    int wordCounter = 0;
    int sentenceLength = sentence.length();

    while(sentenceLength >= 0){


        if(sentence[i] == ' ' || sentence[i] == '\0'){

            if(wordCounter > 0){
                string* newLinesArr;
                newLinesArr = new string[arrPhysicalSize*2];
                for(int i = 0; i < wordCounter; i++)
                    newLinesArr[i] = wordsArray[i];
                delete []wordsArray;
                wordsArray = newLinesArr;
                arrPhysicalSize *= 2;
            }

            wordsArray[wordCounter] = word;
            wordCounter++;

            word = "";

        }else{
            word += sentence[i];
        }


        i++;
        sentenceLength--;

    }

    outWordsArrSize = wordCounter;
    return wordsArray;

}

void printArrElem(string* arr, int arrSize){
    cout << "New Size: " << arrSize << endl;
    cout << "[";
    for(int i=0; i < arrSize; i++){
        cout << "'" << arr[i] << "'";
        if(i != arrSize - 1){
            cout << ",";
        }
    }
    cout << "]";
}
Question #2

Question #2

Implement the function:

int* findMissing(int arr[], int n, int& resArrSize)

This function gets an array of integers arr and its logical size n. All elements in arr are in the range {0, 1, 2, … , n}. Note that since the array contains n numbers taken from a range of size n+1, there must be at least one number that is missing (could be more than one number missing, if there are duplicate values in arr).

When called, it should create and return a new array, that contains all the numbers in range {0, 1, 2, … , n} that are not in arr. The function should also update the output parameter, resArrSize, with the logical size of the new array that was created.

For example, if arr=[3, 1, 3, 0, 6, 4], after calling

findMissing(arr, 6, resArrSize), the function should create and return an array that contains [2, 5], and update the value in resArrSize to be 2.

Implementation requirements:

1. Your function should run in linear time. That is, it should run in 𝜃(𝑛).

2. Write a main() program that tests this function..

/*

Implement the function:
int* findMissing(int arr[], int n, int& resArrSize)
This function gets an array of integers arr and its logical size n. All elements in arr are
in the range {0, 1, 2, … , n}.

Note that since the array contains n numbers taken from a range of size n+1, there must
be at least one number that is missing (could be more than one number missing, if there
are duplicate values in arr).

When called, it should create and return a new array, that contains all the numbers in
range {0, 1, 2, … , n} that are not in arr. The function should also update the output
parameter, resArrSize, with the logical size of the new array that was created.

For example, if arr=[3, 1, 3, 0, 6, 4], after calling
findMissing(arr, 6, resArrSize), the function should create and return an
array that contains [2, 5], and update the value in resArrSize to be 2.

Implementation requirements:
1. Your function should run in linear time. That is, it should run in O(n).
2. Write a main() program that tests this function..

*/

#include <iostream>

using namespace std;

int* findMissing(int arr[], int n, int& resArrSize);
void printArray(int* arr, int arrSize);

int main() {

  int arrSize = 6;
  int arr[] = {3,1,3,0,6,4};
  int *missingNumArr;
  int newArrSize;

  missingNumArr = findMissing(arr, arrSize ,newArrSize);
  
  cout << "New Array Size: " << newArrSize << endl;
  printArray(missingNumArr, newArrSize);
  
  delete [] missingNumArr;

  return 0;

}

int* findMissing(int arr[], int n, int& resArrSize){

  int *tempArr = new int[n];
  for (int i = 0; i<n; i++){
      tempArr[i] = i;
  }
   
  int count = n;
  for (int i = 0; i<n; i++){
      if (arr[i] < n){
          if (tempArr[arr[i]] != -1){
            tempArr[arr[i]] = -1;
            count--;
          }
      }
  }

  int *missingArr = new int[count];
  int j = 0;

  for (int i = 0; i<n; i++){
      if (tempArr[i] != -1){
          missingArr[j] = tempArr[i];
          j++;
      }
  }

  delete [] tempArr;
   
  resArrSize = count;
  return missingArr;  
}

void printArray(int* arr, int arrSize){
  for (int i=0; i < arrSize; i++){
    cout << arr[i] << " ";  
  } 
  cout << endl;
}
Question #3

Question #3

In this question, you will write two versions of a program that reads from the user a sequence of positive integers ending with -1, and another positive integer num that the user wishes to search for. The program should then print all the line numbers in sequence entered by the user, that contain num, or a message saying that num does not show at all in the sequence.

Your program should interact with the user exactly as it shows in the following example:

#include <iostream>
#include <vector>
using namespace std;

int* createSequenceArr(int& sequenceArrSize);
vector<int> createSequenceVector();
void searchNumber(int* numArr, int numArrSize, int searchNum);
void searchNumberVector(vector<int> numsVector, int searchNum);
int main1();
int main2();


int main() {

  main2();

  return 0;
}

int main1(){

  int numberToSearch;
  
  int sequenceArrSize;
  int* numArr = createSequenceArr(sequenceArrSize);

  cout << "Please enter a number you want to search: " << endl;
  cin >> numberToSearch;

  searchNumber(numArr, sequenceArrSize, numberToSearch);
  
  delete [] numArr;
  return 0;
}

int main2(){

  int numberToSearch;
  
  vector<int> numsVector = createSequenceVector();

  cout << "Please enter a number you want to search: " << endl;
  cin >> numberToSearch;

  searchNumberVector(numsVector, numberToSearch);
  
  return 0;
}

int* createSequenceArr(int& sequenceArrSize){
  int* numArr = new int[1];
  int numArrSize = 0;
  int numArrPhSize = 1;
  
  int currentEnteredNum;

  cout << "Please enter a sequence of positive integers, each in a separate line. End your input by typing -1." << endl;

  while(currentEnteredNum != -1){
    cin >> currentEnteredNum;
    if(numArrSize == numArrPhSize){
      int* newNumArr = new int[2*numArrPhSize];
      for(int i=0; i < numArrSize; i++){
        newNumArr[i] = numArr[i];
      }
      delete [] numArr;
      numArr = newNumArr;
      numArrPhSize *= 2;
    }
    numArr[numArrSize] = currentEnteredNum;
    numArrSize++;
  }

  sequenceArrSize = numArrSize;
  return numArr;
}


vector<int> createSequenceVector(){
  vector<int> numsVector;
  int currentEnteredNum;

  cout << "Please enter a sequence of positive integers, each in a separate line. End your input by typing -1." << endl;

  while(currentEnteredNum != -1){
    cin >> currentEnteredNum;
    numsVector.push_back(currentEnteredNum);
  }

  return numsVector;
}

void searchNumber(int* numArr, int numArrSize, int searchNum){
  cout << searchNum << " shows in line(s) ";
  for(int i = 0; i < numArrSize; i++){
    if(numArr[i] == searchNum){
      if(i == 0){
        cout << i+1;
      }else{
        cout << ", " << i+1;
      }
    }
  }
  cout << ".";
}

void searchNumberVector(vector<int> numsVector, int searchNum){
  int it = 0;
  cout << searchNum << " shows in line(s) ";
  for(int i = 0; i < numsVector.size(); i++){
    if(numsVector[i] == searchNum){
      if(i == 0){
        cout << i+1;
      }else{
        cout << ", " << i+1;
      }
    }
  }
  cout << ".";
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.