473,382 Members | 1,252 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,382 software developers and data experts.

finding the sum combanation

i have an input file with a set of numbers (number per line) , the numbers must include 0 followed by a line that specifies specifies the value of the sum.

example of input file:

5
1
4
3
7
9
0
10

the program should find all possible combination of the sum (10) from the numbers before the 0.

output file:

1 4 5

3 7

1 9

- i made the input file, i tried to save the numbers before the 0 in an array and save the number after the zero as the sum, the problem is that it takes all the numbers and even extra memory locations.

Code:

#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;


int main () {

int s,st[10] ;

ifstream in("exsum.in", ios::in);

int i=0;

while ("exsum.in") {
if (st[i]!=0){
in >> st[i] ;
cout << st[i] << endl;
i++;
}
}

in >> st[i+1];

s= st[i+1];

return 0;

}


- would appreciate any help ^^ , thanks
Dec 20 '06 #1
3 1456
horace1
1,510 Expert 1GB
you need to test for the 0 immediatly after reading a value from the file, e.g.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main () {
  7. int s,st[10] ;
  8. ifstream in("exsum.in", ios::in);
  9.  
  10. int i=0;
  11.  
  12. while ("exsum.in") {
  13. in >> st[i] ;
  14. cout << st[i] << endl;
  15. if (st[i]==0) break;    // break if 0
  16. i++;
  17. }
  18.  
  19. in >> s;
  20. cout << " sum = " << s << endl;
  21.  
  22. return 0;
  23. }
  24.  
Dec 20 '06 #2
thanks ^^. but now it gives me this as output, not the numbers

-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
1245104
4496172
0
sum = -858993460
Dec 20 '06 #3
oh nvm it worked now! thanks alot ^^
Dec 20 '06 #4

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

Similar topics

1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
1
by: Phil Watkins | last post by:
I am a novice programer in Vb and I am having a major brain ache finding out which item has been selected within a list view and then either deleting that item or editing them. My searching so...
0
by: Helge Jensen | last post by:
Having posted in microsoft.public.dotnet.framework.sdk and microsoft.public.dotnet.framework.wmi without receiving any response, I posthere on the off-chance that someone who isn't following those...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
8
by: Rick Strahl [MVP] | last post by:
Hi all, I'm building an app that uses the ASP.Net runtime... One problem I've run into is that pages running inside of the runtime are not finding DLLs in the GAC. In fact, if I look at the...
1
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find the label, using FindControl, during...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
4
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.