Advantages to using array vs declaring multiple variables?

I'm just curious what the actual advantage is to holding variables in an array versus declaring several variables. It seems to me that the two are virtually the same.

How is:

1
2
3
4
5
char name[4];
  name[0] = 'A';
  name[1] = 'B';
  name[2] = 'C';
  name[3] = 'D';


any different from:

1
2
3
4
char name1 = 'A';
char name2 = 'B';
char name3 = 'C';
char name4 = 'D';


Does it reduce file size by not having to type up "char" so many times? Does it retrieve values from memory more efficiently when the variables are called upon? Obviously, arrays wouldn't exist without good reason, so I would use them when able. Just looking for some background theory I guess.
closed account (2UD8vCM9)
It stores them in memory exactly the same way.
However, think of this scenario.
Let's say I want you to type in 10 temperatures and i'll find the average.
If I don't use arrays, my code would look like this.

Note: This is crappy code that doesn't even do error checking, but it just to prove a point. Also note that if we were to implement error checking for this, it would show an even more drastic difference in lines of code/readability.
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 <iostream>
using namespace std;
int main()
{
	float temp1;
	float temp2;
	float temp3;
	float temp4;
	float temp5;
	float temp6;
	float temp7;
	float temp8;
	float temp9;
	float temp10;
	cout << "Enter temperature:";
	cin >> temp1;
	cout << "Enter temperature:";
	cin >> temp2;
	cout << "Enter temperature:";
	cin >> temp3;
	cout << "Enter temperature:";
	cin >> temp4;
	cout << "Enter temperature:";
	cin >> temp5;
	cout << "Enter temperature:";
	cin >> temp6;
	cout << "Enter temperature:";
	cin >> temp7;
	cout << "Enter temperature:";
	cin >> temp8;
	cout << "Enter temperature:";
	cin >> temp9;
	cout << "Enter temperature:";
	cin >> temp10;
	float average = (temp1 + temp2 + temp3 + temp4 + temp5 + temp6 + temp7 + temp8 + temp9 + temp10)/10;
	cout << "Average temperature:" << average << endl;
	return 0;
}


with arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main()
{
	float temp[10];
	float average;
	for (int i = 0; i < 10; i++)
	{
		cout << "Enter temperature:";
		cin >> temp[i];
		average += temp[i];
	}
	average /= 10;
	cout << "Average temperature:" << average << endl;
	return 0;
}
Topic archived. No new replies allowed.