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
  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • /* * 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...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through...

    Write a C++ program that simulates a lottery. The user will select 5 numbers 0 through 9 and put this in an array. Then these user numbers are compared to the random numbers the computer generated. The program will display both the computer random number and below the user selected numbers. The program will tell the user how many matches are made. If the user guesses all five you let them know they are the grand winner. Here is the...

  • #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: "...

  • Am I using the right cin statement to take in values for my array? If I...

    Am I using the right cin statement to take in values for my array? If I print them out it prints "0x61fea0" with the input 1 2 3 4 5 ///////////////////////////////// #include <iostream> using namespace std; const int ARRAY_LENGTH = 5; int main(){ int numbers[ARRAY_LENGTH]; int threshold; //@todo prompt user to enter array values cout << "Please input 5 numbers: " << endl; for(int i = 0; i < ARRAY_LENGTH; i++){ //@todo add cin statement to read in values for...

  • follow the instructions inside. You are to implement the sequential search of array elements. User will...

    follow the instructions inside. You are to implement the sequential search of array elements. User will enter a value (size) which represents the number of values to process The values entered will be stored in an array of type short that has 1000 elements User will enter size numbers The user will enter a search value The program will search the data for a specific value program will display a message in which element the value was found or display...

  • 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...

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