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

B

begum

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?
 
P

Peter Jansson

begum said:
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/
 
S

Salt_Peter

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
 
K

Kai-Uwe Bux

begum said:
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
 
B

begum

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:
 
P

Peter Jansson

begum said:
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/
 
B

begum

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

Peter Jansson yazdi:
begum said:
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/
 
P

Peter Jansson

begum said:
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/
 
B

BobR

Kai-Uwe Bux wrote in message ...
begum wrote:
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'...".
 
K

Kai-Uwe Bux

BobR said:
Kai-Uwe Bux wrote in message ...
begum wrote:
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
 
B

begum

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();
}
 
P

Peter Jansson

begum said:
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/
 
S

Salt_Peter

begum said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top