Question

In: Computer Science

Write two versions of a program that reads from the user a sequence of positive integers...

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:
Please enter a sequence of positive integers, each in a separate line.
End you input by typing -1.
13
5
8
2
9
5
8
8
-1
Please enter a number you want to search.
5
5 shows in lines 2, 6.
a) The first version of the program, is not allowed to use the vector data structure.
b) The second version of the program, should use the vector data structure.
Implementation requirements (for both programs):
1. Think how to break down your implementation to functions.
2. Your programs should run in linear time. That is, if there are n numbers in the input
sequence, your program should run in ?(?).
3. Write the two programs in two functions named main1() and main2(). Also have
the main() test these two functions.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <vector>

using namespace std;

#define MAX_SIZE 50

void main1(int arr[], int n);

void main2(vector<int> vec);

int main()

{

    int arr[MAX_SIZE], el, n;

    vector<int> vec;

    n = 0;

    cout << "End you input by typing -1." << endl;

    cin >> el;

    while (el != -1)

    {

        arr[n] = el;

        vec.push_back(el);

        n++;

        cin >> el;

    }

    main1(arr, n);

    main2(vec);

    return 0;

}

void main1(int arr[], int n)

{

    cout << "Executing from main1: " << endl;

    int i, search;

    cout << "Please enter a number you want to search." << endl;

    cin >> search;

    bool isFirst = true;

    for (i = 0; i < n; i++)

    {

        if (arr[i] == search)

        {

            if (isFirst)

            {

                cout << search << " shows in lines " << (i + 1);

                isFirst = false;

            }

            else

                cout << ", " << (i + 1);

        }

    }

    if (isFirst)

        cout << search << " not found" << endl;

    else

        cout << "." << endl;

}

void main2(vector<int> vec)

{

    cout << "Executing from main2: " << endl;

    int i, search;

    cout << "Please enter a number you want to search." << endl;

    cin >> search;

    bool isFirst = true;

    for (i = 0; i < vec.size(); i++)

    {

        if (vec.at(i) == search)

        {

            if (isFirst)

            {

                cout << search << " shows in lines " << (i + 1);

                isFirst = false;

            }

            else

                cout << ", " << (i + 1);

        }

    }

    if (isFirst)

        cout << search << " not found" << endl;

    else

        cout << "." << endl;

}


Related Solutions

Do not use arrays and code a program that reads a sequence of positive integers from...
Do not use arrays and code a program that reads a sequence of positive integers from the keyboard and finds the largest and the smallest of them along with their number of occurrences. The user enters zero to terminate the input. If the user enters a negative number, the program displays an error and continues. Sample 1: Enter numbers: 3 2 6 2 2 6 6 6 5 6 0 Largest Occurrences Smallest Occurrences 6 5 2 3. Do not...
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
1. Design and implement a program that reads a series of integers from the user and...
1. Design and implement a program that reads a series of integers from the user and continually prints their average after every reading. The input stops when the user enters “stop” as the input value. Read each input value as a string, then check if it is “stop” or not. If the string is not “stop”, then, attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException, meaning that the input is not...
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid integer), display an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
You need to write a program that reads in two integers from cin and outputs an...
You need to write a program that reads in two integers from cin and outputs an horribly inefficent calculation of the median value. First count from the first number to the second, but stop one short. Then, count from that number back towards the first, again stopping one short. Continue until you reach a single number. Enter 3 and 9 solution: 3456789 987654 45678 8765 567 76 6
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
1. Write an assembly language program that prompts the user for and reads four integers (x1,...
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which. 2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1...
JAVA PROGRAM Write program that will prompt user generate two random integers with values from 1 to 10 then subtract the second integer from the first integer. Note, if the second integer is greater than the first integer, swap the two integers before making the subtraction.Then prompt user for the answer after the subtraction. If the answer is correct, display “Correct”otherwise display “Wrong”.You will need to do this in a loop FIVE times and keep a count of how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT