Thread: Find missing number in an array given the sum

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    1

    Question Find missing number in an array given the sum

    Here is the problem:

    I have to code a program to solve an array. The number in the array that is not -1 are given, so I cannot change them.

    N1 N2 N3
    2 4 -1
    1 -1 1
    -1 8 -1

    rowsum: 8, 9, 16
    columnsum: 8 9 16

    Firstly, what I did is search row1 for any -1 because by having only one -1 in the row, it means that is easily solvable. If 1x-1, Then I replace it with: row1sum-(row1N1+row1N2+row1N3).

    Then I move on to the next row, checking if there is only 1x-1.
    Then since row3 has 2 zeroes, I will not be able to work on that. So I skip it and check columnN1. If it only has 1x-1, then: columnN1 - (row1N1+row2N2+row3N3). Then column2, then column3.

    My program worked with that problem but it doesn't with this:

    n1 n2 n3
    5 -1 -1
    -1 7 -1
    8 -1 0
    rowsum: 14 19 16
    columnsum: 22 15 7

    Why doesn't my logic works? and if you have any suggestion on how to make this more efficient, I will gladly look into it

    PS. It's my first month learning C, so I don't really have an advance knowledge.
    PPS. I convert -1 to 0 first before adding.
    Last edited by futilecheese28; 04-23-2017 at 08:25 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Why doesn't my logic works?
    How should we know?
    You didn't post your code.

    Saying what you did is one thing.
    Whether your program actually implements what you said is an entirely different matter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You haven't even completely described the problem you're trying to solve. Are negative numbers allowed as answers? Presumably not, since -1 is used to indicate missing numbers. So I don't see how you can solve this (what is the answer?) :

    Code:
    .
     2  4 -1  | 8
     1 -1  1  | 9
    -1  8 -1  |16
    __________
     8  9 16

    Also, the way you described your algorithm is obviously incorrect since it involves using the -1 in your calculation.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If I understand, the problem then you have bad data.

    Because it is bad, when the sum of row sums do NOT equal the sum of column sums.

    Edit: It looks like I do NOT understand the problem. Neither of the examples you posted are able to be solved by me; using what I think you say is the method.

    Tim S.
    Last edited by stahta01; 04-23-2017 at 08:34 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help!! Find number in an array..
    By Elias Zaguri in forum C Programming
    Replies: 12
    Last Post: 10-14-2015, 09:09 AM
  2. [HELP] find closes number from array??
    By Fromar123 in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2012, 01:33 PM
  3. Replies: 5
    Last Post: 03-03-2011, 04:23 PM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM

Tags for this Thread