Question

In: Computer Science

Can somebody explain me what this code does in a few or one sentence? #include <iostream>...

Can somebody explain me what this code does in a few or one sentence?

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

int main () {
   const int NUM_ELEMENTS = 8;
   vector<int> numbers(NUM_ELEMENTS);
   int i = 0;
   int tmpValue = 0;

   cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;
   for (i = 0; i < NUM_ELEMENTS; ++i) {
       cout << "Enter Value#" << i+1 << ": ";
       cin >> numbers.at(i);
   }
   for (i = 0; i < (NUM_ELEMENTS /2); ++i) {
       tmpValue = numbers.at(i);
       numbers.at(i) = numbers.at(NUM_ELEMENTS - 1 - i);
       numbers.at(NUM_ELEMENTS - 1 - i) = tmpValue;
   }
   system ("pause");
   return 0;
}

Solutions

Expert Solution

Thanks for the question, the program asks the user to enter 8 numbers and store them in the vector, then it reverses the number from left to right. So the 1st element is swapped with the 8th element, 2nd element position is swapped with the 7th element, 3rd with 6th, 4th with 5th

Here are the comments and the output screenshot to illustrate, I added few codes to print the before and after-effects.

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

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

int main () {
const int NUM_ELEMENTS = 8;
vector<int> numbers(NUM_ELEMENTS);
int i = 0;
int tmpValue = 0;

cout << "Enter " << NUM_ELEMENTS << " integer values..." << endl;
for (i = 0; i < NUM_ELEMENTS; ++i) {
cout << "Enter Value#" << i+1 << ": ";
cin >> numbers.at(i);
}

for (i = 0; i < NUM_ELEMENTS; ++i) {
cout << numbers.at(i)<< " ";
  
}
cout<<endl;
// the function reverses the number in the vector
// iterate till the middle element
for (i = 0; i < (NUM_ELEMENTS /2); ++i) {
        // get the first number
tmpValue = numbers.at(i);
// get the last number and iterchange them
numbers.at(i) = numbers.at(NUM_ELEMENTS - 1 - i);
numbers.at(NUM_ELEMENTS - 1 - i) = tmpValue;
    }
   for (i = 0; i < NUM_ELEMENTS; ++i) {
cout << numbers.at(i)<< " ";
}
cout<<endl;
system ("pause");
return 0;
}

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


Related Solutions

Can somebody explain to me the miscibility of isopropanol alcohol and ethanol alcohol in a NaCl...
Can somebody explain to me the miscibility of isopropanol alcohol and ethanol alcohol in a NaCl solution and why higher levels of concentration of salt solution make the alcohol less miscible (homogeneous)?
Guys can somebody please explain to me what is taxonomy? Like I am completely lost in...
Guys can somebody please explain to me what is taxonomy? Like I am completely lost in what all of it means, I don’t even understand when people talk about Metazoan animals and etc... Like every word is confusing and basically I am a mess, when It comes to this topic... Can somebody please help clear this up in a very clear and descriptive way? Thank you!!!!!
Can somebody describe what this following code does step by step. import turtle turtle.penup() x =...
Can somebody describe what this following code does step by step. import turtle turtle.penup() x = int(input("Enter x coordinate of top-left corner: ")) y = int(input("Enter y coordinate of top-left corner: ")) side = int(input("Enter side of the largest square: ")) if side >= 100: gap = int(input("Enter gap between squares: ")) if gap < side: turtle.goto(x,y) turtle.pendown() while True: for i in range(4): turtle.forward(side) turtle.right(90) side -=gap turtle.penup() x += gap y -= gap turtle.goto(x,y) turtle.pendown() if side<0: exit(1)
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Hey guys, can somebody explain to me how get this The value of a corporate bond...
Hey guys, can somebody explain to me how get this The value of a corporate bond can be derivded by calculating the present value of the interest payment and the present value of the face value at the bond's 1- Current Yield 2- Coupon Rate 3- Required rate of return 4- Effective rate can you kindly elaborate on the answer. thanks
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function...
Can someone covert the code into C language #include<iostream> #include<iomanip> #include<ios> using namespace std; /******************************************************************************** Function name: main Purpose:                   main function In parameters: b,r,i Out paramters: trun,error,total,value Version:                   1.0 Author: ********************************************************************************/ void main() {    int i;//declaring this variable to get value for quitting or calaculating series    do {//do while loop to calaculate series until user quits        cout << "Enter 1 to evaluate the series." << endl;       ...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Need a description what does this program does and how does it work. #include <iostream> using...
Need a description what does this program does and how does it work. #include <iostream> using namespace std; double function(int num, double* memorize); int main() {        int num=5;        char cont;        double* memorize = new double[num + 1];        do {               cout << "Enter n ";               cin >> num;               memorize[1] = 1;               memorize[2] = 1;               memorize[3] = 1;               memorize[4] = 3;               memorize[5] = 5;               for (int i = 6; i...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Static methods used for testing //============================================================================ /** * Display the bid information...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT