views:

650

answers:

4
  1. A mileage counter is used to measure mileage in an automobile. A mileage counter looks something like this 0 5 9 9 8 The above mileage counter says that the car has travelled 5,998 miles. Each mile travelled by the automobile increments the mileage counter. Here is how the above mileage counter changes over a 3 mile drive. After the first mile 0 5 9 9 9

After the second mile 0 6 0 0 0

After the third mile 0 6 0 0 1

A mileage counter can be represented as an array. The mileage counter 0 5 9 9 8 can be represented as the array int a[ ] = new int[ ] {8, 9, 9, 5, 0} Note that the mileage counter is "backwards" in the array, a[0] represents ones, a[1] represents tens, a[2] represents hundreds, etc. Write a function named updateMileage that takes an array representation of a mileage counter (which can be arbitrarily long) and adds a given number of miles to the array. Since arrays are passed by reference you can update the array in the function, you do not have to return the updated array.

Note that the mileage counter wraps around if it reaches all 9s and there is still some mileage to add.

if the input array is {9, 9, 9, 9, 9, 9, 9, 9, 9, 9} and the mileage is 13 the array becomes {2, 1, 0, 0, 0, 0, 0, 0, 0, 0}

i tried to solve this problem but am not being able to solve it , how can i do this ? if anybody could help me i would be thankful

using System;
using System.Collections.Generic;
using System.Text;

namespace updateMileageCounter
{
    class Program
    {
        static void Main(string[] args)
        {
            //System.Console.WriteLine(updateMileageCounter(new int[] { 8, 9, 9, 5, 0 }, 1));
            //System.Console.WriteLine(updateMileageCounter(new int[] { 8, 9, 9, 5, 0 }, 2));
            foreach (int a1 in updateMileageCounter(new int[] { 8, 9, 9, 5, 0 }, 13))
            {
                System.Console.WriteLine(a1);
            }
        }

        static int[] updateMileageCounter(int[] a, int miles)
        {
            int remain = miles;
            int[] result = new int[a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                result[i] =a[i] + remain;
                if (i == a.Length - 1 && result[i] > 9)
                {
                    remain = result[i] - 9;
                    for (i = 0; i < a.Length; i++)
                    {
                        result[i] = 0;
                        a[i] = 0;    
                    }
                    if (remain == 1)
                    {
                        return result;
                    }

                    for (i = 0; i < a.Length; i++)
                    {
                        result[i] =a[i] + remain;
                        if (result[i] > 9)
                        {
                            decimal res = result[i] / 10;

                            result[i] = (result[i] - 1)-10;                           
                        }
                    }
                    return result;
                }
                if (result[i] > 9)
                {
                    remain = result[i] - 9;
                    result[i] = 0;
                 }
                else if (result[i] < 9 || result[i]==9)
                {
                    remain = 0;
                }
            }
            return result;
        }
    }
}
+3  A: 

Please don't vote this up or use it as an answer to your question. I don't think your teacher would like it very much.

  1. Reverse and convert your int[] array to a char[] array and pass it to a new String().
  2. Parse your string into an Integer and add 1 to it.
  3. Convert your Integer back to a String
  4. Loop through each character in your string, cast it to an Integer, and put it back into an int[] array.

:)

Cory Larson
Thankyou , i got the idea .... thankyou =)
jarus
Just keep in mind that--as Cory said--your teacher may not like this answer.
Fake Code Monkey Rashid
Actually, I think this is a heck of an idea. Unless the teacher specifically prohibits this type of program, I'd definitely go for it. (Although I would consider talking with the prof about it first to make sure that they're okay with it...) But, in general, I think that this thinking outside the box type of answer is the way good programmers look to solve problems.
Beska
Some professors I had in college would have loved this answer, but like I said... use it at your own risk.
Cory Larson
i'm trying to solve these questions by my self and i don't have any professors to refer to =)
jarus
+1 for "Please don't vote this up"... but you were expecting that. 8-)
RichieHindle
A: 

The problem that I see from your code is that you're trying to work within the bounds of the array. Instead of trying to manipulate the array, which is really hard, pull the values out of the array and then manipulate them.

So for example, write a way to convert

{ 8, 9, 9, 5, 0 } to 05998

Cory suggests one method, the other is to traverse the array and multiply by powers of 10.

Now that you've got a value that's easier to work with, add the mileage and perform a modulus operation on the value so that if it exceeds 99999 it will wrap around.

The final step is to convert your number back to an array.

Some pseudo-code:

private static void updateMilage(int[] a, int miles)
{
    convertFromArrayToInteger()
    addMilageWithAModulus()
    convertFromIntegerToArray()
}
Gavin Miller
+1  A: 
Malcolm
Would that not be better (ie more compatible) as odometer.Length rather than odometer.Count() ?
sgmoore
@sgmoore - good catch, and likely faster as well (since Count() is an IEnumerable extension). I have updated the code to reflect this.
Malcolm
A: 

Seriously, arrays?

public class Odometer
{
    private int digits;
    private int maxReading = 1;

    public int Mileage
    { get; private set; }

    public Odometer( int digits )
    {
        if ( digits <= 0 )
            throw new ArgumentOutOfRangeException("digits");

        this.digits = digits;
        for ( int d = 0; d < digits; maxReading *= 10, d++ );
    }

    public void Add( int miles )
    {
        if ( ( Mileage += miles ) < 0 )
            do { Mileage += maxReading; } while ( Mileage < 0 );
        else
            Mileage %= maxReading;
    }

    public override string ToString()
    {
        return Mileage.ToString().PadLeft(digits, '0');
    }
}

or in self-contained static version:

    static int UpdateMileage(int digits, int mileage, int miles )
    {
        int maxReading = 1;

        for ( int d = 0; d < digits; maxReading *= 10, d++ );

        if ( ( mileage += miles ) < 0 )
            do { mileage += maxReading; } while ( mileage < 0 );
        else
            mileage %= maxReading;

        return mileage;
    }