Question

In: Computer Science

Complete the program used on the instructions given in the comments: C++ lang #include <string> #include...

Complete the program used on the instructions given in the comments:

C++ lang

#include <string>

#include <vector>

#include <iostream>

#include <fstream>

using namespace std;

vector<float>GetTheVector();

void main()

{

vector<int> V;

V = GetTheVector(); //reads some lost numbers from the file “data.txt" and places it in //the Vector V

Vector<int>W = getAverageOfEveryTwo(V);

int printTheNumberOfValues(W) //This should print the number of divisible values by 7 //but not divisible by 3.

PrintIntoFile(W); //This prints the values of vector W into an output file “output.txt”

}

As you see in the main program, there are four functions. The first function is called “GetVector()” that reads a set of integer, place them into the vector V, and returns the vector to the main program.

The second function is called “GetAverageOfEveryTwoNumber() that takes a vector “V” and finds the average of every two consecutive numbers, place the values into another vector called “W” and returns it to the main program. For example if the Vector is:

10 20 30 40 50 60 70 80 90 100

Then vector “W” should be:

15 25 35 45 55 65 75 85 95

Because (10+20) divided by 2 is 15, and so on.

The next function takes the vector “W” and count how many of the values are divisible by “7” but not divisible by “3”

Finally the last function prints the vector “W” into an output file called “output.txt”

Solutions

Expert Solution

Code

#include <string>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

ifstream fp;
ofstream ofp;

vector<int> GetTheVector()
{
   string line;
   vector<int> V(0);
   // Reads the vector from data.txt file
   while (getline(fp, line, ' '))
   {
       V.push_back(stoi(line));       // Converting string number into number
   }
   return V;
}

vector<int> getAverageOfEveryTwo(vector<int> V)
{
   vector<int> W(0);
   vector<int>::iterator it;
  
   for (it = V.begin(); it+1 != V.end(); it++)       // Calculates average of each pair
   {
       int temp = (*it + *(it + 1)) / 2;
       W.push_back(temp);
   }

   return W;
}

int printTheNumberOfValues(vector<int> W)
{
   int count = 0;
   vector<int>::iterator it;

   for (it = W.begin(); it + 1 != W.end(); it++)       // Calculates count of numbers which are divisible by 7 but not 3
   {
       if ((*it % 7 == 0) && (*it % 3 != 0))
           count++;
   }
   return count;
}

void PrintIntoFile(vector<int> W)
{
   ofp.open("output.txt");

   if (ofp.is_open())
   {
       vector<int>::iterator it = W.begin();
       while (it != W.end())
       {
           ofp<< to_string(*it) << " ";
           it++;
       }
   }
   else
   {
       cout << "Could not open the output file. Error!" << endl;
   }
   ofp.close();
}

int main()
{
   vector<int> V, W;
   int count;
   string line;

   fp.open("data.txt");

   if (fp.is_open())
   {
       V = GetTheVector();                   //reads some lost numbers from the file “data.txt" and places it in //the Vector V
       W = getAverageOfEveryTwo(V);
       count = printTheNumberOfValues(W);   //This should print the number of divisible values by 7 //but not divisible by 3.'
       printf("Count of values divisible by 7 but not 3: %d", count);

       PrintIntoFile(W);                   //This prints the values of vector W into an output file “output.txt”
   }
   else
   {
       cout << "Could not open the file. Error!" << endl;
   }
  
   return 0;
}

Test Output


Related Solutions

I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
/* Complete this javascript file according to the individual instructions given in the comments. */ //...
/* Complete this javascript file according to the individual instructions given in the comments. */ // 1) Declare an array named myArray // Assign myArray three elements: 'Tuesday',3,true // Print myArray to the console with console.log() // 2) Use typeof on each element in myArray // Example: typeof myArray[0]; // Print all 3 lines to the console with console.log() // 3) Declare a variable named myStart and set it to // the value of the length property applied of myArray...
Please complete the following code in C using the comments as instructions. Further instructions are below...
Please complete the following code in C using the comments as instructions. Further instructions are below the code. challenge.c // goal: print the environment variables to the file "env.txt", one per line // (If envp is NULL, the file should be empty, opening in write mode will do that.) // example: // inputs: // envp/environ = {"E1=2","E2=7",NULL} // outputs: // env.txt as a string would be "E1=2\nE2=7\n" // example: // inputs: // envp/environ = {NULL} or NULL // outputs: //...
/* Assignment : Complete this javascript file according to the individual instructions given in the comments....
/* Assignment : Complete this javascript file according to the individual instructions given in the comments. */ // 1) Utilize comments to prevent the following line from executing alert('Danger!'); // 2) Assign a value of 5 to a variable named x // and print the value of x to the console // 3) Assign a value of 10 to a variable named myNum // and print the value of myNum to the console // 4) Assign the product of x...
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1)...
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1) Declare a variable named myName equal to your first name //Firstname is Susan // Construct a basic IF statement that prints the variable to the // console IF the length of myName is greater than 1 // 2) Copy your IF statement from above and paste it below // Change the IF statement to check if the length of myName // is greater than...
/* complete javascript file according to the individual instructions given in the comments. */ // 1)...
/* complete javascript file according to the individual instructions given in the comments. */ // 1) Prompt the user to enter their first name. // Display an alert with a personal greeting for the user // with their first name (example: Hello, Dave!) // 2) Create a Try code block that will cause a ReferenceError. // Create a Catch code block that will display the error in the // console with console.error() // 3) Prompt the user to enter a...
/* Complete this javascript file according to the individual instructions given in the comments. */ //1)...
/* Complete this javascript file according to the individual instructions given in the comments. */ //1) Create an Object using an Object Literal (the easiest way) // Name your object Breakfast // Your object should have 3 key:value pairs // The three keys should be: breakfast, lunch, dinner // Put your food choice for each as the value // 2) Display the keys of your Breakfast object in the // console. We have learned two ways to do this.. either...
Complete this javascript question according to the instructions given in the comments. *** DO NOT CHANGE...
Complete this javascript question according to the instructions given in the comments. *** DO NOT CHANGE any of the code that you are not instructed to. */ //////////////////// // Hint: If you see a console.log() in my examples, it is // likely a "return" goes there in your assignment. /////////////////// // 1) Define a "Vehicle" class that has a "wheels" property equal to 4 in // the constructor and a method named "rolling" that returns a string // equal to...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT