program in C ++ which enables the addition of elements in a vector dynamically and the calculation of the sum of the elements of the vector using a function. Use classes, create objects, call functions and validate from the main function

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
 
 
 
 
Build a program in C ++ which enables the addition of elements in a vector dynamically and the calculation of the sum of the elements of the vector using a function. Use classes, create objects, call functions and validate from the main function
Expert Solution
Step 1

Program Approach-

  • Include the header file.
  • Declare the integer type vector named myvector.
  • Declare the class named useofVector with class variable number, data2 and class functions  insertElementinVector(), addVectorElement().
  • Define insertElementinVector(int n) function to prompt the user to enter the elements for myvector. Use for loop to insert the elements in the vector.
  •  Define the addVectorElement() function to calculate the sum of the elements of myvector and print the result.
  • Define the main function.
  • Create the object of the class useofVector named v1 and prompt the user to enter the number of elements that he wants to insert in the vector.
  • Call the insertElementinVector() and addVectorElement() functions to provide the required result.
Step 2

Code ( in C ++) with comments :–

//include header file
#include <bits/stdc++.h> 
//use namespace
using namespace std;
//declare the integer type vector named myvector
vector<int> myvector;
//declare the global var
  int intElement;
  //declare the class named useofVector
class useofVector
{
    //class variables
    private:
        int number;
        int data2;
//class functions
    public:
       
//function to insert the elements in myvector dynaically
       void insertElementinVector(int n)
       {
           number =n;
           cout << "\nEnter elements for Vector: ";
           //for loop
           for(int i = 0 ; i< number ; i++) 
           {
             cin >> intElement; 
             //insert the elements in myvector
             myvector.push_back(intElement);
           }
         
       }
       //function to calculate the sum of the elements of myvector
     void addVectorElement()
                 {
                        cout << "\nSum = " << accumulate(myvector.begin(), myvector.end(), 0); 
                 }
                 
};
//main function
 int main()
 {
      int n;
  //create class object
  useofVector v1;
  //prompt the user to enter the number
     cout<<"Enter how many number you want to insert in the vector"<<endl;
     cin>>n;
     //call the functions 

     v1.insertElementinVector(n);
     v1.addVectorElement();
      return 0;
 }

 

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education