Question

In: Computer Science

Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace...

Modify the program to double each number in the vector.

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

int main() {
   const int N=8;
   vector<int> nums(N); // User numbers
   int i=0; // Loop index
   cout << "\nEnter " << N << " numbers...\n";
   for (i = 0; i < N; ++i) {
      cout << i+1 << ": ";
      cin >> nums.at(i);
   }
   // Convert negatives to 0
   for (i = 0; i < N; ++i) {
      if (nums.at(i) < 0) {
         nums.at(i) = 0;
      }
   }
   // Print numbers
   cout << "\nNew numbers: ";
   for (i = 0; i < N; ++i) {
      cout << " " << nums.at(i);
   }
   cout << "\n";

   return 0;
}

Solutions

Expert Solution

Hi. According to the question, program should be modified to double each number in vector . The given program already contains a loop that convers negative numbers to 0.

So, it is unclear whether to keep this loop or remove it.(Since it is asked to modify the code)

However, two versions of the code along with sample output is added here. So, any code can be chosen based on requirement.

In the first version of code, no existing code is removed.Additional code is added to double each number in vector.

In the second version of code, one for loop is removed(converting negative numbers to 0) and additional code is added to double each number in vector.

Code Version-1( no existing code is removed):

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

int main() {
   const int N=8;
   vector<int> nums(N); // User numbers
   int i=0; // Loop index
   cout << "\nEnter " << N << " numbers...\n";
   for (i = 0; i < N; ++i) {
      cout << i+1 << ": ";
      cin >> nums.at(i);
   }
   // Convert negatives to 0
   for (i = 0; i < N; ++i) {
      if (nums.at(i) < 0) {
         nums.at(i) = 0;
      }
   }
   
   // Double each number in the vector
   for (i=0; i < N; ++i) {
       
       int num=nums.at(i);   //gets the value at index i and stores it in variable 'num'
       nums.at(i)=num*2;    //doubles the value of number at index i
       
   }
   
   // Print numbers
   cout << "\nNew numbers: ";
   for (i = 0; i < N; ++i) {
      cout << " " << nums.at(i);
   }
   cout << "\n";

   return 0;
}

Sample output:

Code Version-2(One for loop is removed):


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

int main() {
   const int N=8;
   vector<int> nums(N); // User numbers
   int i=0; // Loop index
   cout << "\nEnter " << N << " numbers...\n";
   for (i = 0; i < N; ++i) {
      cout << i+1 << ": ";
      cin >> nums.at(i);
   }
   
   // Double each number in the vector
   for (i=0; i < N; ++i) {
       
       int num=nums.at(i);   //gets the value at index i and stores it in variable 'num'
       nums.at(i)=num*2;    //doubles the value of number at index i
       
   }
   
   // Print numbers
   cout << "\nNew numbers: ";
   for (i = 0; i < N; ++i) {
      cout << " " << nums.at(i);
   }
   cout << "\n";

   return 0;
}

Sample output:


Related Solutions

#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width;...
I want to indent this c++ program #include<iostream> using namespace std; class Rectangle{ private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; double getPerimeter() const; bool isSquare() const; }; void Rectangle::setWidth(double w){ width = w; } void Rectangle::setLength(double l){ length = l; } double Rectangle::getWidth() const{ return width; } double Rectangle::getLength() const{ return length; } // Added Method definitions double Rectangle::getArea() const{ return (width * length); } double Rectangle::getPerimeter() const{...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct...
Can anyone change it to double linked list #include<stdio.h> #include<stdlib.h> #include <iostream> using namespace std; struct Node {     int data;     struct Node* next; }; void printMiddle(struct Node *head) {     struct Node *slow_ptr = head;     struct Node *fast_ptr = head;     if (head!=NULL)     {         while (fast_ptr != NULL && fast_ptr->next != NULL)         {             fast_ptr = fast_ptr->next->next;             slow_ptr = slow_ptr->next;         }         printf("The middle element is [%d]\n\n", slow_ptr->data);     } } void...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ;...
#include<iostream> #include<cmath> using namespace std; int p = 7; void main() { extern double var ; int p = abs(-90); cout << ::p + p - var << endl; system("pause"); } double var = 5.5;
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT