0

I am learning C and I keep mixing up array and matrix. and I can't seem to understand where I am doing wrong with my code. I have made 2 different version of it and the only feedback I am getting is, I have mixed array with matrix.

I was wondering if anyone could help me understand exactly where I went wrong, since I am not getting anywhere with my prof and his explanation about it. (Sorry in advanced)

This is the first code, But both of them are really similar.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int array[100];
    int max = 0, min = 1;
    int fro;

    fro = (int)time(NULL);
    srand(fro);

    for (int i = 0; i < 100; i++) {
        array[i] = rand() % (400 + 1 - 100) + 100;
        if (array[i] <= min) {
            min = array[i];
        }
        if (array[i] >= max)
            max = array[i];
    }
     
    printf("Array\n");
    for (int row = 0; row < 10; row++) {
        for (int col = 0; col < 10; col++) {
            printf(" %i ", array[row * 10 + col]);
        }
        printf("\n");
    }
    return 0; 
}

This is another version, but I got similar feedback as the first one. I am mixing up array and matrix..

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int tal[10][10];
    int array[100];
    int max = 0, min = 1;
    int fro;

    fro = (int)time(NULL);
    srand(fro);

    // Version 3 of the array slump generation

    for (int row = 0; row < 10; row++) {
        //printf("Yttre loop row %i\n", row);
        
        for (int col = 0; col < 10; col++) { // row = 1, col = 0
            tal[row][col] = rand() % (400 + 1 - 100) + 100;
            // printf("tal[%i][%i] = %i\n", row, col, tal[row][col]);

            // Get max
            if (tal[row][col] >= max) {
                max = tal[row][col];
            }
            // Get min
            if (tal[row][col] <= min) {
                min = tal[row][col];
            }
            // printf("tal[%i][%i] = %i\n", row, col, tal[row][col]);
        }
    }
    
    // Printar ut hela matrisen i row-major order
    for (int row = 0; row < 10; row++) {
        for (int col = 0; col < 10; col++) {
            printf(" %i ", tal[row][col]);
        }
        printf("\n");
    }
    
    return 0;
}

I mean both of them works and I think I am using array :/ We haven't even gone trough matrix yet...

the busybee
  • 10,755
  • 3
  • 13
  • 30
ItsYaKoala
  • 39
  • 5

1 Answers1

2

The notion of a matrix is not an ISO C concept. However, you can use an array to denote a matrix by imposing imposing additional linear algebra constraints. There are several linear algebra libraries for C. However, be prepared for some people use the term matrix loosely to mean any two-dimensional array, because they look similar in most presentations.

Your first example is an int array[100] which stores array 100 of int. You then access it by 10 row and 10 col; since 10 * 10 = 100 <= 100 that you've reserved for it, this is entirely valid. One might prefer this representation because it is explicitly contiguous, but has no way for the compiler to bounds-check.

Your second example is int tal[10][10], which is array 10 of array 10 of int, all in the same block of memory. It's the same 100-element array, but accessed differently, as you've done. This is also valid, and I think more what your teacher was asking.

The one you treat more like a matrix will be more like a matrix.

Neil
  • 1,767
  • 2
  • 16
  • 22