monkey_king 39 Junior Poster

If you want to read in a matrix you need some considerations.

First of all you need some cind of datastructure to have the data in some datatype.

A int** would be very suitable.

But you need to know the dimesions,
you will get the number of columns after the first line,
but you need to loop through the entire row to get the number of lines.

So either make some "dynamic" datastructure, or just read the file 2 times.

It doesn't look like you are allocating any memory in your code snippet.

monkey_king 39 Junior Poster

I have no fucking idea what the question is

monkey_king 39 Junior Poster

I don't understand your problem.

Doesn't the 500 line code work?

Is it a design question?

In a general context in c++,
you never have to write a method that does the sorting.
It's a good exercise though.

monkey_king 39 Junior Poster

I'm not an expert,
but you should be able to do it.

I think its called an aggregate class
http://en.wikipedia.org/wiki/C%2B%2B_structure#Aggregate_classes

monkey_king 39 Junior Poster

Well, your program is a chaotic mix of C and C++.

You might want to work on that for a while, before you get too comfortable with the lazy "it works today" approach.

Start by replacing all printf() with std::cout and all char arrays (and char pointers) with std::string.

Hi,
Thanks for you reply,
apparently I don't get notified when people reply anymore.
So sorry for the delayed response.

I was using std::string,
But it is ridicously slow compared to char*.

I'm reading and parsing huge textfiles, on the mulitigig scale, and many of these.

I tried the std::vector<std::string> approach, and it was very very slow. Each file took aprox 10minutes,
while the single linked list with char arrays took less than 2 minutes.


I agree that it's not a good thing to intermix c/c++ generally

But once in awhile if you know what you are doing, I would deem it ok.

Just as a goto statement makes perfect sense in jumptable.

And not even the c++ devs follows this apartheid approach of std::string vs char*.

The libraries are full of char* function prototypes.
like
'fstream::open(const char*)'

And just the lack of a c++ std::string tokenizer justifies char* in a c++ program.

thanks for your reply

monkey_king 39 Junior Poster

What is the ordering?
If you want to sort by the most frequent elements first, then you can use the link given to you.

If you are sorting by some other rule,
you should say what rule that is.

monkey_king 39 Junior Poster

I solved it,
strdup uses malloc,
so whenever you use a these cstring functions,
you should use free and not delete.

monkey_king 39 Junior Poster

Hi thanks for your reply,
but I don't know the syntax for clearing something that has been allocted like

char ***tokens;
(*tokens) = new char*[len];
for(int i=0 ; i<len ; i++)
  (*tokens)[i] =strdup("cstring");
monkey_king 39 Junior Poster

I'm trying to get myself acquainted with memory allocation and deallocation while writing a program that will actually be usefull for me.

The program is a simple datareader, that tries to read a matrix from file.

The "main" program which works is
'int get_lex(char *in,char *delims,char ***returnvals)'
This will essential return a argv style list of the tokens through 3.parameter.

like

char **line=NULL;
int numToks = get_lex("small.txt"," \t",&line)
for(int i=0;i<numtoks;i++) 
    printf("%i %s",i,line[i]);

The innerworkings of this is a single linked list of char*, that rolls back to a normal array as soon as the cstring is tokenized.
This works.

Now as I loop through the lines of the files I've created another singlelinked list which will have the tokenized argv style string from get_lex, and then a pointer to the next row.

The program works as is!, but I can't nail down a memoryleak that shows up when using valgrind.
A sample file: small.txt would be

1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25

and the program can be run as
'./a.out small.txt'


Heres the full program thanks in advance,
btw I know this can be done easily with std::string and vector or some booost::tokenizer.
But I just want to know how I can fix this faulty program
I know the code is huge but …

monkey_king 39 Junior Poster

when you use new the argument it takes is a size_t
normally everyone is simply using an int since this is enough for most purposes.
the type of size_t is implementation specific, on my computer its a
unsigned long int
This means that on my system If I have a lot of memory, I should be able to allocate
2^63-1 length array.

I'm using 64 bit

monkey_king 39 Junior Poster

just out of curiosity,
what does your init method do?

-----------------------------------------
I would do some thinking about the problem before i started coding.

What base do you need to use,
base_10 is quite naturally for most people.

What kind of datastructure do you want.
If you are using base_10 then a ctype int is somewhat a waste since you wont be using more than 1 byte.
So a char would be nicer but also more difficult to code.

Would it be wise to use reversed lists internally in the program.
When you do plus, minus, multiplication you always start with the least significant bit.

monkey_king 39 Junior Poster

Do you just need to use huge integers,
then check the links given by tux4life.

Or is this some kind of school assignment?

monkey_king 39 Junior Poster

I can't tell you any special problem because there are lots.
But i ask you how to represent big integers and how to to do operations in general.

What exactly do you want, your question is to general.

If you want to represent a arbitrary integer,
you could use a int array, and only assume you will be using vals from 0-9 on each entry.
This will make basicly be how you write a integer on a piece of paper.

And what kind of operations are you talking about?
Basic algebra? or high level polynomial factorization, or some other obscure problem.

You really need to clarify,

good luck

monkey_king 39 Junior Poster

I found out that the sizeof function does return a size_t type.
So it wasn't that strange,

thanks for your reply

monkey_king 39 Junior Poster

Why does printf("size of size_t: %d\n",sizeof(size_t )); make a warning?

Use well-known %lu format specifier for unsigned long int...

What I don't really grasp is that the sizeof function maps into 'unsigned long int' domain, instead of simply int?

Why cant I do a 63 bit left shift?

1 << 63 - constant 1 type is int, not unsigned long int...

thanks typecasting (unsigned long int) 1 << 63 works

Nope. The language standard does not define max array extent, it's an implementation-defined value.

But given the 'one hell of a machine' would I be able to allocate
integer array with length '18446744073709551615' doing

int[] array = new int[18446744073709551615]

Thanks for your reply

monkey_king 39 Junior Poster

I'm having difficulties understanding and printing the max value of a size_t type.

Apparantly the size_t is typedefed to a unsigned long int on my system. Is this safe to assume its the same on all c++ compilers?

The 'sizeof' function returns the number of bytes(size char), used to represent the given datatype.

Why does 'printf("size of size_t: %d\n",sizeof(size_t ));' make a warning?

The size_t is a unsigned long int, so apparantly it uses 8 bytes,
thats 8*8 bits, thats 64 bit
Why cant I do a 63 bit left shift?

It seems that the max value of a size_t on my 64bit ubuntu system is
18446744073709551615
If I had one hell of a machine would this mean that I should be able to allocate an array with this extreme big dimension?

Thanks in advance

#include <iostream>
#include <limits>

int main(){
  std::cout<<(size_t)-1 <<std::endl;
  std::cout<<std::numeric_limits<std::size_t>::max()<<std::endl;
  printf("max using printf:%lu\n",std::numeric_limits<std::size_t >::max()\
);
  printf("size of size_t: %lu\n",sizeof(size_t ));
  printf("size of size_t: %d\n",sizeof(size_t ));
  printf("size of long int: %d\n",sizeof(long int));
  printf("size of unsingned long int: %d\n",sizeof(unsigned long int));

  unsigned long int tmp = 1<<63;
  std::cout<<tmp<<std::endl;

  return 0;
}
-*- mode: compilation; default-directory: "~/" -*-
Compilation started at Sun Mar 29 00:24:02

g++ test.cpp -Wall
test.cpp: In function ‘int main()’:
test.cpp:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has t\
ype ‘long unsigned int’
test.cpp:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has t\
ype ‘long unsigned int’
test.cpp:12: warning: …
monkey_king 39 Junior Poster

hehe,

To the original poster.

Maybe you should clarify a little,
what it is that you want.

monkey_king 39 Junior Poster

You should start by defining the problem itself.

First aff all assume you will only be using ascii symbol.
This way you will only be needing to keep track of 128 diffent kind of charachters.
Make an int array 128 long, initilaize to zero for entire entries


Make a good filereader function,
that reads the file char by char until end of file.
For each char get the ascii value and increment the
intarray[asciival of char] ++;

the int array will then contain a table of occurences of the different chars.
The sum of elements should equal the number of chars read from file.

You say you want the frequenzy, then you just need to divide with the total sum.
But for creating the huffmancode, you dont need the frequnzies just an ordered list.

This should get you started

good luck

monkey_king 39 Junior Poster

HI,
a huffman tree is essentially a funky binary tree.
So you can use all the tricks from the binary tree.
Check wiki
http://en.wikipedia.org/wiki/Tree_traversal

I think you should be looking into the preorder traversel

good luck

monkey_king 39 Junior Poster

It is somewhat difficult to know what you have in real code,
like your comment
//initialize ptr using new

Do you actually do this?

If you supply your full sourcecode it will be easier to help you

monkey_king 39 Junior Poster

Without having lookin so closely at your code shouldnt
child2->accessPtd() be
instance->accessPtr()
instead?

monkey_king 39 Junior Poster

Don't come asking for help when you can search your help all by yourself.

This is the first hit on goole c++ class example
http://functionx.com/cpp/examples/simpleclass.htm
And I think it covers your problems.

People are not angry with you,
but are annoyed by the fact that you didn't bother to check up on a solution for yourself.

If you think your book is bad,
buy another book.

If you are overburden by your courses,
skip one of them.

I have no idea what 16 credit hours mean,
nor do I know what 7 subjects for a semester mean.

And actually I don't care so much.


Feel free to ask for help again,
but atleast give google a try.

monkey_king 39 Junior Poster

monkey_king it compiles but got few errors with it when i run it then enter 12 numbers then select option 3 it closes staright away, there is few more errors , if u jst copy the code run it , u will see.
I was also wondering if there is a way to change it make it look neater , shorter.

I have no intent of doing a copy paste of some code,
that you simply have copy pasted.

Selecting option tells the program to exit,
so exiting is really no error is it?

What are the other errors?

What do you want?
Do you want us to explain you the code line by line?

monkey_king 39 Junior Poster

Post your compile errors,
and I'll have a look at it.

monkey_king 39 Junior Poster

Thanks for your reply,
It wasnt the answer I wanted to hear,
but thanks.

monkey_king 39 Junior Poster

did you try this?

lol, got to remember this one

monkey_king 39 Junior Poster

why dont you atleast try before wasting bandwidth

monkey_king 39 Junior Poster

Use either valgrind or gdb for finding errors.

compile with -g option
then

gdb ./yourprog
run

or
valgrind ./yourprog

monkey_king 39 Junior Poster

I'm using unix, so I can't look at your source files,
but theres something I don't understand in you code for case2

Your are setting temp->next = null;
which probly mean that you want temp->next->next to be deleted instead of temp.

But your should do it before you set the next to null
something like

temp = temp -> next;
delete temp->next;
temp -> next = NULL;
delete cursor;

I have no idea what your 'curser' is though,
so I might be way of.
I haven't looked at your case3.

monkey_king 39 Junior Poster

or as a reference

monkey_king 39 Junior Poster

whats your platform?

monkey_king 39 Junior Poster

emacs if i'm going to code for more than 5 minutes otherwise vim.
sometimes I'm using emacs with speedbar and gud mode.

But mostly I'm sitting in a term over ssh,
so 90% of the time I'm using emacs with the -nw option.

I would really like to use vim more,
but the indentation is really inferior to the emacs indentation.
Pressing tab will indent to the correct column according to you scope.
Vim basicly just indents to whatever you have set your tab width to.

I tried a .vimrc hack that were supposed to do it emacs style,
but then it messed up my Makefiles.

I'm in no way trying to convert ppl to using a commandline for everything. I started up using all kinds of gui.

But using an terminal for everything is basicly making me more productive.

monkey_king 39 Junior Poster

A commen misunderstanding is that the closed form version,
is an approximation.

For integers, it will be just as accurate as the iterative or recursive version.

monkey_king 39 Junior Poster

Hi,
I'm considering using some boost stuff for my next project.

But it's quite essential, that the program can be ported to multiple platforms with different setups.

I've always used
-ansi -pedantic -Wall
And so far the projects has been working on sun/gnu/intel compilers,
on both windows and linux.

But what if i #include<booststuff>,
is this a big problem for users on a foreign platform.

Can I simply make a copy of the .hpp file to the project dir,
or are boost file too much interrelated?

thanks in advance

monkey_king 39 Junior Poster

Then show us the line that contains the assignment error.
And maybe even the compile error itself.

monkey_king 39 Junior Poster

I still don't understand what the question is ;)

monkey_king 39 Junior Poster

>>if you can have it in memory, then you can have it in a vector.

No it doesn't. If he is still using 32-bit compiler than the size of the long int is not changed from what it is on a 32-bit operating system, and the STL libraries will still be 32-bit libraries. He will need to use a 64-bit compiler to make any use of the 64-bit os.

Well the poster says he is using a 64bit linux,
so I found it safe to assume he is using a 64bit toolchain.

But your argument is valid. But for sake of completeness, the same can be said about a 16 bit compiler :)

Ancient Dragon commented: Yes, that was my point :) +36
monkey_king 39 Junior Poster

If you are using 64 linux,
then your long ints are 8 bytes.

Not sometimes 4,5,6,7 but always 8.
The max mapped memory for 32bit is 2^32 around 4 gig
The max mapped memory for 64bit is 2^64 around 1.6 million terabytes (this means alot!).

The unsigned integer range for 32 bit is 0 to +4,294,967,295
The unsigned integer range for 64 bit is 0 to +18,446,744,073,709,551,615

This roughly means that
if you can have it in memory, then you can have it in a vector.

Now what kind of algorithm are you using, to solve what kind of problem.

I've been using Vector<string> to store 36 gig of memory, in some gene expression statiscal problem.
But in the end I ended up using standard **char, since I couldn't .reserve() because I didn't have knowlegde at the beginning of the program.

monkey_king 39 Junior Poster

Try cutting down your program to just a few lines,
check the compile errors, fix those.
Then you should understand what you did wrong.

monkey_king 39 Junior Poster

It depends on how you matrix is defined as a datastructure.

Sometimes they are defined as one long array.
Then you can just add the array elementwise.

If they are defined as double pointers,
then you should loop through the entire matrices in the sameorder.

monkey_king 39 Junior Poster

I would use modulo and div

that is if I want to extract the second digit of 234

I would (234 %100)/10

The first paranthesis, strips out the 2 such that 34 remains
this slash 10 then calulates how many timis 10 divides 34 without remainder.

monkey_king 39 Junior Poster

I gave it as urgent because of the lack of time in my hand.

I can understand why you call it urgent, when it is urgent for you.
But that doesn't make it urgent for me.

I don't read every post on the boards I've signed up for,
I just check the first page, if there is anything I can help with.

So a subject saying "Urgent" is likely to get fewer responses that something like "connect() error in windows socket programming"

Are there some special reason you don't supply your sourcecode?
I do like to help people,
but I am not really inclined to write 100 lines of code for a specific post on some internet board.

Now regarding your problem:

What platform are you using?

monkey_king 39 Junior Poster

On a general note, (anyone correct me if I am wrong).

The bool type is part of the c99 standard, and is as such not expected to be as portable.

I've never found a use for the bool type that the short int or char couldn't solve.
The bool still takes 1byte of space which is the same as 1char.

monkey_king 39 Junior Poster

Hi,
I had som difficulties understanding what you wanted but I think I got it now.

You want draw a star with an uneven number of points using stdoutput?

Do you use some special algorithm for drawing the star,
or is that your problem?

monkey_king 39 Junior Poster

consider using printf and tabs
If you really want to use c++ you should look into iomanip
http://www.fredosaurus.com/notes-cpp/io/omanipulators.html

printf("1\t2\t3\n");
will be turned into
1 2 3
You shouldn't have to type in the spaces manually

monkey_king 39 Junior Poster

I think you need to include cmath and or compile with -lm

monkey_king 39 Junior Poster

goto's can only jump to labels within the same function,
so I don't think you will be able to do it with goto's if you are in some far away function.

There are any beautifull way of doing what you want without having to redesign you program or use c++ exceptions.

On a general note goto should be avoided.
I've only found them usefill in some crazy switch statements.
Which basicly changes it to a jump table.

monkey_king 39 Junior Poster

I don't really understand the concept of a prime number or how to calculate it sorry.

Then you should ask in a mathforum and not a c++ programmer forum.

A prime number is a number that only has 2 divisors.
the number one and the number itself.

This means that if you can find a number: not 1 and not the number itself, that divides you original number (no remainder).

Then it's not a prime number.
edit:
So your loop shouldn't start at 1 but at 3.

monkey_king 39 Junior Poster

This codepad is nice!!

monkey_king 39 Junior Poster

Actually in a general context the coefficents can be whatever you want them to be.
Not necessary a field, but most likely a ring.

So it should actually be templated.

But I doubt that the original poster is looking at this level of sophistication.