It turns out that I need to design a program that performs the following actions:
-Request an integer from the user.
The program then asks for as many integers as the previously entered number and stores them in a vector.
The program then asks for two more integers: the first corresponds to a position and the second to a new value to insert the vector.
Finally the program has to insert the new element to the vector in the introduced position. If the entered position is negative, the new element will be inserted at the first position of the vector. If the number entered is greater than the possible positions in the list, the new element will be inserted at the last position in the array.
I would like to resolve the errors keeping the structure of the program.
It gives me 2 errors:
Severity Code Description Project File Line Suppression State Error CS0021 Cannot apply indexing with [] to an expression of type 'method group' insert C:\Users\Usuario\source\repos\insert\insert\Program.cs 25 Active
Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'int[]' to 'int' insert C:\Users\User\source\repos\insert\insert\Program.cs 63 Active
My code is the following:
using System;
namespace insert
{
class Program
{
static void Main(string[] args)
{
int elements, position, change;
Console.WriteLine("Insert total elements: ");
elements = int.Parse(Console.ReadLine());
int[] numbers = new int[elements];
for(int i = 0; i<elements; i++)
{
Console.WriteLine("Insert integer value: ");
numbers[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Insert position to insert element: ");
position = int.Parse(Console.ReadLine());
Console.WriteLine("Insert integer value to insert: ");
change = int.Parse(Console.ReadLine());
Console.WriteLine("The final values for the array are: ");
for(int i = 0; i <= elements; i++)
{
Console.WriteLine(insert[i]);
}
}
static int insert(int[] numbers, int change, int position, int elements)
{
int[] newnumbers = new int[elements + 1];
if(position < 0)
{
newnumbers[0] = change;
for(int i = 1; i <= elements; i++)
{
newnumbers[i] = numbers[i - 1];
}
}
if (position > elements)
{
newnumbers[elements] = change;
for (int i = 0; i < elements; i++)
{
newnumbers[i] = numbers[i - 1];
}
}
else
{
newnumbers[0] = change;
for (int i = 0; i <= elements; i++)
{
if (i == position)
{
newnumbers[i] = change;
i++;
}
else
{
newnumbers[i] = numbers[i];
}
}
}
return newnumbers;
}
}
}
My answer is focused on eliminating compilation errors, I did not check the logic of the program or if it produces the expected results.
I could see that in addition to the bugs the question focuses on, using
int.Parse(Console.ReadLine());
will cause an exception if the user types anything other than numbers.The errors are caused by the following:
insert
without passing it arguments, and the method is declared to return an integer and should be an array of integersint[]
.newnumbers
it's an array but the method declaration says to return an integer.The method declaration
insert
should be as follows:In the method
Main
, before thefor
one that returns the result, you must call the methodinsert
passing the values to each parameter and assigning the result to a variable (for this example I called itresultado
).Then, use the new variable
resultado
inside thefor
(I'm leaving out part of the code to keep the focus on the area of interest)It is true that the new variable
resultado
slightly alters the code of your program and it is also true that you can call the methodinsert
inside thefor
, but it does not make sense to repeat the logic of the methodinsert
for each element of the array to be displayed.