C programe


#1

Hi guys,

I just started the university and I have a project to do in C.
The text its the following:

We have a matrix with N lines and M columns and elements natural numbers.
Modify the matrix :
All elements equal with maximum value from matrix to be replaced with minimum value on their columns.
The program reads from a file named numbers.txt the numbers N and M, and then N*M natural numbers, with spaces in between. Then it writes into another file named matrix.txt the matrix obtained.

This above is the problem that I have to do. I have to add that my C knowledge is close to 0 and I have no idea how to do it. ( at the moment I am taking the courses from the website)

Thanks in advance,

Stefan


#2

I would first try to understand what they are asking to do.

We have a matrix with N lines and M columns and elements natural numbers.

So what appears in my head is something like this:

___________________
|8 |4 |26|14|16|9 |
-------------------
|21|2 |8 |17|26|2 |
-------------------
|10|7 |1 |15|24|1 |
-------------------

In this example N=3, M=6. All the elements in the matrix are natural numbers up to 26 (natural numbers are all number from 1 (including) to positive infinity. They exclude irrational, decimal, complex, negative and other numbers. So, it includes the numbers(except 0) you learned in the kindergarten:
{1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; …}

The total number of elements is N * M which is, in this example, is 3 * 6=18

The program reads from a file named numbers.txt the numbers N and M, and then N*M natural numbers, with spaces in between.

In our example, the numbers.txt file would look like this:

3 6 8 4 26 14 16 9 21 2 8 17 26 2 10 7 1 15 24 1

3 is N (number of lines) and 6 is M (number of columns). The other numbers will be the elements of the matrix.

Now that we have drawn an example initial situation, we are going to look at what we should do:

All elements equal with maximum value from matrix to be replaced with minimum value on their columns.

The maximum value here in our example is 26. Every time it appears in our example we should replace it with the minimum value on their columns. So the transformed matrix would look something like this:

___________________
|8 |4 |1 |14|16|9 |
-------------------
|21|2 |8 |17|16|2 |
-------------------
|10|7 |1 |15|24|1 |
-------------------

Now this should be outputted to a file.

Then it writes into another file named matrix.txt the matrix obtained.

If we follow the same convention we followed in numbers.txt, matrix.txt will look like this:

3 6 8 4 1 14 16 9 21 2 8 17 16 2 10 7 1 15 24 1

Now that we have a basic idea on the steps taken we are going to take a piece of paper and write every step that will be taken. Every step should be when written in your program get full focus. With that I mean that you should forget the last finished step and focus completely on making the current step. This is the cleanest and best way on writing low-level programs.
Here’s an example:

  1. Open file.
  2. Create an array containing the numbers read from the file. Let’s call it given_input.
  3. Define lines as given_input[0] and columns as given_input[1].
  4. Create N arrays with M elements.
  5. Create an array containing the pointers to the line arrays. Every array pointed to is a line.

(A function should be written for steps 4 and 5.)

You then translate those steps into C.

I have to add that my C knowledge is close to 0 and I have no idea how to do it. ( at the moment I am taking the courses from the website)

I would recommend buying a book.
You could try to first translate those steps into a high-level language and then translate it to C.

Have fun. :slight_smile:

Note: If you have any more questions, feel free to ask. This was really fun to answer.