Arrays
Having to go in and create a whole bunch of variables can be a huge pain. Typing, typing, typing, typing!!! There is a different way to create multiple variables all at once and it is called creating an Array
Here is an example
int numbers[5];
the line above will create 5 slightly different variable that are an Integer. The five different variables created will be named
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
All of these variables can manipulated in any fashion that we have done in previous lessons. They can be added, subtracted, multiplied, and divided. They can also be modulo'ed, assigned values, inputed into, and so on.
Here is an example
int numbers[5];
the line above will create 5 slightly different variable that are an Integer. The five different variables created will be named
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
All of these variables can manipulated in any fashion that we have done in previous lessons. They can be added, subtracted, multiplied, and divided. They can also be modulo'ed, assigned values, inputed into, and so on.
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
#include
using namespace std;
int main()
{
int vector1[7] = {4, 7, 2, 8, 1, 3, 0};
int vector2[7];
for(int i = 0; i < 7; i++)
// Insert your code here
cout << vector2[i] << ' ';
cout << endl;
return 0;
}
using namespace std;
int main()
{
int vector1[7] = {4, 7, 2, 8, 1, 3, 0};
int vector2[7];
for(int i = 0; i < 7; i++)
// Insert your code here
cout << vector2[i] << ' ';
cout << endl;
return 0;
}
example results
0 4 7 2 8 1 3