Computing.Net > Forums > Programming > C++ maximum sum in a series of #s

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ maximum sum in a series of #s

Reply to Message Icon

Name: magic010
Date: February 24, 2004 at 05:54:48 Pacific
OS: windows 98
CPU/Ram: ?
Comment:

I need help with this problem:
Given a series of numbers separated by spaces, find the maximum sum from some consecutive number subset of the numbers. For ex. if we give you the set of numbers: 10 12 -3 -4 12 -13 3 -35 7 8 9 12 -2 -3 15 -6 -2. The maximum sum from a consecutive subset of numbers is 7+8+9+12-2-3+15=46. You should accept as input the set of numbers and produce as output the max sum from that set of numbers.

All I know how to do is input the numbers. I input them in a string, however, I do not know how to work with numbers in a string.
Any help is greatly appretiated. Thanks




Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: February 24, 2004 at 07:04:34 Pacific
Reply:

Why not input them as numbers using scanf? This code will input the entire line (or file) as long as it's only numbers.

int arry[100];
int i=0;

for (i=0; scanf("%d",&arry[i]) == 1; ++i)
continue;

After the loop, i will tell you how many numbers are in the array. You should probably add a check that you don't input more than the array will hold.


If you want/have to input as strings, use atoi() to convert a string to an int.

int i;
char numString[100] = "123";

i = atoi(numString);


0

Response Number 2
Name: magic010
Date: February 25, 2004 at 06:25:10 Pacific
Reply:

Thanks for the help, I'm trying it out today. I'll post again if I need more help.
Thanks again


0

Response Number 3
Name: magic010
Date: February 25, 2004 at 06:55:32 Pacific
Reply:

Hey, I don't have any experience with scanf. I used the code you posted but then I don't know how to work with the input or get any output. I'm actually in a hs class, and our teacher gave us this problem to prepare for a contest. The problem is, he doesn't even know how to complete the problem. So we've been trying to research a little bit, but most of the code like scanf just looks foreign.
So if you could post something a little more specific or maybe some example code, we would appreciate it. Thanks again


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ maximum sum in a series of #s

Add FileName to a series of TXT files www.computing.net/answers/programming/add-filename-to-a-series-of-txt-files-/19981.html

reading in a file www.computing.net/answers/programming/reading-in-a-file/13112.html

Error message C++ www.computing.net/answers/programming/error-message-c/14103.html