Question

(C++ Algorithms) Given the following function declarations, write the definitions according to the specifications of each....

(C++ Algorithms) Given the following function declarations, write the definitions according to the specifications of each.

All of the algorithms you are being asked to implement operate on pointer ranges. A *pointer range* is a pair of pointers referring to a sequence of objects. For example:

int a[] = {1, 2, 3, 4, 5};
int* first = a + 1;
int* limit = first + 3;

Here, first and limit are pointers to objects in or past the end of a. In a pointer range, the limit pointer is always considered to point past the last element of the range. The range does not include the object pointed to by limit. In fact, we commonly denote ranges using the mathematical interval notation. The expression [first, limit) denotes the sequence of objects starting at first and up to but not including limit

Example Function Definition

// print outputs the value of each object in [first, limit) to std::cout with a space character between consecutive elements.

Given: void print(int* first, int* limit);
void print(int* first, int* limit)
{
  while (first != limit) {
    std::cout << *first << ' ';
    ++first;
  }
}

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

When finished, simply test these functions in main()!

1.) // find returns a pointer to the first object in [first, limit) whose value is equal to "value". Return limit if no such object is found

int* find(int* first, int* limit, int value);

2.) // in Returns true if there exists an object in [first, limit) whose value is equal to value and false otherwise. Note: You should be able to implement this by using find.

bool in(int* first, int* limit, int value);

3.) // count returns the number of objects in [first, limit) whose value is equal to "value".

int count(int* first, int* limit, int value);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//####################### PGM START ###########################

#include<iostream>
using namespace std;
//method to print the elements within [first,limit)
void print(int *first,int *limit){
   while(first!=limit){
       cout<<*first<<" ";
       ++first;
   }
}
//method to find the elements within [first,limit)
int* find(int* first,int* limit,int value){
   while(first!=limit){
       if(*first==value){
           return first;
       }
       ++first;
   }
   return limit;
}
//method to check the value is in [first,limit)
//if found return true else return false
bool in(int *first,int* limit,int value){
   int *result=find(first,limit,value);
   if(result==limit)
       return false;
   else
       return true;
}
//method to count the number of times value is within [first,limit)
int count(int* first,int* limit,int value){
   int count=0;
  
   while(first!=limit){
       if(*first==value){
           count++;
       }
       ++first;
   }
   return count;
}
//main method
int main(){
   int a[]={1,2,3,4,5};
   int *first=a+1;
   int *limit=first+3;
   cout<<"Elements with in limit: ";
   print(first,limit);
   cout<<"\n";
   int *result=find(first,limit,2);
   cout<<"Element 2 is in limit: "<<in(first,limit,2)<<"\n";
   cout<<"Element 8 is in limit: "<<in(first,limit,8)<<"\n";
  
   cout<<"Number of times 3 is in limit: "<<count(first,limit,3)<<"\n";
   return 0;
}

//############################## PGM END ########################################

OUTPUT
########

Add a comment
Know the answer?
Add Answer to:
(C++ Algorithms) Given the following function declarations, write the definitions according to the specifications of each....
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
  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the...

    IN C++ Given ProblemSolution class. Use a pointer to object of “ProblemSolution” class to invoke the “CalculateSum” and “Print” methods of “ProblemSolution”. Write a function:    void Solution(int N1, int N2) that accepts two integers N1 and N2. The function should instantiate “ProblemSolution” object with N1 and N2 values and call “CalculateSum” and “Print” method by creating a pointer to the object of “ProblemSolution”. Input    12 5 Output    17 Assume that, N1, N2 and sum are integers within...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to...

    In C++ #include<iostream> using namespace std; //Implement the function below. bool is_fibonacci_array(int*,int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. //The first two numbers in the sequence are 0 and 1. //The...

  • In this project, you will write a complete program that allows the user to play a...

    In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....

  • Write a C++ program that simulates coin tossing. For each toss of the coin the program...

    Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.   The program should have the following functions as a minimum: void toss() - called from main() and will randomly toss the coin and set a variable equal to the...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h:...

    Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h: ////////////////////////////////////////////////////////////////////////// #ifndef LIST_H #define LIST_H ////////////////////////////////////////////////////////////////////////// namespace CS170 { struct node { int value; node *next; }; class list { public: // Constructor for list. Creates an empty list list(); /* Destructor for list. Empty the list and release the allocated memory */ ~list(); // Prints out the values contained in the list void print_list() const; // Returns the current size of the list...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

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