Posted on

This is study resource 9 for the 24-week cohort during the summer of 2019. There are 4 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

Write a program that will read in a line of text and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, and periods. When outputting the number of letters that occur in a line, be sure to count upper and lowercase versions of a letter as the same letter. Output the letters in alphabetical order and list only those letters that do occur in the input line.

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

Please enter a line of text:
I say Hi.
3 words
1 a
1 h
2 i
1 s
1 y 

Notes:

1. Think how to break down your implementation to functions.

2. Pay attention to the running time of your program. If the input line contains 𝑛 characters, an efficient implementation would run in a linear time (that is Θ(𝑛)).

#include <iostream>
#include <string>

using namespace std;

void analyzeSentence(string enteredText);


int main() {

    string enteredSentence;

    cout << "Please enter a line of text: " << endl;
    getline(cin,enteredSentence);

    analyzeSentence(enteredSentence);

    return 0;
}

void analyzeSentence(string enteredText){

    int wordCounter = 1;
    int letters[26] = {0};
    int c;

    for(int i=0; i < enteredText.length(); i++){

        if(enteredText[i] == ' '){
            wordCounter++;
        }

        if(tolower(enteredText[i]) >= 'a' && tolower(enteredText[i]) <= 'z' && enteredText[i] != ' '){
            c = tolower(enteredText[i]) - 'a';
            letters[c]++;
        }
    }

    cout << wordCounter << "\t" << "word(s)"<<endl;

    for(int i=0 ; i < 26; i++){
        if(letters[i] > 0){
            cout << letters[i] << "\t" << char(i+'a') << endl;
        }
    }

}
Question #2

Question #2

Two strings are anagrams if the letters can be rearranged to form each other. For example, “Eleven plus two” is an anagram of “Twelve plus one”. Each string contains one ‘v’, three ‘e’s, two ‘l’s, etc.

Write a program that determines if two strings are anagrams. The program should not be case sensitive and should disregard any punctuation or spaces.

Notes:

1. Think how to break down your implementation to functions.

2. Pay attention to the running time of your program. If each input string contains 𝑛 characters, an efficient implementation would run in a linear time (that is Θ(𝑛)).

#include <iostream>
#include <string>

using namespace std;

const int MAX_SIZE_ARR = 52; //assuming strings contain english alphabet only

bool areSentencesAnagram(char* sentence1, char* sentence2);

int main() {

    string firstSentenceEntered;
    string secondSentenceEntered;

    cout << "Find out if two sentences are anagrams." << endl;
    cout << "Enter the first sentence: ";
    getline(cin, firstSentenceEntered);
    cout << "Enter the second sentence: ";
    getline(cin, secondSentenceEntered);


    char * string1 = new char[firstSentenceEntered.length()];
    char * string2 = new char[secondSentenceEntered.length()];

    for(int i=0; i <= firstSentenceEntered.length(); i++){
        string1[i] = firstSentenceEntered[i];
    }

    for(int i=0; i <= secondSentenceEntered.length(); i++){
        string2[i] = secondSentenceEntered[i];
    }

    if(areSentencesAnagram(string1,string2)){
        cout << "The two strings are anagrams.";
    }else{
        cout << "The two strings are NOT anagrams.";
    }

    delete [] string1;
    delete [] string2;

    return 0;
}

bool areSentencesAnagram(char* sentence1, char* sentence2){

    int count[MAX_SIZE_ARR] = {0};
    int i;

    for( i=0; sentence1[i] && sentence2[i]; i++){
        count[sentence1[i]]++;
        count[sentence2[i]]--;
    }

    if(sentence1[i] || sentence2[i]){
        return false;
    }

    for(i=0; i < MAX_SIZE_ARR; i++){
        if(count[i])
            return false;
    }

    return true;
}
Question #3

Question #3

In this question, you will write four versions of a function getPosNums that gets an array of integers arr, and its logical size. When called it creates a new array containing only the positive numbers from arr.

For example, if arr=[3, -1, -3, 0, 6, 4], the functions should create an array containing the following 3 elements: [3, 6, 4], The four versions you should implement differ in the way the output is returned.

The prototypes of the functions are:

a) int* getPosNums1(int* arr, int arrSize, int& outPosArrSize)

returns the base address of the array (containing the positive numbers), and updates the output parameter outPosArrSize with the array’s logical size.

b) void getPosNums2(int* arr, int arrSize,

int*& outPosArr, int& outPosArrSize)

updates the output parameter outPosArr with the base address of the array (containing the positive numbers), and the output parameter outPosArrSize with the array’s logical size.

c) int* getPosNums3(int* arr, int arrSize, int* outPosArrSizePtr)

returns the base address of the array (containing the positive numbers), and uses the pointer outPosArrSizePtr to update the array’s logical size.

d) void getPosNums4(int* arr, int arrSize,

int** outPosArrPtr, int* outPosArrSizePtr)

uses the pointer outPosArrPtr to update the base address of the array (containing the positive numbers), and the pointer outPosArrSizePtr to update the array’s logical size.

Note: You should also write a main program that calls and tests all these functions.

#include <iostream>

using namespace std;

int* getPosNums1(int* arr, int arrSize, int& outPosArrSize);
void getPosNums2(int* arr, int arrSize, int*& outPosArr, int& outPosArrSize);
int* getPosNums3(int* arr, int arrSize, int* outPosArrSizePtr);
void getPosNums4(int* arr, int arrSize, int** outPosArrPtr, int* outPosArrSizePtr);
void printArray(int * arr, int arraySize);

int main() {

    int size = 7;
    int arr[] = {3,-1,-3,-4,10,9,4};
    int posNum1NewSize;

    int* posNum1;
    posNum1 = getPosNums1(arr, size, posNum1NewSize);

    cout << "getPosNums1 new arr address:  " << posNum1 << " - New Size: " << posNum1NewSize << endl;
    printArray(posNum1, posNum1NewSize);

    int* outPosArr;
    int posNum2NewSize;

    getPosNums2(arr, size, outPosArr, posNum2NewSize);

    cout << "getPosNums2 new array address: " << outPosArr << endl;
    cout << "getPosNums2 function new array size: " << posNum2NewSize << endl;

    printArray(outPosArr, posNum2NewSize);


    int* outPosArrSizePtr = &posNum2NewSize;
    int* posNum3;

    posNum3 = getPosNums3(arr, size, outPosArrSizePtr);
    cout << "getPosNums3 new array address: " << posNum3 << endl;
    cout << "getPosNums3 function new array size: " << *outPosArrSizePtr << endl;

    printArray(posNum3, *outPosArrSizePtr);


    getPosNums4(arr, size, &posNum3, outPosArrSizePtr);
    cout << "getPosNums4 new array address: " << posNum3 << endl;
    cout << "getPosNums4 function new array size: " << *outPosArrSizePtr << endl;

    printArray(posNum3, *outPosArrSizePtr);


    delete [] posNum1;
    delete [] outPosArr;
    delete [] posNum3;

    return 0;
}

int* getPosNums1(int* arr, int arrSize, int& outPosArrSize){

    int* newPosArr = new int [arrSize];
    int counter = 0;

    for(int i=0; i < arrSize; i++){
        if(arr[i] > 0){
            newPosArr[counter] = arr[i];
            counter++;
        }
    }

    outPosArrSize = counter;

    return newPosArr;
}

void getPosNums2 (int* arr, int arrSize, int*& outPosArr, int& outPosArrSize){

    outPosArr = getPosNums1(arr, arrSize,outPosArrSize);

}


int* getPosNums3(int* arr, int arrSize, int* outPosArrSizePtr){

    int* newArr = new int[(* outPosArrSizePtr)];
    int newArrPos = 0;

    for(int i=0; i < arrSize; i++){
        if(arr[i] > 0){
            newArr[newArrPos] = arr[i];
            newArrPos++;
        }
    }

    return newArr;
}

void getPosNums4(int* arr, int arrSize, int** outPosArrPtr, int* outPosArrSizePtr){
    *outPosArrPtr = new int[*outPosArrSizePtr];
    int newArrPos = 0;
    for(int i=0; i < arrSize; i++){
        if(arr[i] > 0){
            (*outPosArrPtr)[newArrPos] = arr[i];
            newArrPos++;
        }
    }
}

void printArray(int * arr, int arraySize){
    cout <<"Positive Values: ";
    for(int i=0; i < arraySize; i++){
        cout << arr[i] << " ";
    }
    cout << endl<<endl;
}
Question #4

Question #4

Implement the function:

void oddsKeepEvensFlip(int arr[], int arrSize)

This function gets an array of integers arr and its logical size arrSize.

When called, it should reorder (in-place) the elements of arr so that:

1. All the odd numbers come before all the even numbers

2. The odd numbers will keep their original relative order

3. The even numbers will be placed in a reversed order (relative to their original order).

For example, if arr = [5, 2, 11, 7, 6, 4], after calling oddsKeepEvensFlip(arr, 6), arr will be [5, 11, 7, 4, 6, 2]

Implementation requirements:

1. Your function should run in linear time. That is, if there are n items in arr, calling oddsKeepEvensFlip(arr, n) will run in 𝜃(𝑛).

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

****** See TA’s feedback at the end of question******

#include <iostream>
#include <vector>

using namespace std;

void oddsKeepEvensFlip(int arr[], int arrSize);

int main() {


    int arraySizeEntered;


    cout << "Enter the size of the array: " << endl;
    cin >> arraySizeEntered;

    int numArray [arraySizeEntered];

    cout << "Enter the numbers into the array: "<< endl;


    for(int i=0; i < arraySizeEntered; i++){
        cin >> numArray[i];
    }


    oddsKeepEvensFlip(numArray, arraySizeEntered);

    return 0;
}

void oddsKeepEvensFlip(int arr[], int arrSize){

    vector<int> evenNumbers;
    vector<int> oddNumbers;
    vector<int> result;

    for(int i = 0; i < arrSize; i++){

        if(arr[i] % 2 == 0){
            evenNumbers.push_back(arr[i]);
        }else{
            oddNumbers.push_back(arr[i]);
        }

    }

    vector<int> newEvenNumbers;

    for(int i=evenNumbers.size()-1; i >= 0; i--){
        newEvenNumbers.push_back(evenNumbers[i]);
    }

    result.reserve(oddNumbers.size()+newEvenNumbers.size());
    result.insert(result.end(), oddNumbers.begin(), oddNumbers.end());
    result.insert(result.end(), newEvenNumbers.begin(), newEvenNumbers.end());

    for(int i=0; i < result.size(); i++){
        cout << result[i] << " ";
    }


}

TA’s feedback

Photo by True Agency on Unsplash

Leave a Reply

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