Question

Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp...

Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array.

PartTwo.cpp Is posted here:

// This program reads data from a file into an array. Then, it
// asks the user for a number. It then compares the user number to
// each element in the array, displays the array element if it is larger.
// After looking at entire array, it displays the number of elements in the array larger
// than the user number.

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

const int ARRAY_SIZE = 365;        // Array size

// Function Prototype
void numberGreaterFunction(int [], int, bool &);

int main()
{
   int stepsTaken[ARRAY_SIZE];    // Array with 365 elements
   int count = 0;    // Loop counter variable
   int userNumber;           // User's number
   bool found = false;       // Flag that signals number found
   ifstream inputFile;     // Input file stream object
  
   // Open the file.
   inputFile.open("steps.txt");
  
   // Read the numbers from the file into the array.
   while (count < ARRAY_SIZE && inputFile >> stepsTaken[count])
        count++;
  
   // Close the file.
   inputFile.close();

   // Prompt user for a number
   cout << "Please enter an integer between 1000 and 12000: ";
   cin >> userNumber;

   // Validate that the user's number is in the range of 1000 to 12000.
   while (userNumber < 1000 || userNumber > 12000)
   {
       cout << "ERROR: Enter a number between 1000 and 12000: ";
       cin >> userNumber;
   }

   numberGreaterFunction(stepsTaken, userNumber, found);
  
   // Display whether user number is in the array.
   if (found)
       cout << "Your number " << userNumber << " is in the array." << endl;
   else
       cout << "Sorry, your number " << userNumber << " is NOT in the array." << endl;

   return 0;
}

//**************************************************************************************************
//* The numberGreaterFunction searches the array for elements than the user's number. *
//* If an element is greater, it is displayed and the count increased. Count is displayed after all*
//* elements are compared to userNum. If userNum is found in the array, the flag found is *
//* set to true. *
//**************************************************************************************************

void numberGreaterFunction(int array[], int userNum, bool &found)
{
   int numberGreater = 0;       // Counter for the greater numbers
int count = 0;    // Loop counter variable

   // Display the numbers greater than the user number
   cout << "\nThe numbers greater are: " << endl;
   for (count = 0; count < ARRAY_SIZE; count++)
   {
       if (userNum < array[count])
        {
           cout << array[count] << " ";
           numberGreater += 1;
       }
       else if (userNum == array[count])
           found = true;
   }
   cout << endl << endl;

   // Display the count of numbers greater.
   cout << "The numbers greater than your number " << userNum << " is ";
   cout << numberGreater << endl << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

==========================================================================

// This program reads data from a file into a vector.

// Then, it asks the user for a number. It then compares the user number to

// each element in the vector, displays the vector element if it is larger.

// After looking entire vector, it displays the number of elements in the vector larger

// than the user number.

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

// Function Prototype

void numberGreaterFunction(vector<int>, int, bool &);

int main()

{

vector<int> vect; //declaring vector

int userNumber; // User's number

bool found = false; // Flag that signals number found

ifstream inputFile; // Input file stream object

//open the file.

inputFile.open("steps.txt");

//check if file is open or not

if (inputFile) {

int value;

// read the elements in the file into a vector

while ( inputFile >> value ) {

vect.push_back(value);

}

//close the file.

inputFile.close();

}

// Prompt user for a number

cout << "Please enter an integer between 1000 and 12000: ";

cin >> userNumber;

// Validate that the user's number is in the range of 1000 to 12000.

while (userNumber < 1000 || userNumber > 12000)

{

cout << "ERROR: Enter a number between 1000 and 12000: ";

cin >> userNumber;

}

//calling function

numberGreaterFunction(vect, userNumber, found);

// Display whether user number is in the array.

if (found)

cout << "\nYour number " << userNumber << " is in the array." << endl;

else

cout << "\nSorry, your number " << userNumber << " is NOT in the array." << endl;

return 0;

} //end of main method/fumction

//**************************************************************************************

//* The numberGreaterFunction searches the array for elements than the user's number.

//* If an element is greater, it is displayed and the count increased.

//* Count is displayed after all elements are compared to userNum.

//* If userNum is found in the array, the flag found is set to true.

//**************************************************************************************

void numberGreaterFunction(vector<int> vect, int userNum, bool &found)

{

int numberGreater = 0; // Counter for the greater numbers

// Display the numbers greater than the user number

cout << "\nThe numbers greater are: " << endl;

for (int num : vect)

{

if (userNum < num)

{

cout << num << " ";

numberGreater += 1;

}

else if (userNum == num)

found = true;

}

cout << endl << endl;

// Display the count of numbers greater.

cout << "There are " << numberGreater << " number(s) greater than your number " << userNum << endl;

} //end of numberGreaterFunction method/function

1 2 3 4 5 6 7 // This program reads data from a file into a vector. // Then, it asks the user for a number. It then compares

//close the file. inputFile.close(); } 32 33 34 35 36 37 38 39 40 41 1/ Prompt user for a number cout << Please enter an int

*** 60 61 62 63 64 //***** 1/* The numberGreaterFunction searches the array for elements than the users number. 1/* If an el

============================================================================

Output:

Please enter an integer between 1000 and 12000: 5514 The numbers greater are: 58962 There are 1 number (3) greater than your

==============================================================================

Hope it will be usefull and do comments for any extra info if needed. Thankyou

==============================================================================

Add a comment
Know the answer?
Add Answer to:
Modify PartTwo.cpp so that it is uses a vector, named vect, instead of an array. PartTwo.cpp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* * Program5 for Arrays * * This program illustrates how to use a sequential search...

    /* * Program5 for Arrays * * This program illustrates how to use a sequential search to * find the position of the first apparance of a number in an array * * TODO#6: change the name to your name and date to the current date * * Created by Li Ma, April 17 2019 */ #include <iostream> using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() {...

  • * This program illustrates how to use a sequential search to find the position of the...

    * This program illustrates how to use a sequential search to find the position of the first apparance of a number in an array TODO#6: change the name to your name and date to the current date * * Created by John Doe, April 17 2019 */ #include using namespace std; //global constant const int ARRAY_SIZE = 10; //TODO#5: provide the function prototype for the function sequentialSearch int main() { //TODO#1: declare an integer array named intList with size of...

  • #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code...

    #include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • Question 1) Suppose a program has the following code: const int X = 2, Y =...

    Question 1) Suppose a program has the following code: const int X = 2, Y = 3; int sum; int values[X][Y] = {{1, 2, 3},                                  {4, 5, 6}}; for(int i=0; i<X; i++) {      sum=0;      for(int j=0; j<Y; j++)         sum+=values[i][j];    cout<<sum<<endl; } What is this program getting the sum of? Group of answer choices D-) The columns of the 2D array C-) The rows of the 2D array A-) All of the elements of the 2D...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT