Valid Operations with Arrays

I'm on the part which shows valid operations with arrays, and this one is confusing me. What is the purpose of the nested bracket on the first part. What specifically is thar referring to, why not just have a in one set of brackets?

1
2
3
4
array[0] = a;
array[a] = 75;
b = array [a+2];
array[array[a]] = array[2] + 5;


Also when I try to compile this example I get an error saying 'a' was not declared in this scope. Is that because the array is of type int?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>>
using namespace std;
int main ()
{
int billy [5] {0,1,2,3,4};
billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy [a]] = billy[2] + 5;
cout << a;
	return 0;
}
Last edited on
That code makes no sense. Where did you get it from?


There are very few valid operations on arrays. You can:

  • create them
    int xs[100];

  • create and initialize them
    int xs[100] = { 1, 2, 3, 4 };

  • access elements
    xs[2] = 7;
    std::cout << xs[2] << "\n";

  • get its size in bytes
    auto bytes_in_xs = sizeof(xs);

  • calculate its size in elements
    auto elts_in_xs = sizeof(xs) / sizeof(xs[0]);

  • turn it into a pointer
    int* ys = xs;
    some_function( xs );

C++ adds a couple of layers to make some of these things easier, but not much.
In C11, you can also declare an array with its length set to the value of a variable:
1
2
int n = 100;
int xs[n];

Hope this helps.
I got it from the c++ tutorial on this site, page 56 in the pdf.
Last edited on
Also I've gotten further in the chapter and I'm now looking at the code example, and I am confused:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>>
using namespace std;

int array [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for (n=0 ; n<5; n++)
{
	result += array[n];
}
cout << result;
	return 0;
}


I understand that result is the sum of all the elements in the array, but i'm confused by result += array[n]; how is the variable n being plugged into the array to describe which position the array is at, and how does c++ know to add each element position to result, when it's just showing n? Is n pointing to each element individually?



Last edited on
An array is just a list of values, all of the same type. The list is conveniently indexed, starting at zero.

Hence, in the array

    16 2 77 40 12071

There are five elements, numbered 0 through 4.

Element 0 is 16.
Element 1 is 2.
Element 2 is 77.
Etc.

Hope this helps.
im plugging numbers into result += array[n];
like 0 1 2 3 and 4, instead of the variable n, and result's value makes no sense when i run the program. when i put 0, the result is 80, when i put 1 the result is 10, and these are obviously not the values of the first two elements in the array. What is going on?
Last edited on
The result makes no sense because that code makes no sense.

Where did you get it?
What is it supposed to do?
If you choose to plug 0 into the variable n, the final result will be 80. The array[0] is equal to the value of 16; hence, in each loop iteration, it adds 16 to the variable result.
For example,
For the first iteration of the loop, the result is 16.
For the second iteration of the loop, the result is 32 (16 + 16)
For the third iteration of the loop, the result is 48 (16 + 32).
For the fourth iteration of the loop, the result is 64 (16 + 48).
For the fifth iteration of the loop, the result is 80 (16 + 64).

Below is a code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int array [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for (n=0 ; n<5; n++)
{
	result += array[0]; // Result is 16, 32, 48, 64, 80
	cout << result << endl; // Added this to visualize the running result.
}
cout << result << endl;
	return 0;
}


If you choose to plug 1 into the variable n, the final result will be 10. The array[1] is equal to the value of 2; hence, in each loop iteration, it adds 2 to the variable result.
For example,
For the first iteration of the loop, the result is 2.
For the second iteration of the loop, the result is 4 (2 + 2)
For the third iteration of the loop, the result is 6 (2 + 4).
For the fourth iteration of the loop, the result is 8 (16 + 6).
For the fifth iteration of the loop, the result is 10 (2 + 8).

Below is a code example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int array [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for (n=0 ; n<5; n++)
{
	result += array[1]; // Result is 2, 4, 6, 8, 10
	cout << result << endl; // Added this to visualize the running result.
}
cout << result << endl;
	return 0;
}


I hope it helps.
thank you very much. I really appreciate it!
No problem.
Ah, the tutorial. http://www.cplusplus.com/doc/tutorial/arrays/#access

You have cut and pasted random segments of code together.
Ask yourself: why would that ever work?

(It’s okay, you are still learning!)


At heart, an array is just an ordered list of values with the same type.

  • ordered, because the items have a specific order, starting with 0, then 1, etc.
  • same type, because you cannot mix strings and integers, for example.

Need a list of the grades you got from each of the four tests this semester?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
  double grades[4];

  // Get the grades from the user
  for (int n = 0; n < 4; n++)
  {
    std::cout << "Grade for test #" << (n+1) << "? ";
    std::cin >> grades[n];
  }

  // Find the average grade
  double sum = 0;
  for (int n = 0; n < 4; n++)
    sum += grades[n];

  double average_grade = sum / 4;

  // Tell the user
  std::cout << "Your grade for the semester is " << average_grade << ".\n";
}

Need a list of the top ten girls’s names this year?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <ciso646>
#include <iostream>
#include <string>

std::string top_10_girls_names[10] =
{
  "Emma",
  "Olivia",
  "Ava",
  "Isabella",
  "Sophia",
  "Mia",
  "Charlotte",
  "Amelia",
  "Evelyn",
  "Abigail"
};

int main()
{
  int popularity;
  std::cout << "How popular do you want your daughter to be (1 = most, 10 = least)? ";
  std::cin >> popularity;
  if ((1 > popularity) or (popularity > 10))
    std::cout << "Invalid popularity.\n";
  else
    std::cout << "Name her " << top_10_girls_names[ popularity-1 ] << ".\n";
}

Any time you want a list of like things, you can use an array. All you have to do is make sure your array is large enough to hold all the items you may need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <exception>
#include <iostream>
#include <string>

int main() try
{
  constexpr int MAX_NUMBERS = 10;
  double numbers[MAX_NUMBERS];
  int num_numbers = 0;

  std::cout << 
    "Enter up to " << MAX_NUMBERS << " numbers to average, one per line.\n"
    "Press Enter twice to finish entering numbers.\n";

  while (num_numbers < MAX_NUMBERS)
  {
    std::string s;
    getline( std::cin, s );
    if (s.empty()) break;

    numbers[num_numbers++] = std::stod( s );
  }

  // Calculate the average
  double sum = 0;
  for (int n = 0; n < num_numbers; n++)
    sum += numbers[n];

  double average = sum / num_numbers;

  // Inform the user
  std::cout << "The average is " << average << ".\n";
}
catch (const std::exception& e)
{
  std::cout << e.what() << "\n";
  return 1;
}

Hopefully, this will help you wrap your head around arrays better.

Now, there are all kinds of weird and messed-up things you can do with arrays. Such as the punning used in a counting sort, where the meaning of the different numbers changes as the algorithm progresses. http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/counting-sort/#algorithm
Don’t worry about stuff like that yet. When you get there you will be ready.

Hope this helps.
Topic archived. No new replies allowed.