Thread: printing number from array question..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Well... you have to make it syntactically correct C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to make it work
    and it doesnt print anything
    it just terminates the program
    although i set the arra to be all zeros

    ??

    Code:
                                               //start calculating the long addition procces
    
    for (kndex = 0; kndex < cols; kndex++)
       {
          for (index = 0; index < rows; index++)
          {
              
             arr[kndex]=arr[kndex]+matrix[index][kndex];
    	  }
    
       }
    
    
    carry = 0;
    //loop from the least significant "digit" to the most significant "digit"
    for(index=cols-1;index>=0;index++){
        number = arr[index] + carry;
        arr[index] = number % 10;
        carry = number / 10;
       }
    
    for (kndex = 0; kndex < cols; kndex++)
          {
    printf("%d ",arr[kndex]);
    }
    Last edited by transgalactic2; 12-25-2008 at 01:33 PM.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    transgalactic2, indent your code properly. Post the smallest and simplest compilable code that demonstrates the problem.

    I have been repeating this to you for a very, very long time. If you are not going to listen, I am not going to help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is the simplest code
    the first part is taking the sum of all colums into one array
    the second part is supposed to do the proccess i described in the start
    i built the second part uppon your algorithm
    and my main matrix was
    1 1 1
    2 2 2
    so the array was
    3 3 3
    but when it enters your loop it should print 333
    instead it going to endless loop
    ??
    Code:
                                                                //start first part
    for (kndex = 0; kndex < cols; kndex++)
       {
          for (index = 0; index < rows; index++)
             {
     
             arr[kndex]=arr[kndex]+matrix[index][kndex];
              }
    
         }          //end first part
                                                                                                                             
            carry = 0;          //start second part
           for(index=cols-1;index>=0;index++)
                {
                number = arr[index] + carry;
                arr[index] = number % 10;
                carry = number / 10;
                }
    
    for (kndex = 0; kndex < cols; kndex++)
          {
                 printf("%d ",arr[kndex]);
           }                                                      //end second part

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    this is the simplest code
    No, it is not the simplest code. If it is the simplest code then you are doing something wrong since the "first part" is completely wrong - there is no point computing the sum of all the columns into one array when you can create that one array from scratch. This is an example of what I am talking about:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int digits[50] = {0};
        int i;
        int carry;
        digits[47] = 8;
        digits[48] = 19;
        digits[49] = 10;
    
        carry = 0;
        for (i = 49; i >= 0; --i)
        {
            int number = digits[i] + carry;
            digits[i] = number % 10;
            carry = number / 10;
        }
    
        for (i = 0; i < 50; ++i)
        {
            printf("%d", digits[i]);
        }
        putchar('\n');
    
        return 0;
    }
    Look at how I concentrate on the problem. Look at how you can compile and run the code as-is.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i need to shift the whole array the the right in order that proccess to work

    my array looks like
    |1|2|3|0|0|0|....|0|0|0|
    i need it to look like |0|0|...|1|2|3|
    i tried to do it like this
    ??
    Code:
    for (index = 0; index < 50; index++)
          {
             temp4=digits[49-(cols-1)+index];
             digits[49-(cols-1)+index]=digits[index];
    		 digits[index]=temp4;
           }
    Last edited by transgalactic2; 12-25-2008 at 02:15 PM.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    i need to shift the whole array the the right in order that proccess to work
    That does not make sense. The process should work as-is.

    If you do want to "shift" it, how will that work? A right shift would mean discarding the least significant "digit".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    your code is ok
    it works fine
    but my whole program works on
    this |1|2|3|0|0|0|....|0|0|0|
    kind of array for which i used only the |1|2|3| part
    in order to do this operation like you showed in your code
    i need it to look like |0|0|...|1|2|3|
    Code:
    /*      your array is shifted them to the right i need to do that to
       digits[47] = 8;
        digits[48] = 19;
        digits[49] = 10;
    */
    for (index = 0; index < 50; index++)
          {
             temp4=digits[49-(cols-1)+index];
             digits[49-(cols-1)+index]=digits[index];
    		 digits[index]=temp4;
           }

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    in order to do this operation like you showed in your code
    i need it to look like |0|0|...|1|2|3|
    Ah, but you don't. In your case you are using a little-endian format, so instead of looping in reverse, loop in a forward direction. The algorithm is otherwise the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the problem is with the zeros
    if my number is
    3|3|3|0|000.........
    i need to cut the zeros
    so i could say if you see a zero then stop printing
    but what if
    my array is
    3|0|3|0|000......... in that case it whould print only 3 instead of 303
    thats why i need it to shift the the right like you did
    the problem with that i am afraid that if i will get all the cells full
    then by "shifting" it will change the number i am suppossed to print
    ??
    Code:
    for (index = 0; index < 50; index++)
          {
             
             digits[49-(cols-1)+index]=digits[index];
    		
          }

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i copy the value to the other end i need to put other value to cell i just copied from
    if i put zero it will work for such numbers as |3|3|3|....|0| =>|0|0|0|....|3||3||3|
    but if i get a number like all 9
    |9|9|9|...|9| then it would change the number instead of shifting..
    what to do with the cell i just copied from
    ???

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    so i could say if you see a zero then stop printing
    When printing, scan from the right until you find a non-zero, then keep printing until the end. Basically, you are just ignoring insignificant zeroes. If you scan to the end (i.e., the first element, all the way on the left) without finding a non-zero, the number itself is zero.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i tried to do your example on my type of array
    and i got a line of zeros
    how to shift it to the left if we get a number that goes out of the
    boundres
    because our number was 1000
    and we had only 3 places
    ??
    Code:
    digits[0] = 8;
        digits[1] = 19;
        digits[2] = 10;
    
    	  
    carry = 0;
        for (index = 49; index >= 0; --index)
        {
            int number = digits[index] + carry;
            digits[index] = number % 10;
            carry = number / 10;
        }
    
        for (index = 0; index < 50; ++index)
        {
            printf("%d", digits[index]);
        }
        putchar('\n');

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by transgalactic2
    when i tried to do your example on my type of array
    and i got a line of zeros
    I notice that you are still looping in reverse.

    Quote Originally Posted by transgalactic2
    how to shift it to the left if we get a number that goes out of the
    boundres
    because our number was 1000
    and we had only 3 places
    As I said, do not attempt to "shift". Just ignore the leading zeroes, e.g.,
    Code:
    #include <stdio.h>
    
    #define NUM_DIGITS 50
    int main()
    {
        /* Use little-endian format. */
        int digits[NUM_DIGITS] = {10, 19, 8};
        int i;
        int carry;
    
        /* Normalise such that each digit is a numeral in base 10. */
        carry = 0;
        for (i = 0; i < NUM_DIGITS; ++i)
        {
            int number = digits[i] + carry;
            digits[i] = number % 10;
            carry = number / 10;
        }
    
        /* Find the first non-zero. */
        for (i = NUM_DIGITS - 1; i > 0 && digits[i] == 0; --i);
    
        /* Print the significant digits. */
        for (; i >= 0; --i)
        {
            printf("%d", digits[i]);
        }
        putchar('\n');
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i get 000000000000000000000000000000000000....00001000
    when i run your code
    i need only 1000
    ??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. array question...
    By jerrykelleyjr in forum C Programming
    Replies: 14
    Last Post: 11-14-2006, 05:50 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Replies: 3
    Last Post: 11-03-2003, 08:55 PM