0

Before anything else, my thanks to anyone who is reading this topic. It's much appreciated.

In my final project of a programming subject in university, I've been told to write a program that uses a single matrix, with the following "restrictions":

  1. The user inputs an interval of years (example: 2017-2020) which will determine the number of lines of the matrix by using the formulae:

    NumberOfLines=(FinalYearGiven - InicialYearGiven) + 1;

  2. The matrix must have a fixed column number of 6.

  3. The 1st column of each line will show the year.

  4. The 2nd will show the number of days in february.

  5. The 3rd will show the number of days in the year.

  6. The 4th will show the number of hours in the year.

  7. The 5th will show the number of minutes in the year.

  8. The 6th will show the number of seconds in the year.

Last year I was instructed to do a program that multiplicated two inputed matrices and its allocation was as follows:

 double** multiplication_of_2_matrices(double **A, double **B, int lines1, int columns1, int lines2, int columns2)
    {
        int i, j, k;
        double **C; // Pointer to the resultant matrix, dynamically allocating the matrix in this function
        C = (double**)malloc(lines1 * sizeof(double*));
        for (i = 0; i<lines1; i++)
            C[i] = (double*)malloc(columns2 * sizeof(double));    
        for (i = 0; i < lines1; i++) 
        {
            for (j = 0; j < columns2; j++) 
            {
                C[i][j] = 0.0;
                for (k = 0; k < columns1; k++) 
                {
                    C[i][j] += A[i][k] * B[k][j];
                }
            }
        }
        return C;
    }

My doubt consists in what can I do in this particular project to dynamically allocate the matrix. Can I use the form of allocation specified above or need to use another form?

Community
  • 1
  • 1

1 Answers1

0

Yes you can this exactly the same way of allocation but only thing is you will now want to use the int elements rather than double.

The steps will be precisely

  • After you get the numberOfLines you will allocate the dynamic jagged array.

  • While doing that two things

    1. Don't cast the return value of malloc . It's unnecessary.
    2. Also free the dynamically allocated memory.
unsigned int **c = malloc(sizeof *a*numberOfLines);
if (c == NULL){
    fprintf(stderr,"Error in allocation");
    exit(1);
} 
for(size_t i = 0; i< numberOfLines; i++){
   c[i] = malloc(sizeof *a[i]*NUMCOLS);
   if( c[i] == NULL){
         fprintf(stderr,"Error in allocation");
         exit(1);
   }
}

...
return c;
  • Use magic numbers for the constants like 6.#define NUMCOLS 6. (not mandatory - you can pass it as some parameters also). Just make sure that it is not changed accidentally.

  • I have used unsigned here because none of the data that you are going to store will be negative.

Few modification that has been pointed out$

As unsigned int's size may vary system-wise (it is not true that unsigned int will have 32 bits at least), it is better to use this small trick to ensure maximum sized type.

#if sizeof(unsigned int) < 4 
typedef unsigned long mydata; 
#else 
typedef unsigned int mydata; 
#endif

Also another point is instead of the fragmented memory you can do this to get a 2d array not jagged array.

mydata (*a)[NUMCOLS] = malloc(sizeof *a*numberOfLines);
if (a == NULL){
    fprintf(stderr,"Error in allocation");
    exit(1);
} 
...
return a;

You can do it another way also where you allocate the total number of elements all at one. And then access the positions accordingly.

mydata *a = malloc(sizeof *a * NUMCOLS * numberOfLines);
if (a == NULL){
    fprintf(stderr,"Error in allocation");
    exit(1);
} 
..
..

//accessing a[i][j] will be i*NUMCOLS+j 

$PeterJ_01 pointed out these details and suggested the scope of another way to achieve the allocation

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • I didn't recall using the #define! Thank you so much – Bernardo Fernandes Nov 21 '17 at 10:20
  • @BernardoFernandes.: Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the [About] page and also [How do I ask questions here?](https://stackoverflow.com/help/how-to-ask) and [What do I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – user2736738 Nov 21 '17 at 10:21
  • @coderredoc allcating rows separatelly makes sense only if the number of the columns is not constant or the row is very large and allocation of the whole matrix may fail. Otherwise it makes program much less effective, Second issue - you assume the unsigned integer as minimum 32 bits which does not have to be true,. Third issue is that the requirement is to have a single two dimensional matrix not the matrix of the pointers . IMO it is intentional to see if students understand how to allocate memory for the 2D array. – 0___________ Nov 21 '17 at 10:29
  • @PeterJ_01.: The first one is showing that the method shown by OP can be reused. Your point is right. Can you clarify the second point? You can see the data that will be stored. Any sensible data is covered by the range considered. A clarification will help me and OP both. – user2736738 Nov 21 '17 at 10:33
  • you should check the size of the unsigned integer. `#if sizeof(unsigned int) < 4 typedef unsigned long mydata; #else typedef unsigned int mydata; #endif` and then allocate the memory for the mydata type. – 0___________ Nov 21 '17 at 10:37
  • @PeterJ_01.: As per the 3rd point do you mean that we need to allocate 2d array not jagged array dynamically (using `int (*a)[6]` to allocate memory?) – user2736738 Nov 21 '17 at 10:42
  • Sorry Wrong term. Usually such a questions test if students understand the addressing - so just the single allocation (rows * columns) and the matrix["calculations here"]. It shows if they understand the address arithmetic – 0___________ Nov 21 '17 at 10:51
  • @PeterJ_01.: Thanks for the comment and the time. Please check once. – user2736738 Nov 21 '17 at 10:57