This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
TP-TR/chap0/mem.c
Super_JK bd84de1a32 :,)
2022-03-29 17:17:15 +02:00

59 lines
1.1 KiB
C

#include <stdio.h> // p r i n t f ( . . )
#include <stdlib.h> // EXIT_SUCCESS
#include <signal.h> // s i g n a l ( . . )
#include <unistd.h>
struct vector {
double *val;
int size;
};
struct vector * new ( unsigned int n ){
struct vector* v = malloc(sizeof(struct vector));
v->size = n;
v->val = calloc(n, sizeof(double));
return v;
}
void printVec(struct vector* v){
printf("[");
for (int i = 0; i< v->size; i++){
printf("%.2f,",v->val[i]);
}
printf("]");
}
struct vector * add ( struct vector * v , struct vector * w ){
if (v->size != w->size) return NULL;
struct vector *y = new(v->size);
for (int i = 0; i< v->size; i++){
y->val[i] = v->val[i] + w->val[i];
}
return y;
}
struct vector * smul ( double s , struct vector * v ){
struct vector *y = new(v->size);
for (int i = 0; i< v->size; i++){
y->val[i] = v->val[i] * s;
}
return y;
}
int main ( int argc , char * argv []) {
struct vector *v = new(3);
v->val[0] = 23.0;
v->val[1] = 5.0;
struct vector *w = new(3);
w->val[1] = 2.0;
w->val[2] = 54.0;
printVec(smul(3.0,add(v,w)));
return EXIT_SUCCESS ;
}