3

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user10906383
  • 323
  • 1
  • 8
  • 3
    It's not the number in your array that's wrong. It's how it's being *printed* that's wrong. – Steve Jan 13 '19 at 01:25
  • 2
    Please specify whether you're using C or C++, there is a difference even if you don't use C++ specific stuff. For example, if you're using C++, we would tell you to use `std::cout` with the `<<` operator so you don't have to mess with `printf` format specifiers and have problems like these. – eesiraed Jan 13 '19 at 01:26
  • 2
    Next time, please try to make an [mcve]. In this case, your problem is caused by you using the incorrect format specifier in `printf` and has nothing to do with 2D arrays. As an example, you could have tried to print out a single double instead of an array of doubles which would have narrowed down the problem and potentially allow you to solve the problem yourself since you wouldn't have believed that the problem had to do with the arrays. – eesiraed Jan 13 '19 at 01:29
  • Possible duplicate of [Unexpected output of printf](https://stackoverflow.com/questions/17898186/unexpected-output-of-printf) – eesiraed Jan 13 '19 at 01:45
  • I'm not sure if you'd do better on [Stack Overflow em Português](https://pt.stackoverflow.com/) — you'd be able to use Portugese, but your command of English seems quite good enough to be able to ask here. – Jonathan Leffler Jan 13 '19 at 01:48
  • [Double-precision floating-point format](https://en.wikipedia.org/wiki/Double-precision_floating-point_format) may be relevant. – Ted Lyngmo Jan 13 '19 at 01:55
  • Printing a `double` using the `%d` format gives undefined behaviour. Use `%f` or `%g` instead. In C++, consider using IO streams, rather than the `printf()` family of functions. – Peter Jan 13 '19 at 05:18

2 Answers2

7

%d format specifier of printf expects an int argument, but you are passing a double. Your program then exhibits undefined behavior. Consult your favorite C or C++ textbook on how to output a value of type double.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • 1
    Here is a page with all the format specifiers https://en.cppreference.com/w/cpp/io/c/fprintf – Chris Rollins Jan 13 '19 at 01:20
  • 3
    Sooo it had nothing to do with matrices and 90% of the question's code was irrelevant. Why does nobody do MCVEs any more :( – Lightness Races in Orbit Jan 13 '19 at 01:21
  • 4
    @LightnessRacesinOrbit if the question asker knew it was because of printf he wouldn't have needed to ask the question in the first place – Chris Rollins Jan 13 '19 at 01:22
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) Viewing the values in a debugger would have shown them to be correct. – Steve Jan 13 '19 at 01:26
  • 2
    @ChrisRollins Finding the problem is actually quite a common side effect of producing an MCVE. Which is one reason why they are so useful. – Baum mit Augen Jan 13 '19 at 01:41
  • @ChrisRollins _all_ is a risky statement. _Many_ would be sufficient. – Ted Lyngmo Jan 13 '19 at 02:00
  • @LightnessRacesinOrbit my point is the asker did not know how to determine that the issue was presentation rather than data. So what does a better MCVE look like in your mind? The code posted is very simple. It has the data, the presentation, and pretty much nothing else. – Chris Rollins Jan 15 '19 at 07:48
  • @ChrisRollins The process of creating a MCVE is actually the process of debugging and it's exactly how you discover that kind of thing. And that's why every guideline on the site for asking questions asks people to do that first, along with detailed instructions on how to do it. In this case, a valid MCVE has nothing to do with matrices, but simply prints a `double` with `%d` and observes the wrong results. It's a four-line program. Cheers! – Lightness Races in Orbit Jan 15 '19 at 10:38
3

You have tagged both C & C++ you should pick one or the other. I'll give answers for both languages.


If using C with printf() when you change your 2D array from int to double the calculations appear to be fine, what is wrong here is your output with printf(). You are using %d which expects an int but you are passing it a double and this leads to undefined behavior. When using float or double change this to %f or %g.


If using C++ try not to use printf(). Instead you should include iostream. Then your output would look something like:

// printf("A soma dos numeros eh %d e o maior numero eh %d\n", soma2, maior2);
std::cout << "A soma dos numerous eh " << soma2 << " e o maior numero eh << maior2 << '\n';

for ( i = 0; i < 5; i++ ) {
    for ( j = 0; j < 2; j++ ) {
        // printf("a[%d][%d] = %d\n", i,j, matriz2[i][j] );
        std::cout << "a[" << i << "][" << j "] = " << matriz2[i][j] << '\n';
    }
}
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59