The problems
Create a console-based program whose Main() method
declares three integers named fi rstInt, middleInt, and
lastInt. Assign values to the variables, display them,
and then pass them to a method that accepts them as
reference variables, places the fi rst value in the lastInt
variable, and places the last value in the fi rstInt variable.
In the Main() method, display the three variables again,
demonstrating that their positions have been reversed.
Save the program as Reverse3.cs.
b. Create a new program named Reverse4, which contains
a method that reverses the positions of four variables.
Write a Main() method that demonstrates the method
works correctly. Save the program as Reverse4.cs.
I got the first one
int first = 33;
int middle = 44;
int last = 55;
Console.WriteLine("Before the swap the first number is {0}", first);
Console.WriteLine("Before the swap the middle number is {0}", middle);
Console.WriteLine("Before the swap the last number is {0}", last);
Swap(ref first, ref middle, ref last);
Console.WriteLine("\n============AFTER===THE===SWAP======================");
Console.WriteLine("After the swap the first is {0}", first);
Console.WriteLine("After the swap the middle is {0}", middle);
Console.WriteLine("After the swap the last is {0}", last);
}
public static void Swap(ref int firstInt, ref int middleInt, ref int lastInt)
{
int temp;
temp = firstInt;
firstInt = lastInt;
lastInt = temp;
But second one gives me a problem
Loading
Prime bPosted Jan 9, 2012, 8:25 PM
If you have other way to solve this problem, please show me how you solved it.
int first = 111;
int second = 222;
int third = 333;
int fourth = 444;
Console.WriteLine("Numbers in normal order: {0},{1},{2},{3}", first, second, third, fourth);
Reverse(ref first, ref second, ref third, ref fourth);
Console.WriteLine("Numbers in inverse order: {0},{1},{2},{3}", first, second, third, fourth);
}
public static void Reverse(ref int first1, ref int second2, ref int third3, ref int fourth4)
{
int temp, temp1;
temp = first1;
first1 = fourth4;
temp1 = second2;
second2 = third3;
third3 = temp1;
fourth4 = temp;