Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
1.40/5 (9 votes)
See more:
Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

input:
2
1 5
10 20

output:
6
30
Posted
Updated 24-Nov-10 4:17am
v2
Comments
Henry Minute 24-Nov-10 10:18am    
And did you have a question?
JF2015 24-Nov-10 10:22am    
Actually this task is not that hard - just use the stuff you learned in school and you should be able to solve it yourself.

Your output appears to be correct, with the possible expection that when you just enter one number, the output should be the number.

I love easy questions like this. I get to help an almost-programmer, AND I get reputation points. Life is good.
 
Share this answer
 
Comments
JF2015 24-Nov-10 10:28am    
Made me laugh!
Pseudo Code:

file = openfile("pathToFile");
file.readLine(); //Discard the first Line 
while(line=file.readLine())
{
    splitted = tokenize(line, " ");
    first = splitted[0];
    second = splitted[1];
    writeToConsole(first + second);
}


Now since everything is broken down into a few small steps,
all you've got to do is translate these steps to C++.

Cheers

Manfred
 
Share this answer
 
Comments
JF2015 24-Nov-10 10:26am    
Good one - will remember using pseudo code the next time someone asks this kind of question ;)
Manfred Rudolf Bihy 24-Nov-10 10:29am    
When I learned to program I found this actually a worthwhile exercise, as it teaches you to structure and organize your thoughts.
Manfred Rudolf Bihy 24-Nov-10 10:46am    
Wondering where the down vote came from ... NOT.
#realJSOP 24-Nov-10 10:54am    
You have to develop a little more style and finesse, and gain some street cred before launching into sarcasm and derision.
Manfred Rudolf Bihy 24-Nov-10 10:57am    
I don't need any of the above mentioned, to get sarcastic, so there. :)
Being a bit controversial here, the problem is one of those problems that if you followed the pseudocode given above you'd end up with a solution that's not really idiomatic in C++, it'd look like a clumsily ported C# or Java solution.

So given the C++ I/O entity is a stream, the solution looks more like:

extract number of lines to read from the input stream
while( there are still lines to process )
    extract the first integer on the line
    extract the second integer on the line
    sum the integers
    insert the result onto the output stream

if any errors occurred
    clear the stream
    tell the user about the error
    give up


Now if you wrap this lot up into a function that take an std::ostream and an std::istream you'll actually get something you can unit test and will work with console IO as well as files and even string.

Cheers,

Ash
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 24-Nov-10 19:36pm    
Ok,I agree with you to some part, but I'd like to take it a step further and postulate that pseudocode should never resemble any known programming language and instead only reflect the ideas behind the possible execution of code on some thought up machine. Hmm, but that could make the whole thing machine dependant (even if thought up), so then we'd just resort to writing an algorithm down in prosa. Oh doh! There's that pseudo code again, but now maybe with some memorable quotes from our ancestors of the writing guild. :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900