Incorrect array values being outputted, compiler error?

I have been sitting scratching my head on why I am getting the wrong values outputted for aplpha. I changed the variable name from "a" to "alpha" thinking it was a variable name issue. Someone please help me with this as it is due tommorow. Could this be an issue with my compiler? I am using devC++
Any help is appreiated!

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

int main()
{
	//Introduce program to user
	cout<<"This program calculates a full 5x3 array using existing values.";
	
	//Declare variables
	int alpha[4][2],b[2],c[4],i;
	
	//Assign data
	alpha[0][0]=2;
	alpha[1][0]=4;
	alpha[2][0]=6;
	alpha[3][0]=8;
	alpha[4][0]=10;
	alpha[0][1]=12;
	alpha[1][1]=14;
	alpha[2][1]=16;
	alpha[3][1]=18;
	alpha[4][1]=20;
	alpha[0][2]=22;
	alpha[1][2]=24;
	alpha[2][2]=26;
	alpha[3][2]=28;
	alpha[4][2]=30;
	
	b[0]=1;
	b[0]=2;
	b[0]=3;
	
	for (i=0;i<=3;i++){
		c[i]=(alpha[i][0]*b[0]+alpha[i][1]*b[1]+alpha[i][2]*b[2]);
	}
	c[4]=alpha[5][1]*b[1]+alpha[5][2]*b[2]+alpha[5][3]*b[3];
	
	//Display results
	cout<<"\nA=\n	1	2	3	4	5\n1	"<<alpha[0][0]<<"	"<<alpha[1][0]<<"	"<<alpha[2][0]<<"	"<<alpha[3][0]\
<<"	"<<alpha[4][0]<<"\n2	"<<alpha[0][1]<<"	"<<alpha[1][1]<<"	"<<alpha[2][1]<<"	"<<alpha[3][1]<<"	"<<alpha[4][1]\
<<"\n3	"<<alpha[0][2]<<"	"<<alpha[1][2]<<"	"<<alpha[2][2]<<"	"<<alpha[3][2]<<"	"<<alpha[4][2];
cout<<"\n"<<alpha[3][0];
}


The results I am getting:

This program calculates a full 5x3 array using existing values.
A=
1 2 3 4 5
1 2 22 24 26 28
2 12 14 16 18 90
3 22 24 26 28 98
26
--------------------------------
Process exited after 0.07828 seconds with return value 0
Press any key to continue . . .

(The last number was for testing purposes....)
Last edited on
I really suggest reviewing another tutorial on arrays.
Perhaps http://www.cplusplus.com/doc/tutorial/arrays/

If you declare an array as
alpha[4][2];
Its valid indices range from alpha[0][0] to alpha[3][1]. alpha[4][2] as an index is not a valid location.

In other words, in your code,
line 18: bad
line 23-28: all bad
(Likewise, there are the same problems when you attempt to print out the invalid indices.)

alpha[5][1]
This is even further wrong, there is no alpha[5][...], let alone alpha[4][...].

1
2
3
	b[0]=1;
	b[0]=2;
	b[0]=3;
Why are you assigning the same thing 3 times?
Last edited on
Always remember: It's never the compiler (until it is).

Also remember that arrays are indexed from 0 up to their size - 1. You seem to want a size of 5 by 3, not 4 by 2 for alpha. The others are off by one, too.

 
int alpha[5][3], b[3], c[5];

Last edited on
@Ganado
Yes, my mistake. Shoud be:
1
2
3
b[0]
b[1]
b[3]

Thanks for the reply
@mbozzi
Haha, yes my fault ;)
But I still do not understand...if I want to assign:
alpha[1][2]
...a value, why does the equal operator not work? From a computer logic perspective?
I am stuck on the initilization part, I just do not know why the above will not work!
Last edited on
Solved, wrong array values.
Thanks for the help!
Topic archived. No new replies allowed.