[SOLVED] Give a string of numbers on a single line, separated by a space

Issue

Give a string of numbers on a single line, separated by a space.
N operations follow to move an element of the string to the first position. Each move is specified on a line where the index of the element to be moved to the first position is entered. All other elements of the string remain in the same order.

Print the modified string to the console after all move operations have been applied to the first position. Then on the next line print True if the elements of the string are in ascending order or False otherwise. The code works good now.

Example:

For input data:

1 2 3 4 5 6
2
1
5

The console will display:

6 2 1 3 4 5

Here is the code:


using System;
namespace MoveFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = ReadValuesList();
            int[] positionsToMove = ReadPositions();

            for (int i = 0; i < positionsToMove.Length; i++)
                MoveFirst(values, positionsToMove[i]);

            PrintValuesList(values);
            Console.WriteLine(CheckIfSortedAscending(values));
            Console.Read();
        }

        static bool CheckIfSortedAscending(int[] values)
        {

            var result = true;
            for (int i = 0; i <= values.Length - 2; i++)
            {
                if (values[i] > values[i + 1])
                {
                    result = false;
                }
            }
            return result;
        }

        static void MoveFirst(int[] values, int index)
        {

            if (index == 1)
            {
                int aux = values[index - 1];
                values[0] = values[index];
                values[index] = aux;
            }
            else
            {
                int aux = values[index];
                for (int i = index; i > 0; i--)
                {
                    values[i] = values[i - 1];
                }
                values[0] = aux;
            }
        }

        static int[] ReadPositions()
        {
            int positionsNumber = Convert.ToInt32(Console.ReadLine());
            int[] positions = new int[positionsNumber];

            for (int i = 0; i < positionsNumber; i++)
                positions[i] = Convert.ToInt32(Console.ReadLine());

            return positions;
        }

        static int[] ReadValuesList()
        {
            string[] inputValues = Console.ReadLine().Split(' ');
            int[] values = new int[inputValues.Length];

            for (int i = 0; i < values.Length; i++)
                values[i] = Convert.ToInt32(inputValues[i]);

            return values;
        }

        static void PrintValuesList(int[] valuesList)
        {
            for (int i = 0; i < valuesList.Length; i++)
                Console.Write(valuesList[i] + " ");
            Console.Write('\n');
        }
    }
}

Solution

First of all, you get a compile error because not all code paths return a value, as it says. Imagine your array has lenght 0, the program will never go into the loop. What will return on that case? You should also return something outside the loop, but anyway that’s not what you want. Your code will only check the first two values, because you have set it to return something after the first comparison. What you need is:

var result = true;
for (int i = 0; i <= values.Length-2; i++)
{
  if (values[i]>values[i+1])
  {
    result = false;
  }
}
return result;

That will check the whole array, because it returns a value after the check. Also i changed the comparison in the loop. Imagine an array with 10 elements. When i=8, you need to compare the elements 9 and 10 (values[8], values[9]). Finally, that will work for the edge case that the array has 2 elements and it will return true in case that it has one element.

Edit: You could also do the following, which is more efficient:

for (int i = 0; i <= values.Length-2; i++)
{
  if (values[i]>values[i+1])
  {
    return false;
  }
}
return true;

but because I assume that is some kind of homework, the first way is probably better.

Answered By – spyros__

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published.