printing number from array question..

This is a discussion on printing number from array question.. within the C Programming forums, part of the General Programming Boards category; i got this 50 cell integer array i use only a few cells from 0(which represents the highest coefficient of ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    printing number from array question..

    i got this 50 cell integer array
    i use only a few cells from 0(which represents the highest coefficient of 10^(col-1) )
    to cell col-1 (which coefficient is 10^0 =1)
    i got an input in this array
    i could have the number 123 in cell col-1
    so i need to add in cell col-1 the number 3
    int cell col-2 (tens) i need to add 2
    and in cell col-3 (the hundreds) i need to add 1

    the problem is that in some cell there could be such a big number
    that i would need to change the variable col.
    and i couldnt do this simple process that i described in the beginning

    i tried to build this :
    Code:
                                           
    kounter=0;
    for (index=0;index<cols;index++){
         sumc[index]=0;
     }
    for(index=cols-1;index>=cols;index--)
           {
                     copy=sumc[index+kounter];
                      while (copy!=0){
    
                          sumc[index+kounter]=sumc[index+kounter]+copy%10;
                          copy=copy/10;
                          kounter--;
                        }
            }
    
     for (index = 0; index < rows; index++)
          {
              printf("%d",sumc[kndex]);
    
           }
    Last edited by transgalactic2; 12-25-2008 at 07:35 AM.

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so if i got this array

    |0|0|0|8|19|10|

    the array should look
    |0|0|1|0|0|0|
    it needs to print
    1000

    i cant calculate the sum because it could go over the integer bounders
    it we are told to print the number as it should be and use integers only

    ??
    Last edited by transgalactic2; 12-25-2008 at 07:40 AM.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    Well you could store the number going from the ones place to the biggest place that you want to support, that would make calculations easier.
    Code:
    int input = 123;
    int odometer[4] = {0}; /** largest number supported: 9999 **/
    int i;
    for( i = 0; i < 4 && input > 0; i++) {
       odometer[i] = input % 10;
       input /= 10;
    }
    And you would just reverse the array when you want to print.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i entered this array
    |0|0|0|8|19|10|

    the array should look
    |0|0|1|0|0|0|

    i tried to fit your code
    its not working
    Code:
                   for (kndex=0;kndex<cols;kndex++){
                       input=sumc[kndex];
                 for( index = 0; index < cols && input > 0; index++) {
               sumc[index] = input % 10;
                  input /= 10;
                 }
                   }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    You have been told time and time again, and I am pretty sure that I have given you at least one example: indent your code properly!

    Quote Originally Posted by transgalactic2
    i tried to fit your code
    its not working
    How does it not work? The next time you merely say "its not working", I'm going to say "too bad" and laugh at your incompetence

    If you want to avoid getting laughed at, read the article I linked to in my signature concerning how to ask smart questions.

    Often, it would also help if you posted the smallest and simplest (compilable) program that demonstrates the problem. (Sounds familiar?)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this code is intended
    and basicly what the code needs to do is
    when i entered this array
    |0|0|0|8|19|10|

    the array should look
    |0|0|1|0|0|0|

    Code:
    kounter=0;
    for (index=0;index<cols;index++){
         sumc[index]=0;
     }
    for(index=cols-1;index>=cols;index--)
           {
                     copy=sumc[index+kounter];
                      while (copy!=0){
    
                          sumc[index+kounter]=sumc[index+kounter]+copy%10;
                          copy=copy/10;
                          kounter--;
                        }
            }
    
     for (index = 0; index < rows; index++)
          {
              printf("%d",sumc[kndex]);
    
           }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Quote Originally Posted by transgalactic2
    this code is intended
    Yes, but not properly (e.g., consistently). This code is indented properly:
    Code:
    kounter = 0;
    for (index = 0; index < cols; index++)
    {
        sumc[index] = 0;
    }
    
    for (index = cols - 1; index >= cols; index--)
    {
        copy = sumc[index + kounter];
    
        while (copy != 0)
        {
            sumc[index + kounter] = sumc[index + kounter] + copy % 10;
            copy = copy / 10;
            kounter--;
        }
    }
    
    for (index = 0; index < rows; index++)
    {
        printf("%d", sumc[kndex]);
    }
    Quote Originally Posted by transgalactic2
    and basicly what the code needs to do is
    Yes, you have explained that. Basically, you are trying to do some kind of bignum-style addition where each element of the array is a digit. What you have not explained is how does your current code not work.

    It looks like you are missing a variable to carry over the addition.

    Oh, and whiteflags suggested a little-endian format. Are you going to do it that way, or stick to a big-endian format?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am going to try and fix my code
    it work b taking a member
    taking its remainder by 10
    and adding this number to the first
    then deviding it by 10
    because its an int
    it will keep only the whole part
    and adding it remainder by 10 on the next cell
    etc..

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Quote Originally Posted by transgalactic2
    it work b taking a member
    taking its remainder by 10
    and adding this number to the first
    then deviding it by 10
    That is not quite right. I would expect the algorithm to be something like this:
    Code:
    carry = 0
    loop from the least significant "digit" to the most significant "digit"
        number = digit + carry
        digit = number % 10
        carry = number / 10 (integer division)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in what place i put values in the array?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Quote Originally Posted by transgalactic2
    in what place i put values in the array?
    What do you mean? Do you understand my pseudo-code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant understand this scheme
    its all about changing the array
    and you didnt mention it in your "code"

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,430
    Quote Originally Posted by transgalactic2
    its all about changing the array
    and you didnt mention it in your "code"
    Can you see that each element of the array is a "digit"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ok i will try to build a code uppon this scheme..

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is that ok?
    Code:
    carry = 0
    //loop from the least significant "digit" to the most significant "digit"
    for(index=cols-1;index>=0;index++){
        number = suma[index] + carry;
        suma[index] = number % 10
        carry = number / 10
       }
    Last edited by transgalactic2; 12-25-2008 at 11:54 AM.

Page 1 of 3 123 LastLast
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, 04:50 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Replies: 3
    Last Post: 11-03-2003, 07:55 PM

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