Engr 123 Name_____________________ Hour Exam 2 March 2, 2015

advertisement
Engr 123
Hour Exam 2
Name_____________________
March 2, 2015
1. Write a method which accepts two 1-dimensional int arrays named a and b. The method
should check if a and b are the same length. If they are not, it should return false. If a is the
same length as b, the method should swap array a with array b and return true. A sample calling
statement is shown in the sequence below:
if(SwapArrays(a, b))
Console.Writeline("Arrays swapped.");
2. Show how to create a two-dimensional array of doubles that has 14 rows and 63 columns.
3. Assuming that the array in problem 2 has been created, write a short sequence to initialize
each of its elements to the product of its row times its column.
4. Draw a circle around those items A to D below which are valid overloads for the method
whose first line is: private static int F1(int a, double b)
A) private static int F1(int a, double b, string c)
B) private static double F1(double a, double b)
C) private static int F1(double x, int y)
D) private static void F1(int a, double b)
5. Show what is printed by the following sequence:
int [] a = {1, 2, 3};
Method1(a[2]);
Console.Writeline(a[2]);
…
Printed result________
private void Method1(int i)
{i = 9;
}
6. Show what is printed by the following program and fill in the memory map below.
{static void Main(string[] args)
{int a = 12, b = 9, c;
c = MyMethod(a, ref b, out c);
Console.WriteLine("{0}, {1}, {2}", a, b, c);
}
private static int MyMethod(int a, ref int b, out int c)
{int x;
x = a + b;
c = b/2;
b++;
Console.WriteLine("{0}, {1}, {2}", a, b, c);
Console.WriteLine(x);
return x;
}
}
Method1
Main
Data
Printed Output
7. The following sequence creates a 2-dimensional array of ints and fills it with data. In the
space below fill in the correct values that will be in the array after the program runs.
{int[,] a = {{1,2,3}, {3,2,1}};
int r, c;
for(r=0;r<2;r++)
{for(c=0;c<3;c++)
{if(a[r,c] == 1)
a[r,c] = 0;
else
a[r,c] = 1 + 2*r + 3*c;
}
}
}
8. Explain how using methods in programs can lead to programs that require much less memory
space.
9. The following statements create an array and fill it with random numbers. Append a sequence
to the existing code to find and print the average of the array values.
Random r = new Random();
int [] a = new int[203];
int i;
for(i=0;i<203;i++)
a[i] = r.Next(1, 12);
10. Write a short sequence of code in C# which will produce 1000 random integers which have
value 1 to 500 and store them in an array called aData. Write a second sequence which goes
through all of the values in aData and changes all of the values which are greater than 250 to 0.
11. Write a method called Input which inputs and returns an int which varies from 1 to 5 from
the user. Your method should prompt the user to input an int between 1 and 5. If the number
received from the user ranges from 1 to 5 the method should return that number. If the number
received from the user is outside of that range, you should print the word "Error" and prompt the
user again for an input in the range 1 to 5. You method should not return until it receives an int
in the range 1 to 5.
Download