Question

#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

#include <iostream>

#include <vector>

#include <iomanip>

using namespace std;

int main() {

const int NUM_ITEMS = 8;

vector <double> inverse(NUM_ITEMS);

int j;

double temp;

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

inverse.at(i) = 1 / (i + 1.0);

}

cout << fixed << setprecision(2);

cout << "Original vector..." << endl;

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

cout << inverse.at(i) << " ";

}

cout << endl;

cout << "Reversed vector..." << endl;

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

cout << inverse.at(i) << " ";

}

return 0;

}

Original vector...

1.00 0.50 0.33 0.25 0.20 0.17 0.14 0.13

Reversed vector...

0.13 0.14 0.17 0.20 0.25 0.33 0.50 1.00

Using the code below write code to reverse the order of the vector. In other words, the value stored at index 0 will be swapped with the last item in the vector, the value stored at index 1 will be swapped with the next to last item in the vector, etc.  

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

int main() {
    const int NUM_ITEMS = 8;
    vector<double> inverse(NUM_ITEMS);
    int j;
    double temp;
    for (int i = 0; i < NUM_ITEMS; i++) {
        inverse.at(i) = 1 / (i + 1.0);
    }
    cout << fixed << setprecision(2);
    cout << "Original vector..." << endl;
    for (int i = 0; i < NUM_ITEMS; i++) {
        cout << inverse.at(i) << " ";
    }
    cout << endl;

    for (int i = 0; i < NUM_ITEMS / 2; ++i) {
        temp = inverse.at(i);
        inverse.at(i) = inverse.at(NUM_ITEMS - i - 1);
        inverse.at(NUM_ITEMS - i - 1) = temp;
    }

    cout << "Reversed vector..." << endl;
    for (int i = 0; i < NUM_ITEMS; i++) {
        cout << inverse.at(i) << " ";
    }
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...
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
  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

  • #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index...

    #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index = 5;    int head = 0;    string s[index];    int flag = 1;    int choice;    while (flag)    {        cout << "\n1. Add an Item in the Chores List.";        cout << "\n2. How many Chores are in the list.";        cout << "\n3. Show the list of Chores.";        cout << "\n4. Delete an...

  • Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() {...

    Use this code to create multiple functions. #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { cout << fixed << showpoint << setprecision(2); ofstream outFile; outFile.open("Feras's.txt"); outFile << "..Skinny Feras's Restaurant ..\n\n" << endl; int choise=10, quantity; float paid, SubTotal=0, Tax = .10, Total, RM, more; while(choise!=0) { system("cls"); cout << "\t**Welcome To Skinny Alsaif Restaurant Lol**" << endl; cout << "\nWhat would you like to have?" << endl; cout << "1. Burger." << endl; cout << "2. Pizza." <<...

  • im not sure why my code isnt working? include <iostream> #include <iomanip> using namespace std; int...

    im not sure why my code isnt working? include <iostream> #include <iomanip> using namespace std; int main() { int amountOfCoffee; double Price; char salesTaxChargeability; double TotalAmount; const double SALESTAX = 0.035; // 3.5 % cout << "\nEnter the number of pounds of coffee ordered in Pounds :"; cin >> amountOfCoffee; cout << "\nEnter the price of coffee per Pound :"; cin >> Price; cout << "\nIs sales tax Chargeable (y or n): "; cin >> salesTaxChargeability; if ( (salesTaxChargeability ==...

  • #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...

    #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const...

    FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const double HOURLY_RATE = 15;     string input_str;     cout << "Enter days of attendance: " << endl;    *BLANK* (cin, input_str);     stringstream input_stream();     int total_hours = 0;     while(!input_stream.())*BLANK*     {         int hours;         string day;         input_stream >> day;         if(day ==  *BLANK*|| day == *BLANK*)         {             hours = 5;         }         *BLANK*(day == "Tuesday" || day == "Thursday")         {             hours = 4;         }         else if(day ==...

  • C++ question #include <iostream> using namespace std; void printReverse(int); int main() {     int number = 12345;...

    C++ question #include <iostream> using namespace std; void printReverse(int); int main() {     int number = 12345;     cout << "Original : " << number << endl;     cout << "Reversed : ";     printReverse(number); } void printReverse(int x) {     if (x == 0)        return;     cout << x % 10;     printReverse(x /= 10);     } Modify the above recursive program to output the number in the same order. Note that the program still should break up the number and then output it in 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...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

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