How to assign a value to specific locations of a matrix in C?

June Source

I have matrix int abc[2][2] = {x11,x12,x21,x22} in I need to take user input on only x12 and x22 location and x11 and x21 are fixed value.

How to do that?

cmatrix

Answers

answered 5 days ago Stargateur #1

If it's as simple your question suggest just use:

if (scanf("%d %d", abc[0] + 1, abc[1] + 1) != 2) {
  // error do something
} 

answered 5 days ago Monc #2

use as follows:

scanf("%d %d", &abc[0][1], &abc[1][1]);

a[0][1] --> 0th row first col i.e.x12

a[1][1] ---> first row first col i.e. x22

Note: can handler error of scanf as:

if (scanf("%d %d", &abc[0][1], &abc[1][1]) != 2) {
  // handle error
} 

answered 5 days ago T. Obadiah #3

So you have a 2x2 array(abc). That means your array has 2 rows,2 columns. It could be nxm. You can access or assign to a nxm array, using subscript notation, remember array subscript start at 0 to size - 1.

In your case to assign to location n,m abc[n][m] = someInt;

Reading and assigning to the location using scanf scanf("%d %d", &abc[0][1], &abc[1][1]). This correspond to what I think you want to do.

comments powered by Disqus