Urgent Help for homework - sum and difference

This is a discussion on Urgent Help for homework - sum and difference within the C++ Programming forums, part of the General Programming Boards category; I am not good at programming, my teacher gave me to write a program and i have to finish it ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Urgent Help for homework - sum and difference

    I am not good at programming, my teacher gave me to write a program and i have to finish it in 2 days. Here is the program which i have to write

    Write a program to determine the sum Z=X+Y and the difference Z=X-Y, where X, Y are n-digit integer decimal numbers and n<=100.

    When I asked how to do that he explained me and he helped me little. Here is what he wrote. Ofcourse they are only clues about the way.


    Z=X+Y
    cost nmax=100
    int X[nmax]
    for(int i=0; i<nmax; i++)
    x[i]= rand()%10;
    z[i] = X[i]+ Y[i] + c;


    Can anyone help me about that program?

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    I suggest you try writing a program with what you know.
    And, then compile it, link it, and run it.

    When, you get the program written post it here and ask for more help.
    Note: I do not expect your program to do much; but, your current stuff has typos and would not even compile.
    Note: My instructor would want "nmax" to be all upper case "NMAX".

    A few good test cases are (for 4 digit numbers 0-3 index in int arrays)
    0,0,0,0 + 0,0,0,1
    9,9,9,9 + 0,0,0,1
    1,0,0,0 - 0,0,0,1

    Hint: You will need to add the ones column first just like you learned addition as a child.

    Edit: I am a C programmer learning C++; so if you are supposed to use classes, I have little I can do to help.

    The line below implies classes/OOP.
    Code:
    Z=X+Y
    Tim S.
    Last edited by stahta01; 03-28-2011 at 09:34 AM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Interesting exercise!

    Quote Originally Posted by Fatal1ty View Post
    Code:
    Z=X+Y
    cost nmax=100
    int X[nmax]
    for(int i=0; i<nmax; i++)
    x[i]= rand()%10;
    z[i] = X[i]+ Y[i] + c;
    This looks reasonable... the intention then is that you store each decimal digit in an int in an array. Fair enough. I don't think using rand()%10 is a good way to test initially -- I think the suggested test cases in stahta01's post are a far better idea - start with something small and manageable that you know the answer to!

    The task description says integers, so this must include support for negative numbers too, right? I'm not sure of the best way to do that. Maybe have a 101st element that stores the sign.

    So you should also try some tests like:
    1,0,0,0 - 9,9,9,9
    1,0,0,0 + (-9,9,9,9) // should be same
    -1,0,0,0 - (-1,0,0,0)


    The Z=X+Y syntax does suggest that you need to put this all in a class and implement operator+. If you're not all that comfortable with operator overloading, implement in C style functions first to get the bit-fiddling right. Unless you do something very bizarre, you should just be able to drop your implementation into an overloaded operator+ later.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,703
    A design decision that needs to be made is does element 0 hold the most significant or least significant digit. I would go with the least significant digit will be in array element zero.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can i count sum of digits in odd/even places?
    By cowa in forum C Programming
    Replies: 9
    Last Post: 11-16-2009, 10:43 PM
  2. help with errors
    By Adila in forum C Programming
    Replies: 2
    Last Post: 10-08-2007, 06:04 AM

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