c# arrays practice

The following problem is just practice, as some of the code is already given. The instructions are commented out and I've read about arrays and such, but I just don't know what to do with each step.

I have finished a few of the steps (I hope I did them correctly), but any help/explanation of the other steps would be awesome. Not asking for someone to "do my homework" as this is just some practice and I need some guidance.

using System;
namespace QA3_Exercise
{
class ArrayPlay
{
/*Public Function to copy on array elements to another array without using array function
* 1. change the method to take a string array as a parameter
* 2. change the return value of the method declaration to return a string array
*/
public string[] CopyArray(string[] cpArray)
{
/*Enter the code necessary to create another array of the same size and type filled manually
* 3. create a new string array using the supplied parameter array's length to dermine the size
*/
string[] outArray = new string[cpArray.Length];
// 4. Using the for loop with the condition < array.length, set the correct starting value for i to iterate the array
for (int i = 0; i < cpArray.Length; i++)
{
// 5. Using the new array and the index i from the for loop assign each item from the function parameter to the new array
outArray[i] = cpArray[i];
}
// 6. Correct the return statement to return the new copied array
return outArray;
}
/*Public Function to reverse the elements of an array
* 7. Using the coding constructs from the CopyArray function as a template,
* Return a copy of the supplied array, reversed.
* HINT: You will need a counter variable so that you can assign the index in the opposite direction
*/
public string[] ReverseArray(string[] revArray)
{
string[] outArray = new string[revArray.Length];
int counter = revArray.Length-1;
for (int i = 0; i < revArray.Length; i++)
{
//7. a) Assign the counter variable and the i for loop variable to the correct array
outArray[i] = revArray[i];
counter--;// 7. B)/>/> decrement the counter variable
}
return outArray;
}
/*Public Function to create a string representation of the string elements contained in an array
* separated by commas (comma separated values CSV)
*/
public string CSV(string[] myArray)
{
const string comma = ", ";//define a string to hold my separator so I can use the length function later to remove the comma and space
string outString = "";
// 8. Correct the foreach statement declaration to reflect the variable used in my Array
foreach (var s in myArray)
{
outString += s.ToString() + comma;//add the string to itself appending he comma separator string
}
//There is an extra appended comma to the end of the string, use the substring and length functions to remove it.
outString = outString.Substring(0, outString.Length - comma.Length);
return outString;
}
}
class Program
{
static void Main(string[] args)
{
string[] myStudents = new string[] {"Travis","Alex","Robert","Tanner", "Mackenzie", "Kale","Jessica", "Drew", "Paul",
"Tylor", "Wendy", "Joseph", "Tanner", "Colin", "Melissa", "James", "Brady", "Joshua", "Sukhraj",
"David", "Jesse", "Erik","Jeff", "Jordan", "Tyler", "Kody", "Zachary", "Evan","Bradley","Kevin",
"Kevin","Joshua","Erick","Cody","Dylan","Jordan","Nolan"};
//9. Instantiage a new ArrayPlay object using the default constructor (as our class has no constructor defined)
ArrayPlay[] myAP = new ArrayPlay[36];
//10. Create a copy of the myStudents string array using the myArrayPlay object reference with . notation and the CopyArray method
string[] myCopyArrayPlay;
myAP.CopyTo(myCopyArrayPlay, 36);
//11. Change the first element of myCopyArrayPlay to Uppercase using the toUpper function
myCopyArrayPlay[36] = myCopyArrayPlay[36].ToUpper();
//12. Create a copy of the myStudents string array using the myArrayPlay object reference with . notation and the ReverseArray method
string[] myRevArrayPlay = myAP;
//Write out the values of the copyArray and ReverseArray to the console
Console.WriteLine(myAP.CSV(myStudents));
Console.WriteLine();
Console.WriteLine(myAP.CSV(myCopyArrayPlay));
Console.WriteLine();
Console.WriteLine(myAP.CSV(myRevArrayPlay));
Console.ReadLine();
}
}
}

This post has been edited by modi123_1: 25 November 2013 - 07:31 PM
Reason for edit:: please use code tags

Re: c# arrays practice

Posted 26 November 2013 - 10:55 AM

Quote

I have finished a few of the steps (I hope I did them correctly)

Are you able to run, and test, your current code? Does it work?

You should try and get your code in a state so that you can at least run it, as soon as possible. If you wait until you think that you've completed every task, and then try and run it and it doesn't work, it will be much harder to find and resolve the issue(s).

Anyway, for step 7, you have a counter variable but you are not using it. Currently, you are just copying the array in its original order.

To reverse the array you need to:

Copy the last element of this array to the first element of the new array.