The simplest of probelms..any ideas ?

This is a discussion on The simplest of probelms..any ideas ? within the C Programming forums, part of the General Programming Boards category; Ok ...now I am a complete rookie to programming...I am doing a college project.No you dont have to do it ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    The simplest of probelms..any ideas ?

    Ok ...now I am a complete rookie to programming...I am doing a college project.No you dont have to do it for me....I so love coding ever since I started that I would do it myself even if its gonna take me 12 hrs today...

    I however need some help...in the project...I am in need of comparing an array to an Integer.

    for example an array b = [407] to an integer variable xyz

    So how do I convert the above array elements into one integer as in "int a = 407" that way i figures i could then just compare 407 to xyz using library functions.(I do not know what XYZ is untill user inputs it)

    If there is any other way to do this...pls free to enlighten me....

    Awaiting reply with baited breath

    Greatly appreciate any help

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Your confusion probably is related to the fact you don't understand the problem. Comparing an array to an integer is rather abstract. To some people it could mean you just want to see if the integer is an element of said array. Is this the case? Then you need a simple loop to check each element.

    Remember, an array's elements are simply the type that the array is declared with. That means if you have an array declared like this:

    Code:
    int array[100];
    That means array[0] is just an int, literally, like any other int.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    34
    Thanks MacGyver.....i think I am having difficulties in expressing what my problem is..

    I am not attempting to compare an array to an integer like one compares one integer with another integer....

    Let me try and explain again....

    1)I have an integer array with 3 integer elements in it.Namely [4 0 7]

    2)Later in my code i have an integer variable xyz = 751

    3)Now i need to see if the array elements all put together...i.e the number 407 is equal to 751 or not.If i were to store user input (i.e the no 407) in an integer variable say "int a" then i wouldnt have problems comparing "int a" to "int xyz" But that I cannot do...the user input has to be stored in an array and then I need to compare the number in array to the variable XYZ...

    4)Its like the 0th element ,the 1st elment and the 2nd element of my array (in this case 407 respectively) together is a number 407...now compare this to any other variable

    Thanks again to all for any assistance

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You have two options.

    1. Do it the math way of dividing by 10, 100, etc. on the int in question and disect it digit by digit, comparing it with the array.
    2. Convert the int to a string via sprintf(), snprintf(), itoa(), or some other means, and then converting (by subtracting '0' from the char) and comparing each character to each digit of the array.


    I would go with the last option if you want to be able to handle this gracefully and yet compare an arbitrary number of digits. If you know the digits will be fixed at 3, let's say, then the first option might be more appealing since you can hardcode the division.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    You can have a simple loop like this one to transform the array into the integer you are looking for this way:

    Code:
    for all elements of the array starting at 0
        new_integer *= 10
        new_integer += element i of array
    Then you can compare new_integer with xyz.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    hi!
    i was given a similar problem at my college and
    the method i followed was as follows
    ( It is the opposite Macgyver's 1st method multiplying instead of dividing,
    similar to desolation's method)
    Use the following loop

    Code:
    integer=0;
    for(i=0;i<n;i++) //n is the number of elements in the array 'a'
    {
     a[i]*=pow(10,n-1-i);
    integer+=a[i];
    }
    Compare integer with your required value

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    34

    Thumbs up

    wow thanks guys.....I have come to simply love coding beacause of the various ways in which a single problem can be solved...

    Once again thanks for the replies..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 09:37 PM
  3. Company Name Ideas
    By brunomiranda in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-16-2003, 05:15 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 01:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM

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