CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: A few things...

  1. #1
    Join Date
    Sep 2006
    Posts
    8

    A few things...

    Ok so just a few code tidbits needed - a function to add all the odd integers from 1 to n, a function that adds all the integers up from 1 to a number equalling that number such as 1 + 2 + 3 + 4 if they enter 10, and a function that adds the digits of a negative number such as 3 if the number is -12 or 5 if the number is -23.

    Thanks please

  2. #2
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: A few things...

    Hi,

    Have you tried to do this by yourself yet? If so please post your code and say what you are having a problem with and then we'll be able to help you.
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  3. #3
    Join Date
    Jan 2006
    Posts
    90

    Re: A few things...

    They aren't all that hard to do.

    Took me 30 minutes this morning to cobble some things together...

  4. #4
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    I got the first two but for the next question, which is really just finding the sums of a NONnegative number, I jotted down the stuff below then got too confused:

    public class test{
    int i = 0;
    public static int lube(int n){
    while (i < n){
    i = (n / 100) + (n / 10 % 10) + (n % 10);}
    return i;}}

    Is it something like that?

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: A few things...

    Can you please elaborate on your last assignment?

    -petter

  6. #6
    Join Date
    Jan 2006
    Posts
    90

    Re: A few things...

    Quote Originally Posted by Frankmms
    I got the first two but for the next question, which is really just finding the sums of a NONnegative number, I jotted down the stuff below then got too confused:

    public class test{
    int i = 0;
    public static int lube(int n){
    while (i < n){
    i = (n / 100) + (n / 10 % 10) + (n % 10);}
    return i;}}

    Is it something like that?

    Have a look at this:
    Code:
     int addDigits(int num)
       {
          int sum = 0;
    
          while(num > 0)
          {
             sum += (num % 10); // gets the last digit and adds it to the sum
    
             num = num / 10; //relies on integer division to move the number down.
          }
    
          return sum;
       }

  7. #7
    Join Date
    Apr 2001
    Location
    South Africa, Jo'burg
    Posts
    680

    Re: A few things...

    You could also convert the number to a String, strip of the minus sign, convert all the characters back to integers and then add them.

    Code:
    public int addDigits(int num){
    	String s = String.valueOf(num);
    	String[] chars = s.split("");
    	
    	int sum = 0;
    	for(int i = 0; i < chars.length; i++){
    		try{
    			sum += Integer.parseInt(chars[i]);
    		}catch(NumberFormatException nfe){
    			//Catch block will catch the 
    			//Exception that gets thrown when 
    			//trying to convert the minus sign 
    			//to an int and will ignore it
    		}
    	}
    	
    	return sum;
    }
    Byron Tymvios

    Please use [ CODE ] and [/ CODE ] tags when posting code! See THIS on how to use code tags.

  8. #8
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    Taking the sum and dividing it by 10 each time the loop runs then modding to get the last number looks like the easiest way to do it Thanks all.

  9. #9
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    Haha back again...One day this thread will be hundreds of pages long :O

    Ok so I'm supposed to create a triangle out of ***s, given a number of rows, so if you put in 3, it makes the triangle 3 rows tall, 4 is 4 rows tall, etc, looking like:

    3 spaces *
    2 spaces ***
    1 space *****
    0space*******

    I have no idea how to go about even starting this
    Last edited by Frankmms; October 4th, 2006 at 04:23 PM.

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: A few things...

    A new problem should start a new thread...

    Think about how you would do it if you had to use a typewriter and paper (i.e. line by line). Then translate those steps into code.

    Give me a fish and I eat for a day. Teach me to fish and I eat for a lifetime...
    Chinese proverb
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    Ok so I'm thinking

    public int makeTriangle(int rows){
    for (i=1; i > rows; i++){
    System.out.println("Okay now I get lost");}}

  12. #12
    Join Date
    Nov 2003
    Posts
    1,405

    Re: A few things...

    Quote Originally Posted by Frankmms
    Ok so just a few code tidbits needed - a function to add all the odd integers from 1 to n, a function that adds all the integers up from 1 to a number equalling that number such as 1 + 2 + 3 + 4 if they enter 10, and a function that adds the digits of a negative number such as 3 if the number is -12 or 5 if the number is -23.

    Thanks please
    Well, to become a programmer you need to know basic math. The one builds on the other, stupid.

  13. #13
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    It's not math I have trouble with, it's converting math into Java, stupid.

  14. #14
    Join Date
    Nov 2003
    Posts
    1,405

    Re: A few things...

    Quote Originally Posted by Frankmms
    It's not math I have trouble with, it's converting math into Java, stupid.
    So what's the problem then, smartie?

  15. #15
    Join Date
    Sep 2006
    Posts
    8

    Re: A few things...

    It's not math I have trouble with, it's converting math into Java, stupid.

    I also don't get the adding spaces in front of the ***s to make it a centered triangle, maybe another loop, but that's the problems I'm having.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured