jeudi 5 novembre 2020

Matrix addition of two randomly generated matrices

I want to do matrix addition of two 1D matrices of N elements (1xN), where the matrices generated by the code itself. I've seen many examples for this online, but in those examples user input is required, but in my case N is too large, so I want my code to generate it. So far I got:

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

int n=5;
int a[5], b[5];
double sum[5];

int main() {
unsigned short i,j, k;

    for(i = 0; i < n;i++) {
      printf("%d ", a[i]=rand()%100);
    }

    for(i = 0; i < n;i++) {
      printf("%d ", b[j]=rand()%100);
    }

    for(i = 0; i < n;i++) {
      sum[i]=a[i]+b[i];
    }
    return 0;
}

But this gives an output of 10 numbers in one line, and doesn't add them. The output is:

83 86 77 15 93 35 86 92 49 21 [user home]$

So how can I perform the addition? I'd appreciate your help as a beginner..

PS: I don't know why the output appears in front of the prompt. Also, I don't have to generate a new number since the main purpose is generating N numbers in an array.




Aucun commentaire:

Enregistrer un commentaire