Matrix operations in C language

In summary, Mr V wants to know how to code a program to print the numbers 1-4 in each row, using a nested loop. He is unsure about how to do this, and suggests using the approach suggested which is easier.
  • #1
phillosopher
4
0
i have a matrix 4x4 like this

1 5 7 3
2 6 8 0
1 4 3 2
0 4 0 5

my teacher asks me to print out 1 2 3 4
each of the digits is from the 1st 2nd 3rd 4th rows
it looks very simple, but because i just learned c for two months, i don't know how to code a program, true.

could you help me ?:shy:
thank you
 
Technology news on Phys.org
  • #2
Hello phillosopher
The answer is quite simple.
I am not jotting down the actual code (it is against PF rules), but I will give you a hint.
Did you notice that the number that you have to print in each row is equal to the row-number. So just write a comparison-performing code, which compares each digit of a row with the row number. If the digit matches with the row-number, print the digit.
You will need a nested loop (a loop inside a loop) for this. Both the external and internal loops will need four iterations.

regards
Mr V
 
  • #3
Mr Virtual said:
Hello phillosopher
The answer is quite simple.
I am not jotting down the actual code (it is against PF rules), but I will give you a hint.
Did you notice that the number that you have to print in each row is equal to the row-number. So just write a comparison-performing code, which compares each digit of a row with the row number. If the digit matches with the row-number, print the digit.
You will need a nested loop (a loop inside a loop) for this. Both the external and internal loops will need four iterations.

regards
Mr V
:biggrin:thank you,
I forget that life-saving spot.
No, I will use only one loop to output the number of rows. That'll be nicer, isn't it ?:tongue2:
 
  • #4
No, I will use only one loop to output the number of rows. That'll be nicer, isn't it ?
Yeah, you can easily do that. But if you want the program to look like a "searching" program (that's the topic of your thread), then the approach I suggested will be better.
 
Last edited:
  • #5
Mr Virtual said:
Yeah, you can easily do that. But if you want the program to look like a "searching" program (that's the topic of your thread), then the approach I suggested will be better.
Yes, that approach is better for the searching program because I suppose I can make the program guess what number comes next especially when the numbers don't denote the row's numbers, thank you, :biggrin:
 

1. What are matrix operations in C language?

Matrix operations in C language refer to the mathematical operations performed on matrices, such as addition, subtraction, multiplication, and division. These operations can be used to manipulate data in a matrix format in order to perform calculations or solve problems.

2. How do I declare a matrix in C language?

To declare a matrix in C language, you need to specify the number of rows and columns in the matrix and use the int data type. For example, int matrix[3][4]; will declare a matrix with 3 rows and 4 columns.

3. How do I perform matrix multiplication in C language?

To perform matrix multiplication in C language, you can use nested for loops to iterate through the rows and columns of the matrices. The result of the multiplication will be stored in a third matrix, where the number of rows will be equal to the number of rows in the first matrix and the number of columns will be equal to the number of columns in the second matrix.

4. How do I add or subtract two matrices in C language?

To add or subtract two matrices in C language, you can use nested for loops to iterate through the rows and columns of the matrices. The result of the addition or subtraction will be stored in a third matrix, where the number of rows and columns will be equal to the number of rows and columns in the original matrices.

5. Can I perform matrix operations on non-numerical data in C language?

No, matrix operations in C language can only be performed on numerical data. If you need to manipulate non-numerical data in a matrix format, you can use a different data structure such as an array of strings or characters.

Similar threads

  • Programming and Computer Science
Replies
1
Views
907
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
985
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
751
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
863
  • Programming and Computer Science
Replies
34
Views
2K
Replies
6
Views
2K
Back
Top