max value of a subsequence in a given sequnce of numbers.

Discussion in 'C++' started by begum, Oct 7, 2006.

  1. begum

    begum Guest

    in this program they want me to find the max value of a subsequence in
    a given sequnce of numbers(the sequence is represented by an array) by
    using templates and pointers.also i must write a main in order to
    instantiate differnt objects of my class. in this program i don't want
    to find the sum!!!!. please can anyone help me?
     
    begum, Oct 7, 2006
    #1
    1. Advertising

  2. begum wrote:
    > in this program they want me to find the max value of a subsequence in
    > a given sequnce of numbers(the sequence is represented by an array) by
    > using templates and pointers.also i must write a main in order to
    > instantiate differnt objects of my class. in this program i don't want
    > to find the sum!!!!. please can anyone help me?
    >


    Post what you have done so far and we may help.


    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
     
    Peter Jansson, Oct 7, 2006
    #2
    1. Advertising

  3. begum

    Salt_Peter Guest

    On Oct 7, 4:45 am, "begum" <> wrote:
    > in this program they want me to find the max value of a subsequence in
    > a given sequnce of numbers(the sequence is represented by an array) by
    > using templates and pointers.also i must write a main in order to
    > instantiate differnt objects of my class. in this program i don't want
    > to find the sum!!!!. please can anyone help me?


    Really? Wow. So like... you have an array? Lets see it.
    Show us the array, populate it as required and then print its elements
    to the screen.
    No templates needed, no pointers needed, no max value needed. Just
    output the array.
    Lets face it, if you can't do that, how are you going to manipulate its
    contents in any way?
    hmm?

    http://www.parashift.com/c -faq-lite/how-to-post.html
     
    Salt_Peter, Oct 7, 2006
    #3
  4. begum

    Kai-Uwe Bux Guest

    begum wrote:

    > in this program they want me to find the max value of a subsequence in
    > a given sequnce of numbers(the sequence is represented by an array) by
    > using templates and pointers.also i must write a main in order to
    > instantiate differnt objects of my class. in this program i don't want
    > to find the sum!!!!. please can anyone help me?


    The header <algorithm> provides a generic function:

    template<class RandomAccessIterator>
    void nth_element(RandomAccessIterator first,
    RandomAccessIterator nth,
    RandomAccessIterator last);

    using std::less, but you can also supply a comparison predicate of your own:

    template<class RandomAccessIterator, class Compare>
    void nth_element(RandomAccessIterator first,
    RandomAccessIterator nth,
    RandomAccessIterator last,
    Compare comp);

    So, in order to find the minimum in a range [from,to), you would do:

    nth_element( from, from, to );
    minimum = *from;

    You can vary this to find the maximum instead.

    Warning: this algorithm rearranges the sequence. Make sure that you are
    allowed to do that.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 7, 2006
    #4
  5. begum

    begum Guest

    i wirte only one part of program. here it is;
    template <class someType>
    class sum{
    private:
    someType arr[100];
    public:
    void add();
    void get_sequence();
    }
    but i cant find how to write max value.

    Peter Jansson yazdi:
    > begum wrote:
    > > in this program they want me to find the max value of a subsequence in
    > > a given sequnce of numbers(the sequence is represented by an array) by
    > > using templates and pointers.also i must write a main in order to
    > > instantiate differnt objects of my class. in this program i don't want
    > > to find the sum!!!!. please can anyone help me?
    > >

    >
    > Post what you have done so far and we may help.
    >
    >
    > Sincerely,
    >
    > Peter Jansson
    > http://www.p-jansson.com/
    > http://www.jansson.net/
     
    begum, Oct 7, 2006
    #5
  6. begum wrote:
    > i wirte only one part of program. here it is;
    > template <class someType>
    > class sum{
    > private:
    > someType arr[100];
    > public:
    > void add();
    > void get_sequence();
    > }
    > but i cant find how to write max value.
    >


    You have not written the implementation of the above interface? Should
    not get_sequence return anything as the name "get_..." implies?

    Regarding the max value, how about the following interface:

    template <class someType>
    class sum{
    private:
    someType arr[100];
    public:
    void add();
    void get_sequence();
    someType max_value() const;
    }


    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
     
    Peter Jansson, Oct 7, 2006
    #6
  7. begum

    begum Guest

    and what about pointers. this is my main problem i have to use
    pointers that's why i have problems

    Peter Jansson yazdi:
    > begum wrote:
    > > i wirte only one part of program. here it is;
    > > template <class someType>
    > > class sum{
    > > private:
    > > someType arr[100];
    > > public:
    > > void add();
    > > void get_sequence();
    > > }
    > > but i cant find how to write max value.
    > >

    >
    > You have not written the implementation of the above interface? Should
    > not get_sequence return anything as the name "get_..." implies?
    >
    > Regarding the max value, how about the following interface:
    >
    > template <class someType>
    > class sum{
    > private:
    > someType arr[100];
    > public:
    > void add();
    > void get_sequence();
    > someType max_value() const;
    > }
    >
    >
    > Sincerely,
    >
    > Peter Jansson
    > http://www.p-jansson.com/
    > http://www.jansson.net/
     
    begum, Oct 7, 2006
    #7
  8. begum wrote:
    > and what about pointers. this is my main problem i have to use
    > pointers that's why i have problems


    Well, as soon as you write some code that shows us (me at least) what
    you have tried then I will be happy to give you some directions on what
    is wrong. Otherwise it is diffucult for us (me at least) to guess what
    your problem that you describe as "problem i have to use pointers"
    really is.


    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
     
    Peter Jansson, Oct 7, 2006
    #8
  9. begum

    BobR Guest

    Kai-Uwe Bux wrote in message ...
    >begum wrote:


    >> in this program they want me to find the max value of a subsequence in
    >> a given sequnce of numbers(the sequence is represented by an array) by
    >> using templates and pointers.also i must write a main in order to
    >> instantiate differnt objects of my class. in this program i don't want
    >> to find the sum!!!!. please can anyone help me?


    >The header <algorithm> provides a generic function:

    [snip]
    >You can vary this to find the maximum instead.


    <smartass>
    Why not just use std::vector and std::max_element ? (they *are* templates)

    {
    std::vector<int> Vec;
    std::generate_n(back_inserter(Vec), 1000, rand);
    std::vector<int>::const_iterator it = max_element(Vec.begin(), Vec.end());
    std::cout << "The largest element is " << *it << std::endl;
    }
    // [ begum, do NOT turn that in! You'll get an "F"! ]
    </smartass>

    Instructors should be more careful with their wording and/or students should
    copy/paste the assignment (and their effort, of course.).

    Being a rebel, I would turn that in, and then go to the Dean and complain "he
    said 'using templates', not 'write a templated class'...".

    --
    Bob <G> R
    POVrookie
     
    BobR, Oct 7, 2006
    #9
  10. begum

    Kai-Uwe Bux Guest

    BobR wrote:

    >
    > Kai-Uwe Bux wrote in message ...
    >>begum wrote:

    >
    >>> in this program they want me to find the max value of a subsequence in
    >>> a given sequnce of numbers(the sequence is represented by an array) by
    >>> using templates and pointers.also i must write a main in order to
    >>> instantiate differnt objects of my class. in this program i don't want
    >>> to find the sum!!!!. please can anyone help me?

    >
    >>The header <algorithm> provides a generic function:

    > [snip]
    >>You can vary this to find the maximum instead.

    >
    > <smartass>
    > Why not just use std::vector and std::max_element ? (they *are* templates)
    >
    > {
    > std::vector<int> Vec;
    > std::generate_n(back_inserter(Vec), 1000, rand);
    > std::vector<int>::const_iterator it = max_element(Vec.begin(),
    > Vec.end()); std::cout << "The largest element is " << *it << std::endl;
    > }


    Oops, you are right: that is much better then using nth_element. I just had
    forgotten about max_element.


    > // [ begum, do NOT turn that in! You'll get an "F"! ]


    That would be either a shame (if the instructor does it to punish the
    solution because it was not taught yet) or perfectly justified (if the
    instructor does it to punish copying).


    > </smartass>
    >
    > Instructors should be more careful with their wording and/or students
    > should copy/paste the assignment (and their effort, of course.).


    I would bet you, the wording of the problem was originally much clearer and
    far less ambigugus.


    > Being a rebel, I would turn that in, and then go to the Dean and complain
    > "he said 'using templates', not 'write a templated class'...".


    You shouldn't make it past the associate chair of the department with that
    complaint.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 7, 2006
    #10
  11. begum

    begum Guest

    i wirte the all program but there is only 1 error and i can't fix it.
    may be you can help me. this is my program. my problem is in line 18.
    it says that Sum type is undeclared . what can i do to declare it

    #include <iostream>
    #include <conio.h>
    #include <iomanip.h>
    #pragma hdrstop
    template <class someType>
    class Sum
    {
    private:
    someType arr[100];
    public:
    void add();
    void get_sequence();
    someType max_value() const;


    };
    template <class someType>
    void Sum<sumType>::add()
    {
    int i;
    cout<<"enter a sequence to find maximum value\n"<<endl;
    cin>>"%d",&d>>endl;
    }
    template <class someType>
    void Sum<sumType>::get_sequence()
    {
    int maxSum=0;
    int thisSum=0;
    int j;
    for (j=0; j<101; j++)
    {
    thisSum +=arr[j];
    if (thisSum > maxSum)
    {
    maxSum = thisSum;
    }
    else if (thisSum < 0)
    {
    thisSum=0;
    }
    return maxSum;
    }
    }
    void main ()
    {
    void add();
    void get_sequence();
    }
    Peter Jansson wrote:
    > begum wrote:
    > > and what about pointers. this is my main problem i have to use
    > > pointers that's why i have problems

    >
    > Well, as soon as you write some code that shows us (me at least) what
    > you have tried then I will be happy to give you some directions on what
    > is wrong. Otherwise it is diffucult for us (me at least) to guess what
    > your problem that you describe as "problem i have to use pointers"
    > really is.
    >
    >
    > Sincerely,
    >
    > Peter Jansson
    > http://www.p-jansson.com/
    > http://www.jansson.net/
     
    begum, Oct 11, 2006
    #11
  12. begum wrote:
    > i wirte the all program but there is only 1 error and i can't fix it.
    > may be you can help me. this is my program. my problem is in line 18.
    > it says that Sum type is undeclared . what can i do to declare it
    >
    > #include <iostream>
    > #include <conio.h>
    > #include <iomanip.h>
    > #pragma hdrstop
    > template <class someType>
    > class Sum
    > {
    > private:
    > someType arr[100];
    > public:
    > void add();
    > void get_sequence();
    > someType max_value() const;
    >
    >
    > };
    > template <class someType>
    > void Sum<sumType>::add()
    > {
    > int i;
    > cout<<"enter a sequence to find maximum value\n"<<endl;
    > cin>>"%d",&d>>endl;
    > }
    > template <class someType>
    > void Sum<sumType>::get_sequence()
    > {
    > int maxSum=0;
    > int thisSum=0;
    > int j;
    > for (j=0; j<101; j++)
    > {
    > thisSum +=arr[j];
    > if (thisSum > maxSum)
    > {
    > maxSum = thisSum;
    > }
    > else if (thisSum < 0)
    > {
    > thisSum=0;
    > }
    > return maxSum;
    > }
    > }
    > void main ()
    > {
    > void add();
    > void get_sequence();
    > }



    Hello,

    * <iostream.h> should be <iostream>
    * <cionio.h> does not exist in Standard C++.
    Remove it, is not needed here.
    * cout, cin and endl should be qualified, e.g.
    std::cout.
    * sumType should be someType.
    * In the add() method;
    - d is not defined.
    - What is ("%d",&d)?
    - Should you not be adding something to the sequence?
    * main should return int and not void.
    * You need to difine an instance of your template class
    in main and use it's methods instead of just declaring
    some methods there.


    Hope this helps, you have to figure out what to do in add()
    by reading your favourite C++ tutorial or book.


    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
     
    Peter Jansson, Oct 11, 2006
    #12
  13. begum

    Salt_Peter Guest

    begum wrote:
    > i wirte the all program but there is only 1 error and i can't fix it.
    > may be you can help me. this is my program. my problem is in line 18.
    > it says that Sum type is undeclared . what can i do to declare it
    >
    > #include <iostream>
    > #include <conio.h>
    > #include <iomanip.h>
    > #pragma hdrstop
    > template <class someType>
    > class Sum
    > {
    > private:
    > someType arr[100];
    > public:
    > void add();
    > void get_sequence();
    > someType max_value() const;
    >
    >
    > };
    > template <class someType>
    > void Sum<sumType>::add()
    > {
    > int i;
    > cout<<"enter a sequence to find maximum value\n"<<endl;
    > cin>>"%d",&d>>endl;
    > }
    > template <class someType>
    > void Sum<sumType>::get_sequence()
    > {
    > int maxSum=0;
    > int thisSum=0;
    > int j;
    > for (j=0; j<101; j++)
    > {
    > thisSum +=arr[j];
    > if (thisSum > maxSum)
    > {
    > maxSum = thisSum;
    > }
    > else if (thisSum < 0)
    > {
    > thisSum=0;
    > }
    > return maxSum;
    > }
    > }
    > void main ()
    > {
    > void add();
    > void get_sequence();
    > }
    > Peter Jansson wrote:
    > > begum wrote:
    > > > and what about pointers. this is my main problem i have to use
    > > > pointers that's why i have problems

    > >
    > > Well, as soon as you write some code that shows us (me at least) what
    > > you have tried then I will be happy to give you some directions on what
    > > is wrong. Otherwise it is diffucult for us (me at least) to guess what
    > > your problem that you describe as "problem i have to use pointers"
    > > really is.
    > >
    > >
    > > Sincerely,
    > >
    > > Peter Jansson
    > > http://www.p-jansson.com/
    > > http://www.jansson.net/


    Do yourself a favour and start with the basics. The class is not a
    "Sum", its a container which happens to have a private, primitive array
    as its only member. Lets call it Array for the sake of sanity. Array is
    not an object, it does NOT exist, its just a blueprint.
    So job#1 is how do you create an instance of that Array class?

    template< typename T >
    class Array
    {
    T arr[10];
    };

    int main()
    {
    /* create an instance of Array here */

    return 0;
    }

    If you can't do that, its no point showing you anything. Everything
    else is secondary.
     
    Salt_Peter, Oct 11, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lord0
    Replies:
    1
    Views:
    605
    Thomas Weidenfeller
    Apr 19, 2006
  2. chiara
    Replies:
    6
    Views:
    504
    Barry Schwarz
    Oct 6, 2005
  3. begum
    Replies:
    2
    Views:
    386
    Daniel T.
    Oct 10, 2006
  4. 2Barter.net
    Replies:
    0
    Views:
    401
    2Barter.net
    Dec 13, 2006
  5. diz rael

    subsequence regular expression

    diz rael, Jan 25, 2006, in forum: Ruby
    Replies:
    4
    Views:
    100
    Simon Strandgaard
    Jan 25, 2006
Loading...

Share This Page