c++ - What is the most efficient way to find multiple sums from a file? -
let's have file containing multiple rows of 3 columns:
3 1 3 1 2 3 4 5 6 . . .
the goal find sum of each column. solution simple: make 3 variables sum , 3 more temp variables.
however, solution doesn't scale well. if there 6 columns? i'd have make total of 12 variables. there other ways making count variable , temp variable , adding temp variable correct sum using modulus of count. seems hack.
is there better way of doing or c++ standard library meant this?
why not have single variable called sum , variable called temp. basic outline:
initialize sum 0; while there more lines: read 1 line of input till \n found while line has more inputs: read each number out of (using temp) sum += temp next number print out sum , reset 0 next line
Comments
Post a Comment