Can someone explain what form data has to take to work on this simple swapping function?
public static void exchange (int[] data, int m, int n)
{
int temporary;
temporary = data [m];
data [m] = data [n];
data [n] = temporary;
}
from link:
http://anh.cs.luc.edu/170/notes/CSharpHtml/sorting.html
>>351963
An array of ints at least as long as m and at least as long as n, no?
>>351984
How?
I've tried it like this:
int m = 2;
int n = 10;
int[] data = new int[] { 3, 5, 4, 6 };
But this comes out:
"
Error CS7036: There is no argument given that corresponds to the required formal parameter 'data' of 'Program.DoStuff(int[], int, int)'
"
>>352202
Bump. Still need help with this.
>>352507
Bump, please help.
It takes an array of integers as its first argument and two integers. however, both integers must be in the range of 0 to n where n is the number of entrys in the array minus 1. YOu got the error because your array only has 4 entries and you tried to access the tenth one with your m.
so to make it work with m=2 and n=10 your array has to at least have eleven entries (because arrays start at 0)
what does your exchange/DoStuff function call look like?
>>352845
>>352882
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
int m = 2;
int n = 10;
int[] data = { 3, 5, 4, 6, 8, 7, 9, 4, 5 };
DoStuff();
//Exhange();
Console.ReadLine();
}
public static void DoStuff(int[] data, int m, int n)
{
int temporary;
temporary = data [m];
data [m] = data [n];
data [n] = temporary;
}
}
Here.
>>352920
You're not calling DoStuff(...) with any parameters.
C doesn't look at your variable names, notice they're the same as the ones in the function definition, and just put them in for you: you have to tell it.
DoStuff(data,m,n);
If you've have just done like >>352845, in the first place, this thread could have been done and dusted in seconds rather than days.
>>352923
This is what happens when kids try to walk before they can crawl. The fucker is looking at sorting algorithms and doesn't even know how code actually works.
>>352920
>DoStuff();
thats what i tought
>>352920
Also data doesn't have enough items in it for n to equal 10. You have 9 items in there, it needs 1 more, and you need to subtract 1 from 10 because arrays count from 0, not from 1.
>>353108
That's not an issue that affects compilation: in C# the compiler does not check array bounds, and is in any case not smart enough to perform that kind of static analysis.
C# does do runtime bounds-checking, though, so OP would be told that by the .NET runtime the first time he pressed "run".