I am learning by myself (started few days ago) and could not find reliable information in portuguese (my home language). After just too much headache, I gave up and came here for help. I do not know how to make am array/2d array of doubles, only intergers. The following code works as expected:
int main(void){
int matriz[5][2] = {{1,2}, // DECLARATION OF MATRIX COMPOSED OF INTERGERS
{3,4},
{5,6},
{7,8},
{9,10}};
int maior = matriz[0][0]; // BIGGEST NUMBER INSIDE MATRIX, STARTED AS FIRST POSITION
int soma = 0; // SUM OF NUMBERS START WITH ZERO
int lin = 0; // INDICATION OF LINE
int col = 0; // INDICATION OF COLUMN
for (lin = 0; lin < 5; lin++){
for (col = 0; col < 2; col++){
if (maior < matriz[lin][col]) {maior = matriz[lin][col];}
soma += matriz[lin][col];
}
}
printf("A soma dos numeros eh %d e o maior numero eh %d\n", soma, maior);
// THE SUM OF NUMBERS IS %D AND THE BIGGEST NUMBER IS %D
// I COPIED THE FOLLOWING CODE FROM ELSEWHERE TO SEE WHAT WAS GOING ON
int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, matriz[i][j] );
}
}
return 0;}
The output is just as expected:
A soma dos numeros eh 55 e o maior numero eh 10
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4
a[2][0] = 5
a[2][1] = 6
a[3][0] = 7
a[3][1] = 8
a[4][0] = 9
a[4][1] = 10
**
However any change as the folowing code is always wrong:
**
int main(void){
double matriz2[5][2] = {{10000,1000},
{100,10},
{1,0.1},
{0.01,0.001},
{0.0001,0.00001}};
double maior2 = matriz2[0][0];
double soma2 = 0;
int lin2 = 0;
int col2 = 0;
for (lin2 = 0; lin2 < 5; lin2++){
for (col2 = 0; col2 < 2; col2++){
if (maior2 < matriz2[lin2][col2]) {maior2 = matriz2[lin2][col2];}
soma2 += matriz2[lin2][col2];
}
}
printf("A soma dos numeros eh %d e o maior numero eh %d\n", soma2, maior2);
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %d\n", i,j, matriz2[i][j] );
}
}
return 0;}
The output is always wrong as I keep changing it. I declared long doubles, doubles, interger and doubles etc. And each time the code outputs wrong answers. Some combinations output negative numbers. Others just output ZERO everywhere. Two examples:
A soma dos numeros eh 0 e o maior numero eh 0
a[0][0] = 10000
a[0][1] = 1000
a[1][0] = 100
a[1][1] = 10
a[2][0] = 1
a[2][1] = 0
a[3][0] = 0
a[3][1] = 0
a[4][0] = 0
a[4][1] = 0
or
A soma dos numeros eh 953826337 e o maior numero eh 0
a[0][0] = 0
a[0][1] = 0
a[1][0] = 0
a[1][1] = 0
a[2][0] = 0
a[2][1] = -1717986918
a[3][0] = 1202590843
a[3][1] = -755914244
a[4][0] = -350469331
a[4][1] = -1998362383
I simply could not find a way to declare or put double number in a matrix. How can I do it?