473,408 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

I need help with this C program

5
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
Sep 17 '06 #1
5 1818
you neab it is the sun of: 27 6 -50 21 -3 14 16 -8 42 33 -21 9
Sep 17 '06 #2
hugo
5
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
Sep 17 '06 #3
D_C
293 100+
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.
Expand|Select|Wrap|Line Numbers
  1.    Total Sum               Max Value
  2. { 0, 0, 0, 0, 0}       { 0, 0, 0, 0, 0}
  3. {-1, 0, 0, 0, 0}       { 0, 0, 0, 0, 0}
  4. { 1, 2, 0, 0, 0}       { 0, 2, 0, 0, 0}
  5. {-2,-1,-3, 0, 0}       { 0, 2, 0, 0, 0}
  6. { 2, 3, 1, 4, 0}       { 0, 3, 1, 4, 0}
  7. {-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.
Sep 17 '06 #4
hugo
5
thanks for the reply.
But, I still face difficulty..is there a simpler program?
Sep 18 '06 #5
D_C
293 100+
I suppose you could brute force it.
Expand|Select|Wrap|Line Numbers
  1. // Read all N integers into an array, say int data[N].
  2.  
  3. int max = 0;
  4. int sum;
  5. for (int i = 0; i < N; i++)
  6. {
  7.   sum = 0;
  8.   for(int j = i; j < N; j++)
  9.   {
  10.     sum += data[j];
  11.     if(sum > max)
  12.       max = sum;
  13.   }
  14. }
Using the same example from before:
-1 2 -3 4 -5

sum takes values (I will bold when max is assigned)
Expand|Select|Wrap|Line Numbers
  1. 0 -1 1 -2 2 -3
  2. 0 2 -1 3 -2
  3. 0 -3 1 -4
  4. 4 -1
  5. 0 -5
Sep 18 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
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...
4
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...
16
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...
8
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...
4
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...
6
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...
1
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...
1
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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
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...
0
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
tracyyun
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...
0
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,...

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.