473,503 Members | 12,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

Oct 7 '06 #1
12 2319
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/
Oct 7 '06 #2


On Oct 7, 4:45 am, "begum" <begums...@gmail.comwrote:
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

Oct 7 '06 #3
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 <algorithmprovides 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
Oct 7 '06 #4
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/
Oct 7 '06 #5
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/
Oct 7 '06 #6
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/
Oct 7 '06 #7
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/
Oct 7 '06 #8

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 <algorithmprovides 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<intVec;
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 <GR
POVrookie
Oct 7 '06 #9
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 <algorithmprovides 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<intVec;
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

Oct 7 '06 #10
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/
Oct 11 '06 #11
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.hshould be <iostream>
* <cionio.hdoes 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/
Oct 11 '06 #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();
}
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.

Oct 11 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15730
by: ^_^ | last post by:
How to convert HEX <-> DEC constant value in C source file, and write out the result c files? a=4; b=6; from or to a=0x4;
4
2302
by: kivan.maharaj | last post by:
What I want to do is write a program to test lexicographical combinations of various sets of numbers to a predefined number and write that true values to a text file. Example: Number choosen...
6
5004
by: Dixie | last post by:
I have asked this question before, but I could not get the suggested solution work. So I will give more details this time. I have an append query that adds several hundred records to a table...
5
31995
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as...
2
1994
by: begum | last post by:
i write a program about finding the max value of a subseqence in a given sequence. i write but i finf lots of syntax mistakes and i can't do them in correct. please help me. Thanks; Begum...
2
3364
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
6
10642
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
9
1725
by: Ken Fine | last post by:
Hi there, I have written a simple function that attempts to set the angle of objects so as to place them in aesthetically appealing ways. The code follows; there is some stupidness in it (e.g. a...
275
12036
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.