Question

In: Computer Science

All the answer need to be in C++ 1) Write a class for a "Vector". The...

All the answer need to be in C++

1) Write a class for a "Vector". The members of the class will be:

Data: int data[100];

methods:

void inserts(int element);

int remove();

int getsize();

bool is empty():

-------------------------------------------------------------------------------------------------------

2) Using your vector class, create an object using a regular instantiation and a pointer instantiation. What are the advantages and disadvantages of both approaches?

-------------------------------------------------------------------------------------------------------

3) What does the following function do and write a program that uses the function:

int foo(int x);

{

if (x>1)

return x*foo(x-1);

return 1;

}

-------------------------------------------------------------------------------------------------------

4) Write a swap function using references.

Solutions

Expert Solution

1)Code -


#include <iostream>

using namespace std;
//vector class created
class Vector{
//public
public:
//variable declare
int data[100];
int count = -1;
//function to insert method to array
void inserts(int element){
count++;
data[count] = element;
}
//function to remove element from array
int remove(){
if(isEmpty()){
cout<<"Array is empty";
}
else{
count--;
}
}
//function to get the size of array   
int getsize(){
return (count+1);
}
//function to check array is empty or not
bool isEmpty(){
if(count == -1)
return true;
else
return false;
}
//display function to display array elemet
void display(){
for(int i=0;i<=count;i++){
cout<<data[i]<<" ";
}
}
};

int main()
{
Vector v;
cout<<"Vector is empty "<<v.isEmpty()<<endl;
v.inserts(2);
v.inserts(3);
v.inserts(4);
v.inserts(5);
v.inserts(22);
v.inserts(2);
v.display();
cout<<"\nSize "<<v.getsize()<<endl;
v.remove();
v.display();
return 0;
}

Screenshots -

2)

Vector v - this is regular way of creating object

Vector *v = new Vector() - this is a pointer instantiation of object

Pointer instantiation of object provide direct access to memory , reduce storage space and complexity of porgram .

Disadvantage - Pointer object has to be deleted externally. Uninitialized pointer object might cause segmentation fault.

3) This program is used to calculate the factorial of a number . It uses recursive function call to implement it.

Code -


#include <iostream>

using namespace std;
int foo(int x)

{

if (x>1)

return x*foo(x-1);

return 1;

}
int main()
{
cout<<"Factorial of 5 is "<<foo(5);

return 0;
}

Screenshots -

4)

void swap(int &a, int &b) {
   int temp;
   temp = a; //temp stores the value at address a 
   a = b;    /* put b into a */
   b = temp; /* put a into b */
  
   return;
}

pls do like , thank you


Related Solutions

(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge returns the vector 1 9 4 7 9 4...
1.Write the Bernoulli equation and state all the assumptions used in its derivation. 2.Write the vector...
1.Write the Bernoulli equation and state all the assumptions used in its derivation. 2.Write the vector form of the conservation of momentum equation for a control volume that is moving with a constant velocity. 3.) Water is flowing in a fire hose with a velocity of 1.0 m/s and a pressure of 200000 Pa. Use the Bernoulli equation to calculate the velocity of the water exiting the nozzle. Note: Derive the expression before plugging in numbers and clearly state all...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will represent Fractions. Note: this is the first part of a multi-part assignment. For this week you should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide...
C. Write your personal story following Purnell’s theory Questions you need to answer: 1. Heritage –...
C. Write your personal story following Purnell’s theory Questions you need to answer: 1. Heritage – tell me about you. 2. How do you communicate within your family? 3. Family dynamics / roles. Who makes decisions? 4. Work force issues – view of education. 5. Biology – skin color, specific body colors, textures – such as hair, or height 6. High risk behaviors – maybe a pattern in the family 7. View of nutrition and how you perceive your nutrition...
I need to write an accounting-internship report for my accounting class which will answer these questions:...
I need to write an accounting-internship report for my accounting class which will answer these questions: i. What you achieved during the internship ii. What you did well iii. What you could have done better iv. Any conflicts that arose during the internship; how you dealt with them v. How you will apply what you learned to your desired career vi. Your next career steps
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all...
11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names (until -1), and use a recursive method to create and output all possible orderings of those names, one ordering per line. When the input is: Julia Lucas Mia -1 then the output is (must match the below ordering): Julia...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT