-1

I'm a beginner in C and I'd like to make a program that calculates a random matrix by a scalar input that user gives. I think that either I should change my second part of scalar() and whole print() or just rewrite the matrix() to get the int to pass easily. My errors are in scalar(), in the for loops. My first function creates, randomizes and prints out the 4x3 matrix. My second function was supposed to take the number from user and multiply the matrix by it and print it out.

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

#define ROW 4
#define COLUMN 3

int matrix[ROW][COLUMN];

void creatematrix() {
    unsigned short i, j;
    srand(time(0));
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COLUMN; j++) {
            matrix[ROW][COLUMN] = rand() % 22 - 11;
            printf("%d Macierz przed pomnozeniem przez skalar: \n",
                   matrix[ROW][COLUMN]);
        }
        printf("\n");
    }
}

void scalar() {
    int scalar, i, j;
    printf("Wprowadz dowolny numer do mnozenia macierzy: ");
    scanf("%d", &scalar);
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COLUMN; j++) {
            matrix[ROW][COLUMN] = scalar * matrix[ROW][COLUMN];
            printf("%d ", matrix[ROW][COLUMN]);
        }
    }
}

int main() {
    creatematrix();
    scalar();
    return 0;
}

Output:

-2 Macierz przed pomnozeniem przez skalar:
-9 Macierz przed pomnozeniem przez skalar:
-7 Macierz przed pomnozeniem przez skalar:

-2 Macierz przed pomnozeniem przez skalar:
-6 Macierz przed pomnozeniem przez skalar:
-8 Macierz przed pomnozeniem przez skalar:

-11 Macierz przed pomnozeniem przez skalar:
-4 Macierz przed pomnozeniem przez skalar:
-6 Macierz przed pomnozeniem przez skalar:

-3 Macierz przed pomnozeniem przez skalar:
7 Macierz przed pomnozeniem przez skalar:
-7 Macierz przed pomnozeniem przez skalar:

Wprowadz dowolny numer do mnozenia macierzy: 4
-28 -112 -448 -1792 -7168 -28672 -114688 -458752 -1835008 -7340032 -29360128 -117440512
Process returned 0 (0x0)   execution time : 6.421 s
Press any key to continue.
chqrlie
  • 131,814
  • 10
  • 121
  • 189
matxi
  • 11
  • 4
  • Is there a specific problem with the code at the moment? Please describe any errors or incorrect behaviours that are prompting you to post this question. – kaylum Jan 17 '21 at 22:05
  • Yeah so I get 8 errors in two of my double for loops. I've rewritten the matrix() void to get the int to easily transfer. @kaylum – matxi Jan 17 '21 at 22:07
  • 1
    I think the first thing you should do is something that compiles – klutt Jan 17 '21 at 22:08
  • Book: https://modernc.gforge.inria.fr/ – klutt Jan 17 '21 at 22:08
  • @klutt my program compiles easily without scalar() and print() – matxi Jan 17 '21 at 22:09
  • 4
    Please [edit](https://stackoverflow.com/posts/65766452/edit) the question to give the exact errors and ask a specific question. Questions on SO need to be more specific than "any suggestions?". – kaylum Jan 17 '21 at 22:09
  • @matxi, you don't have `matrixx` defined in your `scalar` function. also, you are not using `ROW` and `COLUMN` correctly in the loops. Those are "constant", not variables – Moha the almighty camel Jan 17 '21 at 22:10
  • 1
    For the same reason, SO is not a place for book recommendations either. SO is for questions and answers, not so much for discussion of opinions, suggestions, recommendations, etc. – Alexander Guyer Jan 17 '21 at 22:10
  • 1
    Well, `#define ROW` is not a variable declaration, so of course you cannot assign it and increment it – klutt Jan 17 '21 at 22:10
  • as for the book recommendations, SO is not the place, but C is an exception: see here -> https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Moha the almighty camel Jan 17 '21 at 22:11
  • Example : `for (ROW = 0; ROW < i; ROW++)` after preprocess becomes `for (4 = 0; 4 < i; 4++)` which is nonsense. If you can't see the fundamental difference in the loop structure of those functions, `scalar` and `print`, than that of the function that apparently works, `matrix`, you need a more fundamental understanding of what you're trying to do, and how `matrix` succeeds at what it does. – WhozCraig Jan 17 '21 at 22:11
  • @WhozCraig yeah now I see it haha. I also changed the code to erase the third void. I'll try to fix it too. My intentions were to make a 4x3 matrix and randomize it. – matxi Jan 17 '21 at 22:15
  • You're doing that, though even in `matrix` you invoke undefined behavior by addressing `matrix[ROW][COLUMN]`, which is out of range on both superior, and inferior, dimensions. Regardless, look at the code you added since `matrix`. It's gibberish. Just look at the build loop in `matrix` vs. `scalar`. There is a clear difference between `for (i=0; i – WhozCraig Jan 17 '21 at 22:17
  • `matrix[ROW][COLUMN] = ....` still just as broken. What are you looking for, and more important, what are you looping *with* ? Surely `i` and `j` are more useful than just for loop control. – WhozCraig Jan 17 '21 at 22:25
  • @WhozCraig okay, how about now. I have no errors but the outputs are wrong. I will put that in the post. – matxi Jan 17 '21 at 22:26
  • See prior comment. – WhozCraig Jan 17 '21 at 22:26

1 Answers1

0

The statements to initialize and update the matrix elements are incorrect: you use matrix[ROW][COLUMN] instead of matrix[i][j]

Here is a modified version:

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

#define ROWS    4
#define COLUMNS 3

int matrix[ROWS][COLUMNS];

void creatematrix(void) {
    int i, j;
    srand(time(0));
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLUMNS; j++) {
            matrix[i][j] = rand() % 22 - 11;
        }
    }
}

void scalar(void) {
    int scalar, i, j;
    printf("Wprowadz dowolny numer do mnozenia macierzy: ");
    if (scanf("%d", &scalar) != 1)
        return;
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLUMNS; j++) {
            matrix[i][j] *= scalar;
        }
    }
}

void print(void) {
    int i, j;
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLUMNS; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main() {
    creatematrix();
    print();
    scalar();
    print();
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189