• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

can't get this method to work

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, Im writing a class that can store a 25 element char array with several methods. one method that im having trouble with is supposed to add the elements from one object of the class to another and store the result as a third object array. The array represents a 25 digit number i know that it might be easier to use integers but my professor says that if we use chars it'll run faster and use less memory.

im not sure if the trouble is with the nested if's that are supposed to carry the 1, or it its my data type conversion that's messing it up but when i try to print the result using an already working output method all i get is 25 question marks some with boxes around and some without. any help is very much appreciated=)
oh and MAX=25


 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ryan:

Did you check the values of the chars being returned? A bunch of '?'s suggests that you're returning control characters.

John.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Ryan Florida wrote: . . . if we use chars it'll run faster and use less memory. . . .

That sort of advice is usually misguided. When you can pay $100 for enough memory to store every person’s name on earth, you needn’t worry about 25 ints. Anyway, all the actual arithmetic is done with ints.
Why are you using Character.digit()? If you are working in decimal, you can use c - '0' where c is a char between 0 and 9. It won’t work for other number bases. You have to add the '0' after you have finished to get it back to a char.
It is confusing when you have variables called int11 etc. Get them spread out, one declaration per line, with spaces on each side of your binary operators. You never
Get a pencil and paper and see how you add to large numbers, going from right to left. Note what the value of carry is after each column.

No, forget the bl**d* chars. It is just causing you confusion. If you are worried about memory footprint, change your array to a byte[]. Or use the full width of each int and get 225 decimal digits into the length of your array!
If you have used the paper and pencil, you will see you need two new variables. There is a sum and a carry. You get from the addition to sum and carry with judicious use of the / and % operators, remembering their priorities, and putting () in the appropriate locations. You will also know whether you require the value of any of those variables for the next iteration of the addition. If you ever do, then you would have to make sure that value is still in scope and retained until your next column.

Get that lot working before you try subtraction!
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Florida wrote:i know that it might be easier to use integers but my professor says that if we use chars it'll run faster and use less memory.



You are right and your professor is wrong. Okay, it might run faster (so it completes in 0.007 seconds instead of 0.009 seconds) and it might use less memory (so it uses 0.0000003 of your available memory instead of 0.0000006 of your available memory) but then the programmer has to deal with the char-to-int conversion and spend several extra hours getting it to work right. Not to mention that the faster-and-less-memory assertion is unproven and could even be false.

So yeah, you'll still have to do what your professor says, but the take-away lesson is "Premature optimization is evil". It's much better to write a simple program which might be a bit slow and pudgy than it is to write a complicated and unmaintainable program which is a tiny bit faster and slimmer.
 
Ryan Gates
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all very much for your feedback, I just ended up rewriting it using an int array, my professor says i might get some points off for not doing how he said but i've been working on this since 10 this morning so i couldn't care less. I appreciate all the tips and insight you guys gave me. This sites pretty cool im glad i found it. I look forward to discussing future topics with y'all.

Cheers,
Ryan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic