0

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;
9
  • You say "assignment", so I assume you're not allowed to use something like a Queue<T>? Commented May 13, 2022 at 13:11
  • You're right so far. You've replaced the value at myDoubleArray[i] with v as 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 at myDoubleArray[i] which is needed for myDoubleArray[i + 1] in the solution. Commented May 13, 2022 at 13:12
  • Are you allowed to use 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) Commented May 13, 2022 at 13:13
  • When you hand in your assignment, you should mention that arrays are the wrong collection type for this type of operation. Arrays don't like being "shifted". Commented May 13, 2022 at 13:31
  • There are many different ways you could do this.. One would be to just convert it to a list and do an insert. 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. Commented May 13, 2022 at 13:33

1 Answer 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.