Question

Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

Q1. Write a recursive function in C++

void printNumberedChars(string str, int i=0) { ... }

which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following:

1. h

2. e

3. l

4. l

5. o

Q2. Write a recursive function in C++

int sumArray(const int* arr, unsigned int size) { ... }

which takes an array of integers, of the specified size, and returns the sum of the numbers in the array.

Q3. Write a recursive function in C++

long lowestPrimeFactor(long N, long i=2) { ... }

that returns the lowest prime factor of the number N. Note the following cases:

  • If N is less than 2, then return 1
  • If N is prime, then return N
  • Otherwise, return the lowest prime factor of N

Note: It is suggested that the default value of the parameter i should be 2. This default may be different depending on your implementation.

Q4. Write a recursive function in C++

void printPrimeFactors(long N) { ... }

that prints out all prime factors of N in order of largest to smallest.

For example, calling your method from main() with

printPrimeFactors(1289531243);

should print out the following to the output console:

83 83 17 13 11 11 7

Note: Your function code should use the lowestPrimeFactor() function that you wrote in Q3.

Q5. Write a Java class called Rectangle which has length and width private data members and the following methods:

  • A constructor Rectangle(double l, double w), which sets the length and width when a new rectangle object is being created
  • A double perimeter() method which returns the perimeter length of the rectangle
  • A void printInfo() method, which prints out the length, width and perimeter of the rectangle

Q6. Add a second constructor to your Rectangle class which has the method signature Rectangle(double x1, double y1, double x2, double y2) where the points (x1,y1) and (x2,y2) specify the lower left and upper right hand corners of a rectangle. Don't add any new data members to the class. Note that the length of the rectangle is taken to be the dimension in the X-direction and the height is in the Y-direction. Also add a main() method to your class which contains test code that creates two Rectangle objects, using a different constructor for each object, and calls the printInfo() method of each object.

Q7. Write a Java class Publication which has private data members author, title and year (as Java Strings). Add a constructor to your class which allows author, title and year to be initialized. Add a method void display() which prints out the details (author, title and year) of a publication.

Q8. Write a Java class Book which inherits from your Publication class and has an additional data member publisher. Override the void display() method of the parent Book class so that the publisher name is also included when printing out the details of a Book. Add a constructor to your Book class which allows all data members to be given initial values. Book’s constructor must call the constructor of its parent class.

Q9. Add a main() method to your Book class which creates an array of three Publication references. Create and assign a new Book object to each reference in the array and write a for loop that calls the display() method of each Book object.

Q10. Write a Java class called Vector3. A Vector3 is a three-component vector, i.e. representing the vector x i + y j + z k. , where i, j and k are the unit vectors in the x, y and z directions, respectively. Your class should have a constructor, which takes x, y and z component values. Your class should also have a method:

Vector3 crossProduct(Vector3 vec) { ... }

which returns the cross-product of this vector and a given vector vec.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

void printNumberedChar(string str,int i)

{

if(str[i]=='\0')

return ;

else

cout<<(i+1)<<":"<<str[i]<<endl;

printNumberedChar(str, i+1);

  

}

int main()

{

printNumberedChar("PROGRAMMING",0);

}

2)

#include<iostream>

using namespace std;

int sumArray(const int *arr,int size)

{

if(size<0)

return 0;

else

{

//cout<<"\n->"<<*(arr+size);

return *(arr+size)+sumArray(arr,size-1);

}

}

int main()

{

int a[20],n;

cout<<"\nEnter the total numbers in array :";

cin>>n;

for(int i=0;i<n;i++)

{

cout<<"Enter number :";

cin>>a[i];

}

int i=sumArray(a,n-1);

cout<<endl<<"Sum of number in array:"<<i;

}

Please don't post so many questions at once... Only one question at a time is what we are asked to solve.. Also please upvote thanks

Add a comment
Know the answer?
Add Answer to:
Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...
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
  • a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string...

    a. Write a constructor which initializes the canvas_frame, canvas, and title. The constructor receives a string parameter to initialize the title, a Rectangle object to pass to the Rectangle constructor when creating the canvas_frame object, and a Rectangle object to pass to the Rectangle constructor when creating the canvas object. b. Write a print method that prints the title and prints the perimeter of canvas_frame and the area of canvas - call the methods area and perimeter to get both...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • please use c++ Write a program that contains a class Rectangle with two private double precision...

    please use c++ Write a program that contains a class Rectangle with two private double precision members iLength and iWidth, public set and get member functions for these two members, a two argument constructor that sets the length and width to any two user specified values and a void function area() that computes the area and then prints this value by inserting it into the cout output stream. Write a main function that ereates a Rectangle with a length of...

  • . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should...

    . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should have the following features: • A constructor that accepts two double values (x,y) to create a point object. A default constructor. • A void print() method that prints out the points coordinates. • A void set(Point2D p) method that allows you to set the point using the given object of Point2D. • A Point2D midPoint(Point2D p1) method that returns a Point2D object that is...

  • 12. Write a C function void rect(int *ar, int *per, int len, int wid) that computes...

    12. Write a C function void rect(int *ar, int *per, int len, int wid) that computes the area ar and perimeter per of a rectangle with length len and width wid. Test it with a main program that inputs the length and width of a rectangle and outputs its area and perimeter. Output the value in the main program, not in the procedure. Sample Input 6   10 Sample Output Length: 6 Width: 10 Area: 60 Perimeter: 32

  • This exercise guides you through the process of converting an Area and Perimeter application from a...

    This exercise guides you through the process of converting an Area and Perimeter application from a procedural application to an object-oriented application. Create and use an object 1. Open the project named ch04_ex2_Area and Perimeter that’s stored in the ex_starts folder. Then, review the code for the Main class. 2. Create a class named Rectangle and store it in the murach.rectangle package. 3. In the Rectangle class, add instance variables for length and width. The, code the get and set...

  • Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double...

    Book: - title: String - price: double +Book() +Book(String, double) +getTitle(): String +setTitle(String): void +getPrice(): double +setPrice(double): void +toString(): String The class has two attributes, title and price, and get/set methods for the two attributes. The first constructor doesn’t have a parameter. It assigns “” to title and 0.0 to price; The second constructor uses passed-in parameters to initialize the two attributes. The toString() method returns values for the two attributes. Notation: - means private, and + means public. 1....

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

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