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!
#include<iostream>
#include<cmath>
usingnamespace 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 . . .
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][...].
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.
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!