Matrix operations in C language

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 3K views
phillosopher
Messages
4
Reaction score
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
 
Physics news on Phys.org
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
 
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 ?:-p
 
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:
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: