0% found this document useful (0 votes)
9 views5 pages

Exercice 1:: TD4 - C/C++

The document contains 7 exercises involving C/C++ functions for matrix operations, including: 1) Functions to print an integer, set an integer value, and call them in main. 2) Functions to swap two double values by reference and by pointer, calling them in main. 3) Functions to calculate the mean and standard deviation of an array of doubles. 4) A function to calculate the product of two matrices by iterating through rows and columns. 5) Multiple functions for matrix multiplication with vectors and scalars. 6) Functions to calculate the factorial of a number and determinant of a matrix. 7) A function to perform LU factorization on a matrix into lower and upper triangular matrices.

Uploaded by

aclerc828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Exercice 1:: TD4 - C/C++

The document contains 7 exercises involving C/C++ functions for matrix operations, including: 1) Functions to print an integer, set an integer value, and call them in main. 2) Functions to swap two double values by reference and by pointer, calling them in main. 3) Functions to calculate the mean and standard deviation of an array of doubles. 4) A function to calculate the product of two matrices by iterating through rows and columns. 5) Multiple functions for matrix multiplication with vectors and scalars. 6) Functions to calculate the factorial of a number and determinant of a matrix. 7) A function to perform LU factorization on a matrix into lower and upper triangular matrices.

Uploaded by

aclerc828
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TD4 - C/C++

Exercice 1 :

void printInt(int i) {

std::cout << i << std::endl;


}

void setInt(int * p, int set = 0) {


*p = set;
}

int main() {
int i=3;
setInt(&i, 10);
std::cout<<i;
}

Exercice 2 :

void swap(double* v1, double* v2) {


double tmp = *v1;
*v1 = *v2;
*v2 = tmp;
}

int main() {
double i = 1;
double j = 2;
swap(&i, &j);
std::cout << "i : " << i << "\nj : " << j;

return 0;
}

void swap(double v1, double v2 , double &x, double &y) {


x = v2;
y = v1;
}

int main() {
double i = 1;
double j = 2;
swap(i, j, i, j);
std::cout << "i : " << i << "\nj : " << j;
return 0;
}
Exercice 3 :

double mean(double t[]) {


double r = 0;
int N = 5;
for(int i=0; i<N; i++) {
r+=t[i];
}
return r/N;
}

double et(double t[]) {


int N = 5;
double m = mean(t);
double a = 0;
for(int i=0; i<N; i++) {
a+= pow(t[i] - m, 2);
}
return sqrt(5*a/(N-1));
}

Exercice 4 :

#include <iostream>

void produitMatriciel(double A[][3], double B[][2], double RES[][2], int


lignes1, int colonnes1, int colonnes2) {
for (int i = 0; i < lignes1; i++) {
for (int j = 0; j < colonnes2; j++) {
RES[i][j] = 0;
for (int k = 0; k < colonnes1; k++) {
RES[i][j] += A[i][k] * B[k][j];
}
}
}
}

int main() {
double matrice1[2][3] = {{1, 2, 3}, {4, 5, 6}};
double matrice2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
double resultat[2][2];

produitMatriciel(matrice1, matrice2, resultat, 2, 3, 2);

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++) {
std::cout << resultat[i][j] << " ";
}
std::cout << "\n";
}

return 0;
}

Exercice 5 :

void produitMatriciel(double A[][3], double B[][2], double RES[][2], int


lignes1, int colonnes1, int colonnes2) {
for (int i = 0; i < lignes1; i++) {
for (int j = 0; j < colonnes2; j++) {
RES[i][j] = 0;
for (int k = 0; k < colonnes1; k++) {
RES[i][j] += A[i][k] * B[k][j];
}
}
}
}

void produitMatriciel(double V[], double M[][3], double res[], int lignes1,


int colonnes1) {
for(int i=0; i<lignes1; i++) {
res[i] = 0;
for(int j=0; i<colonnes1; i++) {
res[i]+= V[j]*M[i][j];
}
}
}

void produitMatriciel(double M[][3], double V[], double res[], int colonnes1,


int colonnes2) {
for(int i=0; i<colonnes2; i++) {
res[i] = 0;
for(int j=0; i<colonnes1; i++) {
res[i]+= V[j]*M[i][j];
}
}
}

void produitMatriciel(double a, double M[][3], double res[][3], int lignes1,


int colonnes1) {
for(int i=0; i<lignes1; i++) {
for(int j=0; j<colonnes1; j++) {
res[i][j] = a*M[i][j];
}
}
}
Exercice 6 :

int fact(int n) {

if(n==0) {
return 1;
}
return n*fact(n-1);

double det(double M[][3]) {


int res = 0;
for(int i=0; i<3; i++) {
std::cout << pow(-1, i) << std::endl;
res+= pow(-1, i) * M[i][0] * (M[(i+1) % 3][1] * M[(i+2) % 3][2] -
M[(i+1) % 3][2] * M[(i+2) % 3][1]);
}

return res;
}
Exercice 7 :

void factorisationLU(const std::vector<std::vector<double>> &matrice,


std::vector<std::vector<double>> &L, std::vector<std::vector<double>> &U)
{
int n = matrice.size();
L = std::vector<std::vector<double>>(n, std::vector<double>(n, 0.0));
U = std::vector<std::vector<double>>(n, std::vector<double>(n, 0.0));

for (int i = 0; i < n; ++i)


{
L[i][i] = 1.0;

for (int j = i; j < n; ++j)


{
double sum = 0.0;
for (int k = 0; k < i; ++k)
{
sum += L[i][k] * U[k][j];
}
U[i][j] = matrice[i][j] - sum;
}

for (int j = i + 1; j < n; ++j)


{
double sum = 0.0;
for (int k = 0; k < i; ++k)
{
sum += L[j][k] * U[k][i];
}
L[j][i] = (matrice[j][i] - sum) / U[i][i];
}
}
}

You might also like