'how to replace double in an array with index and value given by user and shifting each element to the right and dropping the last element

I have a problem on replacing the value as I'm not sure which function to use or how to use it.

The question: Create and populate an array of 5 doubles. Ask the user to input a value ‘v’ and an index ‘i’. Your program should replace the value at index ‘i’ in the array with the value ‘v’ while shifting each element to the right and dropping the last element.

This is what I've done so far but it hasn't worked

double[] myDoubleArray = {1, 2, 3, 4, 5};

System.Console.Write(" Please input a value:  ");
int v = Convert.ToInt32(Console.ReadLine());

System.Console.Write(" Please input an index value:  ");
int i = Convert.ToInt32(Console.ReadLine());

myDoubleArray[i] = v;


Solution 1:[1]

You are explicitly saying that this is an assignment (which, in my mind, is OK, as long as you say so). However, I don't like doing your assignments; if I do it, you won't learn anything. Instead, I'm going to help you figure it out. First your code:

double[] myDoubleArray = {1, 2, 3, 4, 5};

System.Console.Write(" Please input a value:  ");
int v = Convert.ToInt32(Console.ReadLine());

System.Console.Write(" Please input an index value:  ");
int i = Convert.ToInt32(Console.ReadLine());

myDoubleArray[i] = v;

Now the assignment, more explicitly:

  • Create and populate an array of 5 doubles.
  • Ask the user to input a value ‘v’ and an index ‘i’.
  • Your program should replace the value at index ‘i’ in the array with the value ‘v’
  • shifting each element to the right and dropping the last element.

You have done the first two parts.

What you need to do is do swap the last two pieces - first make room for the new element, and then, once there is a place for the new value, put it in.

To make room for the new value, you have two cases:

  • The user picks the last index in the array. In this case, you just overwrite the last element and your work is done
  • The user picks any other index (0..N-2). In that case, what you need to do is to loop over the array from the selected index to end of the array (less one), copying from the ith entry to the i+1th entry. While you are doing this, you will likely find out why most programmers think that "off-by-one" issues are the most common bugs there are.
    • Think about this though. You need to loop backwards (i.e. from the end of the array back towards the selected index). If you do it in the normal forwards direction, then you will be overwriting things before you have created any space

Once you have "created some space", then overwrite the appropriate array entry.

Some other comments:

  • You type your array as double[], name it myDoubleArray and initialize it with ints. All your interactions with your users use ints as well. I realize that ints can be implicitly converted to doubles, but you should really decide what you want to do and do it consistently. A double literal is just a number with a decimal point like 3.14152
  • You need to decide if you are going to number your indexes 0..N-1 (which C# likes) or 1..N (which humans like). Write out a hint about this right at the top of your program. Something like:
System.Console.WriteLine($"There is an an array with {myDoubleArray.Length} doubles in it, indexed 1 to {myDoubleArray.Length}.");
  • The way you have your code, if a user enters an incorrectly formatted value, your program will throw an exception. A user fat-fingering data entry is hardly exceptional behavior. Consider using int.TryParse and double.TryParse. They allow you to test for a good value before doing the conversion. That way if the user enters bad data, you can repeat the prompt until he/she enters good data.

Enjoy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Flydog57