Help with array

This is a discussion on Help with array within the C Programming forums, part of the General Programming Boards category; Hi. I need some help with an arrays. I have an array: Code: array[3] = {1, 2, 3} , and ...

  1. #1
    pedofil
    Guest

    Help with array

    Hi. I need some help with an arrays. I have an array:
    Code:
    array[3] = {1, 2, 3}
    , and i want to make the number from the numbers in the array, in this case 1 * 100 + 2 * 10 + 3 * 1 = 123. Cone somebody help me please?
    Thank you a lot.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You haven't said what data type in is the array.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    pedofil
    Guest
    It's integer.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Easy if you know how decimal works,

    Work with positional notation, and each position is a power of 10 (decimal).
    For example 123 is
    (right-to-left): 10^0 * 3 + 10^1 * 2 + 10^2 * 1

    So, in sort traverse your array backwards, and add each element * 10^i (Where i is the iteration of your loop) together.

    You'll need pow(): http://www.cplusplus.com/reference/c...cmath/pow.html

  5. #5
    Beginner leiming's Avatar
    Join Date
    Jan 2008
    Location
    Fujian, China
    Posts
    25
    Maybe using string is a way
    Code:
    int array[3] = {1, 2, 3}, num;
    char temp[4];
    for(i = 0; i < 3; i++)
    	temp[i] = array[i] + '0';
    temp[i] = 0;
    num = atoi(temp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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