how to write a program that reads integers until end-of-file and then prints the maximum sum of consecutive values?
example:
if the input is
27 6 -50 21 -3 14 16 -8 42 33 -21 9
the output is
115
which is the sum of
21 -3 14 16 -8 42 33
5 1818
you neab it is the sun of: 27 6 -50 21 -3 14 16 -8 42 33 -21 9
you neab it is the sun of: 27 6 -50 21 -3 14 16 -8 42 33 -21 9
the program wants the maximum sum of consecutive values:
if i sum all, it would be:
27 + 6 + -50 + 21 -3 + 14 + 16 + -8 + 42 + 33 + -21 + 9 = 86 < 115
Open the file, and read all the integers into an integer array. If you have trouble with that like I do, just google it, there are plenty of examples.
Suppose you have N integers. You need two integer arrays of size N. One array is to keep track of the running sum, the other array is to keep track of the maximum. The first entry is for the sum starting with the first number, the second entry is for the sum starting from the second number, etc.
Suppose the input is {-1, 2, -3, 4, -5}. The following shows the values of each array after one iteration. - Total Sum Max Value
-
{ 0, 0, 0, 0, 0} { 0, 0, 0, 0, 0}
-
{-1, 0, 0, 0, 0} { 0, 0, 0, 0, 0}
-
{ 1, 2, 0, 0, 0} { 0, 2, 0, 0, 0}
-
{-2,-1,-3, 0, 0} { 0, 2, 0, 0, 0}
-
{ 2, 3, 1, 4, 0} { 0, 3, 1, 4, 0}
-
{-3,-2,-4,-1,-5} { 0, 3, 1, 4, 0}
After those calculations, traverse through the array max value, looking for the maximum. In this case, {0, 3, 1, 4, 0}. After each iteration through the array, the maximum of maxiums will take the values 0, 3, 3, 4, 4. Therefore 4 is the maximum.
thanks for the reply.
But, I still face difficulty..is there a simpler program?
I suppose you could brute force it. -
// Read all N integers into an array, say int data[N].
-
-
int max = 0;
-
int sum;
-
for (int i = 0; i < N; i++)
-
{
-
sum = 0;
-
for(int j = i; j < N; j++)
-
{
-
sum += data[j];
-
if(sum > max)
-
max = sum;
-
}
-
}
Using the same example from before:
-1 2 -3 4 -5
sum takes values (I will bold when max is assigned) - 0 -1 1 -2 2 -3
-
0 2 -1 3 -2
-
0 -3 1 -4
-
0 4 -1
-
0 -5
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Spamtrap |
last post by:
I only do occasional Perl programming and most things I write are
short processes. I have something I'm working on that is scanning a
text file with about 15 million lines and trying to extract...
|
by: aj902 |
last post by:
Hello ,
I am trying to create a program
where all detail,
http://www.albany.edu/~csi333/projects.htm
|
by: vgame64 |
last post by:
Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that...
|
by: robinsand |
last post by:
My apologies to those of you who are more advanced Visual C++ .NET
programmers, but I am working on a project for an MBA course that is
condensed into an eight-week schedule, and I need help...
|
by: pamelafluente |
last post by:
I am still working with no success on that client/server
problem. I need your help. I will submit simplified versions
of my problem so we can see clearly what is going on.
My model:
A client...
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only...
|
by: naknak4 |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will...
|
by: naknak |
last post by:
Introduction
This assignment requires you to develop solutions to the given problem
using several different approaches (which actually involves using three
different STL containers). You will...
|
by: peterggmss |
last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
|
by: raghavshastri |
last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity.
Here is a sample PGM image with 10...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |