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
Queue<T>?myDoubleArray[i]withvas the assignment asks, but you need to manually shift the items to the right as/before you do. Right now, you've lost the original value atmyDoubleArray[i]which is needed formyDoubleArray[i + 1]in the solution.Array.Copy()? That handles overlapping segments of the same array and would allow you to do this without writing your own loop. (I bet they want you to write a loop, though)List<double> dbList = new List<double>(myDoubleArray); dbList.Insert(i,v);. Though somehow I feel this is not the point of the assignment and that they want you to reconstruct the array with a for loop.