Complex Arrays
In this section you are going to learn about more complex arrays other than single arrays.
You are going to learn about 2 dimensional rectangular arrays and Jagged arrays.
You are going to learn about 2 dimensional rectangular arrays and Jagged arrays.
Assignment
In this assignment you will begin to manipulate arrayed variables.
Take a look at the code below - it declares two equally sized vectors.
The former is initialized, the latter isn't.
We want the second vector to store the same values as the first one, but in a different order: imagine that all the values have been moved one cell to the right, while the last element has gone to the first position.
We can say that the vector has been rotated to the right. Warning: you must use the for loop for it. Don't use single assignments - they may work but they'll reflect badly on you and on your programming skills.
Here is an example of some code to help you brain juices to start flowing
int variable1 = 1, variable2 = 2, auxiliary;
auxiliary = variable1;
variable1 = variable2;
variable2 = auxiliary;
The code above moves the value of variable1 into the variable auxiliary first, then moves variable2 into variable1, then lastly auxiliary gets moved into variable2. Auxiliary only serves as a temporary place for a value while everything else is switched around
Here is some more code
for(int i = 0; i < 2; i++)
{
auxiliary = numbers[i];
numbers[i] = numbers[4 – i];
numbers[4 – i] = auxiliary;
}
The code above basically does the same thing as before except it is wrapped into a for loop that will continue to work until the condition i<2 proves false. Inside of the for the loop we start off with setting i=0. i simply serves as our counter and this actions is only done once. The end of each loop is i++ and this will increment our counter every time the loop is executed.
Sooo the loop will happened 2 times (i starts off at 0, the loop runs, and i is incremented to 1, the loops runs, and i gets incremented again to 2, we exit the loop this time because i<2 is no longer true
Take a look at the code below - it declares two equally sized vectors.
The former is initialized, the latter isn't.
We want the second vector to store the same values as the first one, but in a different order: imagine that all the values have been moved one cell to the right, while the last element has gone to the first position.
We can say that the vector has been rotated to the right. Warning: you must use the for loop for it. Don't use single assignments - they may work but they'll reflect badly on you and on your programming skills.
Here is an example of some code to help you brain juices to start flowing
int variable1 = 1, variable2 = 2, auxiliary;
auxiliary = variable1;
variable1 = variable2;
variable2 = auxiliary;
The code above moves the value of variable1 into the variable auxiliary first, then moves variable2 into variable1, then lastly auxiliary gets moved into variable2. Auxiliary only serves as a temporary place for a value while everything else is switched around
Here is some more code
for(int i = 0; i < 2; i++)
{
auxiliary = numbers[i];
numbers[i] = numbers[4 – i];
numbers[4 – i] = auxiliary;
}
The code above basically does the same thing as before except it is wrapped into a for loop that will continue to work until the condition i<2 proves false. Inside of the for the loop we start off with setting i=0. i simply serves as our counter and this actions is only done once. The end of each loop is i++ and this will increment our counter every time the loop is executed.
Sooo the loop will happened 2 times (i starts off at 0, the loop runs, and i is incremented to 1, the loops runs, and i gets incremented again to 2, we exit the loop this time because i<2 is no longer true
Initial code
using System;
class forLoopArray
{
public static void Main()
{
var vectorFirst = new int[7] { 4, 7, 2, 8, 1, 3, 0 };
var vectorSecond = new int[7];
for (int i = 0; i < 7; i++)
{
// insert your code here
Console.WriteLine(vectorSecond[i]);
}
}
}
Example results
0 4 7 2 8 1 3