Array problem

Hello. I tried to do this simple "calculator" but it doesn't work the way I want. It gets result right but the first three numbers from the array are weird. Why does it happen?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;

float res;
int q;

int main()
{
	cout << "How many numbers? ";
	cin >> q;
	for(int pos = 1; q >= pos; pos++)
	{
		float in[q];
		cout << "Enter " << pos << ". number: ";
		cin >> in[pos];
		res += in[pos];
	}
	cout << endl;
	for(int pos = 1; q >= pos; pos++)
	{
		float in[q];
		cout << in[pos];
		if(q > pos) cout << " + ";
	}
	cout << " = " << res << endl;
}

/*expected program behavior:
How many numbers? 10
Enter 1. number: 2
Enter 2. number: 5
Enter 3. number: 3
Enter 4. number: 7
Enter 5. number: 9
Enter 6. number: 11
Enter 7. number: 14
Enter 8. number: 1
Enter 9. number: 19
Enter 10. number: 23

2 + 5 + 3 + 7 + 9 + 11 + 14 + 1 + 19 + 23 = 94

WHAT REALLY HAPPENED : 
How many numbers? 10
Enter 1. number: 2
Enter 2. number: 5
Enter 3. number: 3
Enter 4. number: 7
Enter 5. number: 9
Enter 6. number: 11
Enter 7. number: 14
Enter 8. number: 1
Enter 9. number: 19
Enter 10. number: 23

0 + 5.88569e-039 + 0 + 7 + 9 + 11 + 14 + 1 + 19 + 23 = 94
*/
Last edited on
You may be able to use variable-length arrays (your line 13) in later versions of C (C99?), but they are illegal in C++.

Either way you shouldn't declare your storing container every time you run a loop.

Note also: arrays of size q count from 0 to q-1.

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
#include<iostream>
using namespace std;

int main()
{
   double res = 0;                          // <==== Avoid unnecessary global variables; also: INITIALISE
   int q;

   cout << "How many numbers? ";
   cin >> q;

   double *in = new double[q];              // <==== Whatever container you use, only declare this ONCE

   for ( int pos = 0; pos < q; pos++ )      // <==== Arrays count from 0: you'll have to deal with it
   {
      cout << "Enter " << pos + 1 << ". number: ";
      cin >> in[pos];
      res += in[pos];
   }

   cout << endl;

   for ( int pos = 0; pos < q; pos++ )      // <==== Limits as above
   {
      cout << in[pos];
      if ( pos < q - 1 ) cout << " + ";
   }
   cout << " = " << res << endl;

   delete [] in;                            // <==== If using dynamic arrays
}


How many numbers? 10
Enter 1. number: 2
Enter 2. number: 5
Enter 3. number: 3
Enter 4. number: 7
Enter 5. number: 9
Enter 6. number: 11
Enter 7. number: 14
Enter 8. number: 1
Enter 9. number: 19
Enter 10. number: 23

2 + 5 + 3 + 7 + 9 + 11 + 14 + 1 + 19 + 23 = 94



I will probably be expelled by the forum if I don't mention that you can avoid the dynamic arrays (use of new and delete above) if you use
vector<double> in( q );
instead of the dynamically-allocated array in the code above. Access to its elements via [] looks exactly the same. If you use this then (i) add a header #include<vector> and (ii) remove the delete [] in; line.
Last edited on
Thanks you very much :)
Topic archived. No new replies allowed.