?
Solved

How to collapse integers in text file into single digit in the fastest time?

Posted on 2004-10-08
28
Medium Priority
?
575 Views
Last Modified: 2008-02-01
Hi experts!

I have this text file consist of integers (10 MB file) and i want to collapse all the integers into single digit in the fastest time possible. Imagine 10MB of file would consist of how many integers..

I need help on this urgently..maybe you guys can put comments on the coding you wrote so i can understand the process in the coding  because i'm not so good with C++..

i give away 500 points for the best solution..

thanks a lot..
0
Comment
Question by:Diode_Temp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
  • 5
  • +2
28 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 12259366
binary or text. what is the collapsing mechanism? an example:
1234567890
sum of digits = 45
sum of digits = 9
now the number is single digit so you stop.
0
 

Author Comment

by:Diode_Temp
ID: 12259524
i forgot..

the integers in the text file is in one line.. but it would be a long one to make it 10MB.. no space between the integers..

12347435794864982316498652189236598236593218659213865923659385698231659   -->>  all through the way..

collapse them in single digit (plus each integers until get single digit)

cheers
0
 

Author Comment

by:Diode_Temp
ID: 12259618
yeah jhshukla.. exactly like you said.. :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:guntherothk
ID: 12259724
Presumably your 10Mb of text is meant to have some format. How many characters represent each integer? Or do you simply want to pack two 4-bit digits into each byte to halve the size of the file?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12259733
So you could

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main () {

    ifstream is ( "file.txt");
    string str;

    getline(is,str);

    while(str.size() != 1) {

        for ( int i = 0; i < str.size(); ++i) {

             n+=str[i]-'0';
        }

        stringstream ss;

        ss << n;

        str = ss.str();
    }

    cout << n << endl;

    return 0;
}
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12259971
jkr:
...
n += str[i]-'0'
n %= 10;
...

jaydutt
0
 
LVL 86

Expert Comment

by:jkr
ID: 12260150
>>n %= 10;

No, that's being taken of during the next round in the 'while()' loop.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12260427
yup. my bad.
0
 

Author Comment

by:Diode_Temp
ID: 12264633
hi jkr..

i'm having problem compiling your codes because my compiler said it was unable to open include file "sstream.h". I'm using Borland C++ version 5.02.

0
 
LVL 86

Expert Comment

by:jkr
ID: 12264645
>> my compiler said it was unable to open include file "sstream.h"

There shouldn't be such a file - it is <sstream>, without a trailing .h extension. You might want to check if there's an updated STL version for your compiler at www.stlport.org
0
 

Author Comment

by:Diode_Temp
ID: 12264772
i put <sstream> without .h extension and that is the error msg.

i'll try to find the updated STL version you said (although i don't know what it is..:)
0
 
LVL 86

Expert Comment

by:jkr
ID: 12264777
That's baiscally a bunch of header files to replace your STL an the one or the other library...
0
 

Author Comment

by:Diode_Temp
ID: 12264817
i try to install the updated STL but still cannot.. iguess the installion is not a success because it abit messy that i could not understand how to install..

so is there any way to solve this or if there is any other codings that can do the same function?
i like your coding because it short andunderstandable..but i cannot compile it..sorry
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12264827
...
n += str[i]-'0'
n %= 10;
...

doesn't require sstream!
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 12264854
>>doesn't require sstream!

Um, but there's something else at odds a *lot* if a stringstream is giving trouble. At least, that's part of the C++ standard.
0
 

Author Comment

by:Diode_Temp
ID: 12264946
it give error now..

Error:  NONAME01.CPP(12,20):Could not find a match for 'std::getline(ifstream,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'

any other codings? Please..
0
 

Author Comment

by:Diode_Temp
ID: 12271072
Anyone please?

jkr? Any other ways?
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12271933
did you try what I posted? I am not trying to prove jkr wrong but you should try everything that is posted here and that makes sense to you.
0
 

Author Comment

by:Diode_Temp
ID: 12273151
i've tried your version and posted the error up there..

Error:  NONAME01.CPP(12,20):Could not find a match for 'std::getline(ifstream,std::basic_string<char,std::string_char_traits<char>,std::allocator<char>>)'

If there any modification to be done?
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12273731
i tried and it compiles and runs fine with MSVC++ 6.0.
try is.getline()
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12273762
no prob with bcc either. here's command line output

C:\Documents and Settings\Jaydutt>bcc32 cpp.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
cpp.cpp:
Warning W8012 cpp.cpp 17: Comparing signed and unsigned values in function main()
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\Documents and Settings\Jaydutt>

Jaydutt
0
 

Author Comment

by:Diode_Temp
ID: 12274007
sorry Jaydutt.. i've tried.. but still got error..both bcc and msvc++..

can you please write here the whole coding with the changes you made? maybe i wrote the codes wrongly..i'm not so good in C++ even the basics one.. if possible for Borland C++ because i familiar wth Borland C++...i don't know how to detect the error if using MSVC++..it just show errors found but not pointed where the error is..maybe i'm the only one who do not know to use MSVC++ :)

thanks
0
 
LVL 9

Expert Comment

by:zaghaghi
ID: 12285909
you must use these headers:

<iostream> --->  <iostream.h>
<fstream>  --->  <fstream.h>
<string>     --->  <string.h>
<sstream> --->  <strstrea.h> or <strstream.h>

have a good programming day.
0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 1000 total points
ID: 12301560
sorry again for late response. and no I did not make any major change in the code. I copy-pasted jkr's code (10/08/2004 11:23AM EDT) and declared variable n. but that is not what the compiler is complaining about.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12651248
50+/-10 points to jkr
50+/-10 points to jhshukla
delete the rest of points.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question