0

Can anyone explain to me, why the below codes have different output?

void GenerateMatrix(int mat[][MaxSize],int ran[],const int rows,const int cols) { int i,k=0; while (i<rows) { int j=0; while (j<cols) { mat[i][j]=ran[k]; j,k++; } i++; } } 

and the correct one

void GenerateMatrix(int mat[][MaxSize],int ran[],const int rows,const int cols) { int k=0; for (int i=0; i<rows;i++) { for (int j=0; j<cols;j++) { mat[i][j]=ran[k]; k++; } } } 

the ran[] is an array declared in main()

int main() { srand(time(NULL)); int a[10]; for (int i=0;i<10;i++) { a[i]={(rand() % 20-0+1)+1}; cout<<a[i]<<endl; } . . . } 

the first one always output strange random numbers, not in the range of random numbers I set for a[] in main(). The second one is correct. I have change the scope of declaration of k in the first one function, still the same. I'm just wondering witch part result in this?

6
  • 2
    What do you intend j,k++; to do? Did you debug to observe whether the variables change as you intended? Commented May 25, 2021 at 6:32
  • 3
    int i, k = 0 is equivalent to int i; int k = 0 so i is uninitialized when you are accessing it, triggering undefined behavior. You probably meant int i = 0, k = 0. Commented May 25, 2021 at 6:36
  • 1
    Also j,k++ is equivalent to k++ (assuming the evaluation of j doesn't have side-effects), not a very useful statement. Commented May 25, 2021 at 6:37
  • Thanks, I had always thought I can declared two integers at the same time..., and is there a better way instead j,k++ if I want to increase both counter variables at the same time? Commented May 25, 2021 at 6:52
  • Err, j++; k++;? Commented May 25, 2021 at 6:58

2 Answers 2

4

You seem to be following a "spoken English" approach to using the ,, like "do something to this list of variables". In this reading, the initialisation failure (where it does create a list other than you mean; spotted by CherryDT) and the incrementation failure (caused by misuse as comma operator; spotted by two commenters, including me) can be "read".
I propose to change that by reading up on the "comma operator" and reviewing some tutorials on other uses of ",".
Consider reducing/avoiding the use altogether, it is too dangerous for your coding habits.

Sign up to request clarification or add additional context in comments.

5 Comments

For reference, it's called "comma operator".
@CherryDT not in all the problematic cases. Also, I believe that referring to it that way is not helpful to OP. But I will add it for completeness. Thanks.
I just meant it's hard to google a literal comma. But you are right, in case of the initialization issue it has nothing to do with that operator.
@CherryDT True.
Unrelated: yes, you are correct. Kudos for being the first one to point this out.
2

In the first function you have two errors:

  1. The statement

    int i,k=0; 

    is equivalent to

    int i; int k=0; 

    This means that i is uninitialized and will have an indeterminate value.

    You need to explicitly initialize it as well:

    int i = 0, k = 0; 
  2. The second error is the statement:

    j,k++; 

    This is equivalent to

    j; k++; 

    That is, it evaluates the left-hand side of the comma operator (i.e. j) and then throws away the result. Then it evaluates and returns the result of the right-hand side (i.e. k++).

    You need to increment both variables:

    j++; k++; 

Or you can use the for loops which are more compact, still as readable, and harder to make such mistakes as in the first function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.