Matrix operations in C language

AI Thread Summary
The discussion revolves around a coding challenge involving a 4x4 matrix, where the task is to print specific numbers from each row based on their row number. The initial query expresses difficulty in coding this task after only two months of learning C. A participant suggests using nested loops to compare each element in a row with the row number, allowing for the correct digits to be printed. Another participant proposes using a single loop for simplicity, but acknowledges that the nested loop approach better aligns with the concept of a "searching" program. The conversation emphasizes the importance of understanding loop structures in programming and the potential for enhancing the program's functionality.
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
 
Technology 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:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top