Why does this loop end up reversing the array? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
/*
Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3}
yields {2, 3, 1}.
RotateLeft3([1, 2, 3]) → [2, 3, 1]
RotateLeft3([5, 11, 9]) → [11, 9, 5]
RotateLeft3([7, 0, 0]) → [0, 0, 7]
*/
public int[] RotateLeft3(int[] nums)
{
int temp = nums[0];
for (int i = 0; i < nums.Length -1; i++)
{
nums[i] = nums[i + 1];
}
nums[nums.Length - 1] = temp;
return nums;
}
This is an exercise question that I'm having trouble translating into plain English, I'm not following how this ends up rotating* the array after the loop finishes.

This is not reversing the array. It is shifting all the entries to the left one, and wrapping the first element back to the end of the array.
The first operation saves the first item.
The for loop takes each item and copies the next item over the top of it.
The last operation puts the saved first item over top the last item.

The code as posted appears to be rotating the values through the array.
Translating it into words,
Remember the first item
Move the second item into the first position, 3rd into 2nd etc (repeat for all remaining items)
Put the remembered item in the last position.
It doesn't appear to 'reverse' the array.

Related

How to change a list based on another list [duplicate]

This question already has answers here:
How to sort a list so that the same changes would happen in another list?
(3 answers)
Closed 3 months ago.
I have a question:
For example I had a list with 132 and a second list 123
I would want my 132 list.sort but the 123 list to sort like 132:
was:
132
123
After
123
132
Not switch place but if the first element changes position in list 1 so should list 2, please help.
This answer involves writing (or editing an existing) sorting algorithm.
In the example below, I've illustrated a way in Javascript to enhance a simple Bubble Sort algorithm to include a second array to manipulate aswell.
I know the question is for C#, but the same concepts apply.
The arrays need to have the same length, otherwise you will get errors.
// Bubblesort algorithm stolen from https://www.geeksforgeeks.org/bubble-sort/
// edited to include a second array (arr2) that mirrors the sorting of the first array
function swap(arr, xp, yp)
{
var temp = arr[xp];
arr[xp] = arr[yp];
arr[yp] = temp;
}
function bubbleSort( arr, arr2, n)
{
var i, j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(arr,j,j+1);
// swap the elements of the second array aswell
swap(arr2,j,j+1);
}
}
}
}
// just a function for printing
function printArray(arr, size)
{
var i;
for (i=0; i < size; i++)
document.write(arr[i]+ " ");
document.write("<br />");
}
// the two arrays
var array1 = [5, 1, 4, 2, 8];
var array2 = [2, 3, 4, 5, 6];
var n = 5;
document.write("UnSorted arrays: <br />");
printArray(array1, n);
printArray(array2, n);
bubbleSort(array1, array2, n);
document.write("Sorted arrays: <br />");
printArray(array1, n);
printArray(array2, n);
If you run the above snippet, you see that the '1' in the second position of the first array has moved to the first position.
The second array had a '3' in the second position, which is also swapped to the first position, like the first array did.
Long story short: The second array mirrors the sorting of the first array.

Printing numbers 1 to 100 C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am new here and in C# and I dont't know how to figure out that example..Printing numbers 1 to 100 that way "100, 1, 2, 99, 98, 3, 4, 97,....52, 49, 50, 51". I have a course work and should write it befoure 25.10..Thank you all!
for (int i = 1; i < 51; i+=2)
{
Console.WriteLine(101-i);
Console.WriteLine(i);
Console.WriteLine(1+i);
Console.WriteLine(100-i);
}
EDIT: just i want to add that i use (1+i) // (100-i) because if I use i-- or i++ inside the writeline, it will execute the ++ or -- after the print message
By using a lower and a upper variable and a while-loop, you can do it stright forward without any calculations:
int lower = 1, upper = 100;
while (lower < upper) {
Console.WriteLine(upper--);
Console.WriteLine(lower++);
Console.WriteLine(lower++);
Console.WriteLine(upper--);
}

C# Console Application, how to make all results equals totalResult? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How do I make the last result equal the first, second, third, and fourth result combined?
If all results are 10 points except the last one, then the last one should be 40 points. The last result is the total result.
int[] result = new int[5] { 0, 0, 0, 0, 0 };
Here you go
int[] result = { 1, 2, 3, 4, 0 };
result[result.Length - 1] = result.Take(result.Length - 1).Sum(); // 1 + 2 + 3 + 4 = 10
//result = { 1, 2, 3, 4, 10 };

c# how to delete an item in an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Console.WriteLine ("Enter the number of book to delete (1 to {0})", amount);
int posToDelete = Convert.ToInt32 (Console.ReadLine ())-1;
for (int i = posToDelete; i < amount - 1; i++)
b [i] = b [i + 1];
amount--;
// hi! i am new to programming... could anyone please explain this to me in detail
// * what is the use of -1 after the readline
// * explain me the loop plz
//link of the question
//http://practiceexercisescsharp.blogspot.com.es/2013/05/411-books-database.html
fisrt question:
An array is start from 0.
for example: There is an array b includes 4 elements.
b[4] = {1,2,3,4};
so b[0] is 1 and b[3] is 4.
there is no b[4].
if you want to delete the second item(which element is 2) ,you should delete b[1].
so the position is 1.
This is why we use -1 after the readline.
second question:
for (int i = posToDelete; i < amount - 1; i++)
{
b [i] = b [i + 1];
amount--;
}
int = posToDelete is the position of the item we want to delete.
we use the next item to replace it.
this is b[i] = b[i+1];
because we delete one item,the amount need -1;
we also use b[4] for example:
if we delete 2.
initial:[1,2,3,4] amount=4;
loop begin: [1,2,3,4] posDelete is 1,amount is 3;
b[1]=b[2];we use 3 to replace 2;
b[2]=b[3];we use 4 to replace 3;
i is increasing until i is 3;the loop end.
the new array is [1,3,4].
Chances are the indexer of your book array is layout like the following:
book[0] = "Book1" //Where user inputs 1 and then subtract 1 from it to access index 0
Consider the following examples:
book[0] = "Book1" //Book input is 1 less 1 = index of 0
book[1] = "Book2" //Book input is 2 less 1 = index of 1
book[2] = "Book3" //Book input is 3 less 1 = index of 2
Of course the logic of deleting the book is in question, since we are not sure how the book is really arranged in Arrays or Collections. But in your case, you are specifying the position so it can be said that position 1 is
equal to index 0.

Finding the max sum of successive random numbers in an array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array of 25 random integers between -100 and 100.
For example my array can look like this:
-10, 23, 19, -11, 3, -9, -8, 4, 10, 20, 30, 40, 50, -6, -2, 1, -9, 8, -6, 20, -21, -3, -2, -4, -7
I wish to write a method that receive my array as a parameter, and prints out the following: the first index, the last index, and the sum of the successive elements between them, that will give the possible maximum sum.
For my example the output will be: first index: 1 (value: 23), last index: 19 (value:20), total sum: 177
I don't know how I should deal with this problem. I wrote a code, but it's very complex and unefficient because I used a list to store all the possible sums.
Can you please show a Pseudo-code for this problem, or code in c#?
This is a well known problem in literature, known (among many other names) as Maximum Subarray Problem.
You can find more infos, and a pseudo code with a possible solution here: http://en.wikipedia.org/wiki/Maximum_subarray_problem
Since your problem seems to be that of finding a viable algorithm and not implementing it, the following pseudo-code should be enough:
def max_subarray(A):
max_ending_here = max_so_far = 0
for x in A:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
You don't need to keep all the possible sums, as you are only interested in the largest.
Loop throught the combinations, and look for any sum that is larger than what you have so far.
You can make use of the fact that the sum of any range of elements is the sum of the elements excluding the last item plus the last item. I.e you don't need to loop through the elements to calculate each possible sum, because you already have the sum of the range that is one element less:
public static void ShowLargestSum(int[] arr) {
int largest = arr[0], start = 0, end = 0;
for (int i = 0; i < arr.Length; i++) {
int sum = 0;
for (int j = i; j < arr.Length; j++) {
sum += arr[j];
if (sum > largest) {
largest = sum;
start = i;
end = j;
}
}
}
Console.WriteLine("first index: {0} (value: {1}), last index: {2} (value:{3}), total sum: {4}", start, arr[start], end, arr[end], largest);
}
Execution time on my computer for your example array: 0.17 ms.

Categories

Resources