Upload files to "sfusi"
This commit is contained in:
parent
70912ecbda
commit
10a5b0bcc4
|
@ -0,0 +1,37 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
/*dichiarare e inizializzare una matrice 3x4
|
||||
scrivere la funzione stampa che la visualizza */
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
using namespace std;
|
||||
|
||||
void stampa(int arr[3] [4]) {
|
||||
|
||||
for(size_t i = 0; i<3; ++i){
|
||||
for(size_t j = 0; j<4; ++j){
|
||||
cout << setw(3) << arr[i] [j];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
int arr[3] [4]{
|
||||
|
||||
{3,2,6,1},
|
||||
{5,8,3,7},
|
||||
{6,1,5,3},
|
||||
|
||||
};
|
||||
|
||||
stampa(arr);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void sommaColonne(int matrice[4][5], int righe, int colonne) {
|
||||
int somma[5] = {0};
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
somma[j] += matrice[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Somma per colonna: ";
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
cout << somma[j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[4][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}};
|
||||
sommaColonne(matrice, 4, 5);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void bubbleSort(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
swap(arr[j], arr[j + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {5, 1, 4, 2, 8};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
bubbleSort(arr, n);
|
||||
cout << "Sorted array: ";
|
||||
printArray(arr, n);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void trovaMassimo(int matrice[3][3], int righe, int colonne) {
|
||||
int max = matrice[0][0];
|
||||
int x = 0, y = 0;
|
||||
int confronti = 1;
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
confronti++;
|
||||
if (matrice[i][j] > max) {
|
||||
max = matrice[i][j];
|
||||
x = i;
|
||||
y = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Numero di confronti: " << confronti << endl;
|
||||
cout << "Massimo trovato: " << max << " alle coordinate (" << x << ", " << y << ")" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
trovaMassimo(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void prodottoMatriciale(int A[3][2], int B[2][3], int C[3][3], int righeA, int colonneA, int righeB, int colonneB) {
|
||||
for (int i = 0; i < righeA; i++) {
|
||||
for (int j = 0; j < colonneB; j++) {
|
||||
C[i][j] = 0;
|
||||
for (int k = 0; k < colonneA; k++) {
|
||||
C[i][j] += A[i][k] * B[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int A[3][2] = {{1, 2}, {3, 4}, {5, 6}};
|
||||
int B[2][3] = {{7, 8, 9}, {10, 11, 12}};
|
||||
int C[3][3];
|
||||
|
||||
prodottoMatriciale(A, B, C, 3, 2, 2, 3);
|
||||
|
||||
cout << "Prodotto matriciale (A * B):" << endl;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
cout << C[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
//Patriche Robert Cosmin 3Ain
|
||||
/*Dato un vettore di 15 numeri interi, caricare i propri voti di informatica (n voti, con n preso da tastiera e minore o uguale di 15).
|
||||
Successivamente calcolare e visualizzare la media dei voti.*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Funzione per caricare i voti
|
||||
void caricaVoti(int voti[], int n) {
|
||||
cout << "Inserisci " << n << " voti di informatica:" << endl;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << "Voto " << i + 1 << ": ";
|
||||
cin >> voti[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per calcolare la media dei voti
|
||||
double calcolaMedia(int voti[], int n) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
somma += voti[i];
|
||||
}
|
||||
return somma / n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
const int MAX_VOTI = 15;
|
||||
int voti[MAX_VOTI];
|
||||
int n;
|
||||
|
||||
// Input del numero di voti
|
||||
cout << "Quanti voti vuoi inserire? (max 15): ";
|
||||
cin >> n;
|
||||
|
||||
// Controllo del limite massimo
|
||||
if (n <= 0 || n > MAX_VOTI) {
|
||||
cout << "Numero di voti non valido. Deve essere tra 1 e 15." << endl;
|
||||
return 1; // Termina il programma con errore
|
||||
}
|
||||
|
||||
// Caricamento dei voti
|
||||
caricaVoti(voti, n);
|
||||
|
||||
// Calcolo e visualizzazione della media
|
||||
double media = calcolaMedia(voti, n);
|
||||
cout << "La media dei voti inseriti e': " << media << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
//Patriche Robert Cosmin
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
void contaPNZ(int arr[], int size, int &positivi, int &negativi, int &nulli);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 21 - 10; // Numeri tra -10 e 10
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
int positivi = 0, negativi = 0, nulli = 0;
|
||||
contaPNZ(numeri, DIM, positivi, negativi, nulli);
|
||||
|
||||
cout << "Positivi: " << positivi << ", Negativi: " << negativi << ", Nulli: " << nulli << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void contaPNZ(int arr[], int size, int &positivi, int &negativi, int &nulli) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] > 0) positivi++;
|
||||
else if (arr[i] < 0) negativi++;
|
||||
else nulli++;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// Funzione per eseguire la One-Hot Encoding
|
||||
vector<vector<int>> oneHotEncoding(const vector<string>& categorie, const vector<string>& etichette) {
|
||||
vector<vector<int>> matriceOneHot;
|
||||
|
||||
// Per ogni etichetta, troviamo la sua posizione e creiamo un vettore one-hot
|
||||
for (const string& etichetta : etichette) {
|
||||
vector<int> oneHot(categorie.size(), 0); // Inizializza un vettore di zeri
|
||||
for (int i = 0; i < categorie.size(); ++i) {
|
||||
if (categorie[i] == etichetta) {
|
||||
oneHot[i] = 1; // Imposta la posizione corrispondente a "1"
|
||||
break;
|
||||
}
|
||||
}
|
||||
matriceOneHot.push_back(oneHot); // Aggiunge il vettore one-hot alla matrice
|
||||
}
|
||||
|
||||
return matriceOneHot;
|
||||
}
|
||||
|
||||
// Funzione per stampare una matrice
|
||||
void stampaMatrice(const vector<vector<int>>& matrice) {
|
||||
for (const auto& riga : matrice) {
|
||||
for (int valore : riga) {
|
||||
cout << valore << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Definiamo le categorie possibili
|
||||
vector<string> categorie = {"Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica"};
|
||||
|
||||
// Definiamo alcune etichette da codificare
|
||||
vector<string> etichette = {"Lunedì", "Mercoledì", "Domenica", "Venerdì", "Martedì"};
|
||||
|
||||
// Otteniamo la matrice one-hot encoding
|
||||
vector<vector<int>> matriceOneHot = oneHotEncoding(categorie, etichette);
|
||||
|
||||
// Stampiamo la matrice risultante
|
||||
cout << "Matrice One-Hot Encoding:" << endl;
|
||||
stampaMatrice(matriceOneHot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Verifica se tutte le celle dei “bordi” contengono lo stesso valore
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define NUM 4
|
||||
using namespace std;
|
||||
void stampaMatrice(int matrice[NUM][NUM]) {
|
||||
cout << "Matrice:\n";
|
||||
for (int i = 0; i < NUM; ++i) {
|
||||
for (int j = 0; j < NUM; ++j) {
|
||||
cout << matrice[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
bool verificaBordiUguali(int arr[NUM][NUM]){
|
||||
|
||||
int valoreBordo = arr[0][0];
|
||||
|
||||
for(size_t j =0; j<NUM; ++j){
|
||||
if(arr[0][j] != valoreBordo || arr[NUM-1][j] != valoreBordo){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
if(arr[i][0] != valoreBordo || arr[i][NUM-1] != valoreBordo){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
|
||||
int bobby[NUM][NUM]{
|
||||
1,1,1,1,
|
||||
1,0,0,1,
|
||||
1,0,0,1,
|
||||
1,1,1,1
|
||||
};
|
||||
stampaMatrice(bobby);
|
||||
verificaBordiUguali(bobby);
|
||||
if(verificaBordiUguali(bobby))
|
||||
cout << endl<< "La matrice ha tutti i bordi uguali";
|
||||
else
|
||||
cout << "no";
|
||||
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Verifica se tutte le celle dei “bordi” contengono lo stesso valore
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define NUM 4
|
||||
using namespace std;
|
||||
void stampaMatrice(int matrice[NUM][NUM]) {
|
||||
cout << "Matrice:\n";
|
||||
for (int i = 0; i < NUM; ++i) {
|
||||
for (int j = 0; j < NUM; ++j) {
|
||||
cout << matrice[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
bool verificaBordiUguali(int arr[NUM][NUM]){
|
||||
|
||||
int valoreBordo = arr[0][0];
|
||||
|
||||
for(size_t j =0; j<NUM; ++j){
|
||||
if(arr[0][j] != valoreBordo || arr[NUM-1][j] != valoreBordo){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
if(arr[i][0] != valoreBordo || arr[i][NUM-1] != valoreBordo){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
|
||||
int bobby[NUM][NUM]{
|
||||
1,1,1,1,
|
||||
1,0,0,1,
|
||||
1,0,0,1,
|
||||
1,1,1,1
|
||||
};
|
||||
stampaMatrice(bobby);
|
||||
verificaBordiUguali(bobby);
|
||||
if(verificaBordiUguali(bobby))
|
||||
cout << endl<< "La matrice ha tutti i bordi uguali";
|
||||
else
|
||||
cout << "no";
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Programma con una matrice che conta Pari e Dispari
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define DEF 2
|
||||
using namespace std;
|
||||
void riempi(int arr[DEF][DEF]);
|
||||
void stampa(int arr[DEF][DEF]);
|
||||
void contaPariEDispari(int arr[DEF][DEF], int pari, int dispari){
|
||||
for(size_t i = 0; i<DEF; ++i){
|
||||
for(size_t j = 0; j<DEF; ++j){
|
||||
if(arr[i][j] % 2 == 0){
|
||||
pari+=1;
|
||||
}
|
||||
else
|
||||
dispari+=1;
|
||||
}
|
||||
}
|
||||
cout << "In questa matrice ci sono "<< pari << " numeri pari e "<< dispari << " numeri dispari";
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
|
||||
int bobby[DEF][DEF];
|
||||
int npari = 0;
|
||||
int ndispari = 0;
|
||||
|
||||
riempi(bobby);
|
||||
stampa(bobby);
|
||||
contaPariEDispari(bobby, npari,ndispari);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void stampa(int arr[DEF][DEF]){
|
||||
for(size_t i = 0; i<DEF; ++i){
|
||||
for(size_t j = 0; j<DEF; ++j){
|
||||
cout << setw(4) << arr[i][j] ;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
void riempi(int arr[DEF][DEF]){
|
||||
for(size_t i = 0; i<DEF; ++i){
|
||||
for(size_t j = 0; j<DEF; ++j){
|
||||
arr[i][j] = rand() % 20;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Fare una matrice identità
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#define LATO 6
|
||||
using namespace std;
|
||||
|
||||
void riempi(int arr[LATO] [LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
if(i==j){
|
||||
arr[i] [j] = 1;
|
||||
}
|
||||
else
|
||||
arr[i] [j] = 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void stampa(int arr[LATO] [LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
cout << setw(3) << arr[i] [j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
int arr[LATO] [LATO];
|
||||
riempi(arr);
|
||||
stampa(arr);
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Matrice triangolare (sono nulli tutti gli elementi superiori alla diagonale principale)
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <time.h>
|
||||
#include <cstdlib>
|
||||
#define LATO 5
|
||||
using namespace std;
|
||||
|
||||
void triangolare(int arr[LATO] [LATO]){
|
||||
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
if(i==j || i>j){
|
||||
arr[i] [j] = rand() % ((20-3)+1)+3;
|
||||
}
|
||||
else if(i<j){
|
||||
arr[i] [j] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void stampa(int arr[LATO] [LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
cout << setw(3) << arr[i] [j] << " ";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
srand(time(NULL));
|
||||
int arr[LATO] [LATO];
|
||||
triangolare(arr);
|
||||
stampa(arr);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
La funzione mettiAZeroPariDispari prende un
|
||||
array di numeri interi, chiamato v e ne elimina
|
||||
tutte le occorrenze di un valore pari.
|
||||
Alla fine del processo, l'array originale
|
||||
è modificato, contenendo ora solo elementi dispari e zero negli altri
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int mettiAZeroPariDispari(int arr[], size_t size){
|
||||
for(size_t i = 0; i<size; i++){
|
||||
if(arr[i] % 2 == 0){
|
||||
arr[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
|
||||
|
||||
}
|
||||
int stampa(int arr[], size_t size){
|
||||
for(size_t i = 0; i<size; i++){
|
||||
cout << arr[i] << " ";
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
int main(void){
|
||||
int v[10] = {4,5,9,12,23,45,67,46,48,56};
|
||||
|
||||
stampa(v,10);
|
||||
cout << endl;
|
||||
mettiAZeroPariDispari(v, 10);
|
||||
stampa(v,10);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
// ruota il bordo di una matrice
|
||||
#include <iostream>
|
||||
#define RIGHE 4
|
||||
#define COLONNE 4
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Funzione per stampare la matrice
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE]) {
|
||||
for (int i = 0; i < RIGHE; ++i) {
|
||||
for (int j = 0; j < COLONNE; ++j) {
|
||||
cout << matrice[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// Funzione per ruotare il bordo della matrice in-place
|
||||
void ruotaBordoInPlace(int matrice[RIGHE][COLONNE]) {
|
||||
// Verifica che la matrice sia abbastanza grande per avere un bordo da ruotare
|
||||
if (RIGHE < 2 || COLONNE < 2) return;
|
||||
|
||||
// Salva temporaneamente il primo elemento
|
||||
int temp = matrice[0][0];
|
||||
|
||||
// Spostamento orario in senso del bordo
|
||||
// Prima riga
|
||||
for (int j = 0; j < COLONNE - 1; ++j) {
|
||||
matrice[0][j] = matrice[0][j + 1];
|
||||
}
|
||||
|
||||
// Ultima colonna
|
||||
for (int i = 0; i < RIGHE - 1; ++i) {
|
||||
matrice[i][COLONNE - 1] = matrice[i + 1][COLONNE - 1];
|
||||
}
|
||||
|
||||
// Ultima riga
|
||||
for (int j = COLONNE - 1; j > 0; --j) {
|
||||
matrice[RIGHE - 1][j] = matrice[RIGHE - 1][j - 1];
|
||||
}
|
||||
|
||||
// Prima colonna
|
||||
for (int i = RIGHE - 1; i > 0; --i) {
|
||||
matrice[i][0] = matrice[i - 1][0];
|
||||
}
|
||||
|
||||
// Ripristina il primo elemento salvato in fondo alla colonna
|
||||
matrice[1][0] = temp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Matrice predefinita
|
||||
int matrice[RIGHE][COLONNE] = {
|
||||
{1, 2, 3, 4},
|
||||
{5, 6, 7, 8},
|
||||
{9, 10, 11, 12},
|
||||
{13, 14, 15, 16}
|
||||
};
|
||||
|
||||
cout << "Matrice originale:\n";
|
||||
stampaMatrice(matrice);
|
||||
|
||||
// Ruota il bordo in senso orario in-place
|
||||
ruotaBordoInPlace(matrice);
|
||||
|
||||
cout << "Matrice dopo la rotazione del bordo (in-place):\n";
|
||||
stampaMatrice(matrice);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Tavola pitagorica
|
||||
// Righe nel ciclo esterno, colonne nel ciclo interno (righe i, colonne j)
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#define LATO 10
|
||||
|
||||
using namespace std;
|
||||
void riempi(int arr[LATO] [LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j)
|
||||
arr[i][j] = (i + 1) * (j + 1);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void stampa(int arr[LATO] [LATO]){
|
||||
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
cout << setw(5) << arr[i] [j];
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
int mat[LATO] [LATO];
|
||||
riempi(mat);
|
||||
stampa(mat);
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
// Cristian Ronzoni 3Ain
|
||||
/* Funzione che in una matrice
|
||||
trova la riga o la colonna con somma più alta */
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define NUM 7
|
||||
using namespace std;
|
||||
|
||||
// Dichiarazione delle funzioni
|
||||
void riempi(int arr[NUM][NUM]);
|
||||
void stampa(int arr[NUM][NUM]);
|
||||
void sommaRigaColonna(int arr[NUM][NUM]);
|
||||
|
||||
int main(void) {
|
||||
int bobby[NUM][NUM];
|
||||
|
||||
srand(time(0)); // Inizializza il generatore di numeri casuali
|
||||
riempi(bobby); // Riempie la matrice con numeri casuali
|
||||
stampa(bobby); // Stampa la matrice
|
||||
sommaRigaColonna(bobby); // Trova la riga e la colonna con la somma più alta
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Funzione per riempire la matrice con numeri casuali
|
||||
void riempi(int arr[NUM][NUM]) {
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
arr[i][j] = rand() % 50; // Numeri casuali tra 0 e 49
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per stampare la matrice
|
||||
void stampa(int arr[NUM][NUM]) {
|
||||
cout << "Matrice generata:\n";
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
cout << setw(3) << arr[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// Funzione per trovare la riga e la colonna con somma più alta
|
||||
void sommaRigaColonna(int arr[NUM][NUM]) {
|
||||
int maxSommaRiga = 0, maxSommaColonna = 0;
|
||||
int rigaMax = -1, colonnaMax = -1;
|
||||
|
||||
// Calcolo della somma delle righe
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
int sommaRiga = 0;
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
sommaRiga += arr[i][j];
|
||||
}
|
||||
if (sommaRiga > maxSommaRiga) {
|
||||
maxSommaRiga = sommaRiga;
|
||||
rigaMax = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Calcolo della somma delle colonne
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
int sommaColonna = 0;
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
sommaColonna += arr[i][j];
|
||||
}
|
||||
if (sommaColonna > maxSommaColonna) {
|
||||
maxSommaColonna = sommaColonna;
|
||||
colonnaMax = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Stampa dei risultati
|
||||
cout << "La riga con somma più alta è la riga " << rigaMax + 1
|
||||
<< " con somma: " << maxSommaRiga << endl;
|
||||
cout << "La colonna con somma più alta è la colonna " << colonnaMax + 1
|
||||
<< " con somma: " << maxSommaColonna << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
// Cristian Ronzoni 3Ain
|
||||
/* Funzione che in una matrice
|
||||
trova la riga o la colonna con somma più alta */
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define NUM 7
|
||||
using namespace std;
|
||||
|
||||
// Dichiarazione delle funzioni
|
||||
void riempi(int arr[NUM][NUM]);
|
||||
void stampa(int arr[NUM][NUM]);
|
||||
void sommaRigaColonna(int arr[NUM][NUM]);
|
||||
|
||||
int main(void) {
|
||||
int bobby[NUM][NUM];
|
||||
|
||||
srand(time(0)); // Inizializza il generatore di numeri casuali
|
||||
riempi(bobby); // Riempie la matrice con numeri casuali
|
||||
stampa(bobby); // Stampa la matrice
|
||||
sommaRigaColonna(bobby); // Trova la riga e la colonna con la somma più alta
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Funzione per riempire la matrice con numeri casuali
|
||||
void riempi(int arr[NUM][NUM]) {
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
arr[i][j] = rand() % 50; // Numeri casuali tra 0 e 49
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per stampare la matrice
|
||||
void stampa(int arr[NUM][NUM]) {
|
||||
cout << "Matrice generata:\n";
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
cout << setw(3) << arr[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// Funzione per trovare la riga e la colonna con somma più alta
|
||||
void sommaRigaColonna(int arr[NUM][NUM]) {
|
||||
int maxSommaRiga = 0, maxSommaColonna = 0;
|
||||
int rigaMax = -1, colonnaMax = -1;
|
||||
|
||||
// Calcolo della somma delle righe
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
int sommaRiga = 0;
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
sommaRiga += arr[i][j];
|
||||
}
|
||||
if (sommaRiga > maxSommaRiga) {
|
||||
maxSommaRiga = sommaRiga;
|
||||
rigaMax = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Calcolo della somma delle colonne
|
||||
for (size_t j = 0; j < NUM; ++j) {
|
||||
int sommaColonna = 0;
|
||||
for (size_t i = 0; i < NUM; ++i) {
|
||||
sommaColonna += arr[i][j];
|
||||
}
|
||||
if (sommaColonna > maxSommaColonna) {
|
||||
maxSommaColonna = sommaColonna;
|
||||
colonnaMax = j;
|
||||
}
|
||||
}
|
||||
|
||||
// Stampa dei risultati
|
||||
cout << "La riga con somma più alta è la riga " << rigaMax + 1
|
||||
<< " con somma: " << maxSommaRiga << endl;
|
||||
cout << "La colonna con somma più alta è la colonna " << colonnaMax + 1
|
||||
<< " con somma: " << maxSommaColonna << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void sommaDiagonali(int matrice[3][3], int n) {
|
||||
int sommaDiagonalePrincipale = 0;
|
||||
int sommaDiagonaleSecondaria = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
sommaDiagonalePrincipale += matrice[i][i];
|
||||
sommaDiagonaleSecondaria += matrice[i][n - 1 - i];
|
||||
}
|
||||
|
||||
cout << "Somma della diagonale principale: " << sommaDiagonalePrincipale << endl;
|
||||
cout << "Somma della diagonale secondaria: " << sommaDiagonaleSecondaria << endl;
|
||||
|
||||
if (sommaDiagonalePrincipale > sommaDiagonaleSecondaria) {
|
||||
cout << "La somma della diagonale principale è maggiore." << endl;
|
||||
} else if (sommaDiagonalePrincipale < sommaDiagonaleSecondaria) {
|
||||
cout << "La somma della diagonale secondaria è maggiore." << endl;
|
||||
} else {
|
||||
cout << "Le somme delle diagonali sono uguali." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
sommaDiagonali(matrice, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
//Patriche Robert Cosmin 3Ain
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
void contaPariDispari(int arr[], int size, int &pari, int &dispari, double &mediaPari, double &mediaDispari);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
int pari = 0, dispari = 0;
|
||||
double mediaPari = 0, mediaDispari = 0;
|
||||
contaPariDispari(numeri, DIM, pari, dispari, mediaPari, mediaDispari);
|
||||
|
||||
cout << "Elementi pari: " << pari << ", Media pari: " << mediaPari << endl;
|
||||
cout << "Elementi dispari: " << dispari << ", Media dispari: " << mediaDispari << endl;
|
||||
if (mediaPari > mediaDispari)
|
||||
cout << "La media dei pari è maggiore." << endl;
|
||||
else if (mediaDispari > mediaPari)
|
||||
cout << "La media dei dispari è maggiore." << endl;
|
||||
else
|
||||
cout << "Le medie sono uguali." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void contaPariDispari(int arr[], int size, int &pari, int &dispari, double &mediaPari, double &mediaDispari) {
|
||||
int sommaPari = 0, sommaDispari = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 2 == 0) {
|
||||
pari++;
|
||||
sommaPari += arr[i];
|
||||
} else {
|
||||
dispari++;
|
||||
sommaDispari += arr[i];
|
||||
}
|
||||
}
|
||||
if (pari > 0) mediaPari = static_cast<double>(sommaPari) / pari;
|
||||
if (dispari > 0) mediaDispari = static_cast<double>(sommaDispari) / dispari;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
double calcolaMedia(int arr[], int size);
|
||||
int contaMaggiori(int arr[], int size, double media);
|
||||
void trovaMinori(int arr[], int size, double media);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
double media = calcolaMedia(numeri, DIM);
|
||||
cout << "Media: " << media << endl;
|
||||
|
||||
int maggiori = contaMaggiori(numeri, DIM, media);
|
||||
cout << "Elementi maggiori della media: " << maggiori << endl;
|
||||
|
||||
cout << "Indici degli elementi minori della media: ";
|
||||
trovaMinori(numeri, DIM, media);
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double calcolaMedia(int arr[], int size) {
|
||||
double somma = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
somma += arr[i];
|
||||
}
|
||||
return somma / size;
|
||||
}
|
||||
|
||||
int contaMaggiori(int arr[], int size, double media) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] > media) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void trovaMinori(int arr[], int size, double media) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] < media) cout << i << " ";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void heapify(int arr[], int n, int i) {
|
||||
int largest = i;
|
||||
int left = 2 * i + 1;
|
||||
int right = 2 * i + 2;
|
||||
|
||||
if (left < n && arr[left] > arr[largest])
|
||||
largest = left;
|
||||
|
||||
if (right < n && arr[right] > arr[largest])
|
||||
largest = right;
|
||||
|
||||
if (largest != i) {
|
||||
swap(arr[i], arr[largest]);
|
||||
heapify(arr, n, largest);
|
||||
}
|
||||
}
|
||||
|
||||
void heapSort(int arr[], int n) {
|
||||
for (int i = n / 2 - 1; i >= 0; i--) {
|
||||
heapify(arr, n, i);
|
||||
}
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
swap(arr[0], arr[i]);
|
||||
heapify(arr, i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {12, 11, 13, 5, 6, 7};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
heapSort(arr, n);
|
||||
cout << "Sorted array: ";
|
||||
printArray(arr, n);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
int sum = 0;
|
||||
double avg;
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
sum += arr[i];
|
||||
}
|
||||
|
||||
avg = sum / 15.0; // Calcolo della media
|
||||
|
||||
// Stampa degli indici dei numeri minori della media
|
||||
cout << "Indici dei numeri < della media (" << avg << "): ";
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (arr[i] < avg) {
|
||||
cout << i << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void insertionSort(int arr[], int n) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
int key = arr[i];
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && arr[j] > key) {
|
||||
arr[j + 1] = arr[j];
|
||||
j--;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {12, 11, 13, 5, 6};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
insertionSort(arr, n);
|
||||
cout << "Sorted array: ";
|
||||
printArray(arr, n);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
void massimoMedieColonneMediaMassimiRighe(int matrice[3][3], int righe, int colonne) {
|
||||
double maxMediaColonne = -INT_MAX;
|
||||
int sommaMassimiRighe = 0;
|
||||
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
double sommaColonna = 0;
|
||||
for (int i = 0; i < righe; i++) {
|
||||
sommaColonna += matrice[i][j];
|
||||
}
|
||||
double mediaColonna = sommaColonna / righe;
|
||||
if (mediaColonna > maxMediaColonne) {
|
||||
maxMediaColonne = mediaColonna;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
int maxRiga = matrice[i][0];
|
||||
for (int j = 1; j < colonne; j++) {
|
||||
if (matrice[i][j] > maxRiga) {
|
||||
maxRiga = matrice[i][j];
|
||||
}
|
||||
}
|
||||
sommaMassimiRighe += maxRiga;
|
||||
}
|
||||
|
||||
double mediaMassimiRighe = static_cast<double>(sommaMassimiRighe) / righe;
|
||||
cout << "Massimo delle medie sulle colonne: " << maxMediaColonne << endl;
|
||||
cout << "Media dei massimi sulle righe: " << mediaMassimiRighe << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
massimoMedieColonneMediaMassimiRighe(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
//Matrice con 10% di probabilità di riempimento
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#define LUN 8
|
||||
using namespace std;
|
||||
|
||||
|
||||
void stampa(int arr[LUN] [LUN] , size_t size){
|
||||
for(size_t i = 0; i<size; ++i){
|
||||
for(size_t j = 0; j<size; ++j){
|
||||
cout << setw(3) << arr[i] [j];
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void riempi(int arr[LUN] [LUN], size_t size){
|
||||
for(size_t i = 0; i<size; ++i){
|
||||
for(size_t j = 0; j<size; ++j){
|
||||
int a = rand() % 10;
|
||||
if(a==1)
|
||||
arr[i] [j] = a;
|
||||
else
|
||||
arr[i] [j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(void){
|
||||
|
||||
srand(time(NULL));
|
||||
int arr[LUN] [LUN];
|
||||
|
||||
riempi(arr, LUN);
|
||||
stampa(arr, LUN);
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
//Patriche Robert Cosmin
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
double calcolaMedia(int arr[], int size);
|
||||
int trovaMassimo(int arr[], int size);
|
||||
int trovaMinimo(int arr[], int size);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int voti[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array dei voti: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
voti[i] = rand() % 31; // Voti tra 0 e 30
|
||||
cout << voti[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
double media = calcolaMedia(voti, DIM);
|
||||
int massimo = trovaMassimo(voti, DIM);
|
||||
int minimo = trovaMinimo(voti, DIM);
|
||||
|
||||
cout << "Media dei voti: " << media << endl;
|
||||
cout << "Voto massimo: " << massimo << endl;
|
||||
cout << "Voto minimo: " << minimo << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double calcolaMedia(int arr[], int size) {
|
||||
double somma = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
somma += arr[i];
|
||||
}
|
||||
return somma / size;
|
||||
}
|
||||
|
||||
int trovaMassimo(int arr[], int size) {
|
||||
int massimo = arr[0];
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (arr[i] > massimo) massimo = arr[i];
|
||||
}
|
||||
return massimo;
|
||||
}
|
||||
|
||||
int trovaMinimo(int arr[], int size) {
|
||||
int minimo = arr[0];
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (arr[i] < minimo) minimo = arr[i];
|
||||
}
|
||||
return minimo;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
double calcolaMediaMultipli17(int arr[], int size);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
double media = calcolaMediaMultipli17(numeri, DIM);
|
||||
if (media == -1)
|
||||
cout << "Non ci sono multipli di 17." << endl;
|
||||
else
|
||||
cout << "Media dei multipli di 17: " << media << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double calcolaMediaMultipli17(int arr[], int size) {
|
||||
int somma = 0, count = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 17 == 0) {
|
||||
somma += arr[i];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) return -1; // Nessun multiplo di 17
|
||||
return somma / count;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
double calcolaMediaMultipli3(int arr[], int size);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
double media = calcolaMediaMultipli3(numeri, DIM);
|
||||
if (media == -1)
|
||||
cout << "Non ci sono multipli di 3." << endl;
|
||||
else
|
||||
cout << "Media dei multipli di 3: " << media << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
double calcolaMediaMultipli3(int arr[], int size) {
|
||||
int somma = 0, count = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] % 3 == 0) {
|
||||
somma += arr[i];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) return -1; // Nessun multiplo di 3
|
||||
return static_cast<double>(somma) / count;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void moltiplicaMatrice(int matrice[3][3], int numero, int righe, int colonne) {
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
matrice[i][j] *= numero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
int numero = 2;
|
||||
moltiplicaMatrice(matrice, numero, 3, 3);
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
cout << matrice[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
}
|
||||
|
||||
// Stampa degli elementi che soddisfano le condizioni
|
||||
cout << "Elementi >20, <40 e multipli di 5: ";
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (arr[i] > 20 && arr[i] < 40 && arr[i] % 5 == 0) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
//Patriche Robert Cosmin 3Ain
|
||||
/* Dato un vettore di 15 numeri interi casuali, scrivere un programma che calcola la media aritmetica,
|
||||
poi la sostituisce a tutti gli elementi del vettore maggiori di essa.*/
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
// Funzione per calcolare la media aritmetica di un array
|
||||
double calcolaMedia(int arr[], int size) {
|
||||
double somma = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
somma += arr[i];
|
||||
}
|
||||
return somma / size;
|
||||
}
|
||||
|
||||
// Funzione per sostituire gli elementi maggiori della media
|
||||
void sostituisciConMedia(int arr[], int size, double media) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (arr[i] > media) {
|
||||
arr[i] = media; // Conversione a intero
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione principale
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
|
||||
// Inizializza il generatore di numeri casuali
|
||||
srand((time(0)));
|
||||
|
||||
// Popola l'array con numeri casuali tra 1 e 100
|
||||
cout << "Array originale: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
// Calcola la media
|
||||
double media = calcolaMedia(numeri, DIM);
|
||||
cout << "Media aritmetica: " << media << endl;
|
||||
|
||||
// Sostituisce gli elementi maggiori della media
|
||||
sostituisciConMedia(numeri, DIM, media);
|
||||
|
||||
// Stampa l'array modificato
|
||||
cout << "Array modificato: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
//Patriche Robert Cosmin
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
int k, count = 0;
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
}
|
||||
|
||||
// Input valore k
|
||||
cout << "Inserisci il valore k: ";
|
||||
cin >> k;
|
||||
|
||||
// Contare gli elementi maggiori di k
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (arr[i] > k) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Stampa del risultato
|
||||
cout << "Numero di elementi maggiori di " << k << ": " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
int n;
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
}
|
||||
|
||||
// Input valore
|
||||
cout << "Inserisci un valore: ";
|
||||
cin >> n;
|
||||
|
||||
// Stampa dei numeri pari minori del valore
|
||||
cout << "Numeri pari minori di " << n << ": ";
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (arr[i] % 2 == 0 && arr[i] < n) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
//Patriche Robert
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15], newArr[8];
|
||||
int j = 0;
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
}
|
||||
|
||||
// Copia gli elementi di indice pari in newArr
|
||||
for (int i = 0; i < 15; i += 2) {
|
||||
newArr[j++] = arr[i];
|
||||
}
|
||||
|
||||
// Stampa del nuovo array
|
||||
cout << "Nuovo array con indici pari: ";
|
||||
for (int i = 0; i < j; ++i) {
|
||||
cout << newArr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// Funzione per eseguire la One-Hot Encoding
|
||||
vector<vector<int>> oneHotEncoding(const vector<string>& categorie, const vector<string>& etichette) {
|
||||
vector<vector<int>> matriceOneHot;
|
||||
|
||||
// Per ogni etichetta, troviamo la sua posizione e creiamo un vettore one-hot
|
||||
for (const string& etichetta : etichette) {
|
||||
vector<int> oneHot(categorie.size(), 0); // Inizializza un vettore di zeri
|
||||
for (int i = 0; i < categorie.size(); ++i) {
|
||||
if (categorie[i] == etichetta) {
|
||||
oneHot[i] = 1; // Imposta la posizione corrispondente a "1"
|
||||
break;
|
||||
}
|
||||
}
|
||||
matriceOneHot.push_back(oneHot); // Aggiunge il vettore one-hot alla matrice
|
||||
}
|
||||
|
||||
return matriceOneHot;
|
||||
}
|
||||
|
||||
// Funzione per stampare una matrice
|
||||
void stampaMatrice(const vector<vector<int>>& matrice) {
|
||||
for (const auto& riga : matrice) {
|
||||
for (int valore : riga) {
|
||||
cout << valore << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Definiamo le categorie possibili
|
||||
vector<string> categorie = {"Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato", "Domenica"};
|
||||
|
||||
// Definiamo alcune etichette da codificare
|
||||
vector<string> etichette = {"Lunedì", "Mercoledì", "Domenica", "Venerdì", "Martedì"};
|
||||
|
||||
// Otteniamo la matrice one-hot encoding
|
||||
vector<vector<int>> matriceOneHot = oneHotEncoding(categorie, etichette);
|
||||
|
||||
// Stampiamo la matrice risultante
|
||||
cout << "Matrice One-Hot Encoding:" << endl;
|
||||
stampaMatrice(matriceOneHot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
//Patriche Robert Cosmin 3Ain
|
||||
/*
|
||||
Dato un vettore di 15 numeri interi casuali, produrre in output il vettore inverso
|
||||
(mirrored). [Nota: si puo ottenere il risultato in due diversi modi: un tenendo l'array
|
||||
in sola lettura, l'altro modificando i valori ma non la lunghezza]
|
||||
. Es: 10,8,7,9,6,9,1,8,2,7,1,2,3,4,5 ? 5,4,3,2,1,7,2,8,1,9,6,9,7,8,10.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define DIM 15
|
||||
#define MAX 100
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
void inserisciVettore(int vect[DIM],size_t size){
|
||||
for(size_t i = 0; i < size; i++){
|
||||
vect[i] = rand()% MAX;
|
||||
}
|
||||
}
|
||||
|
||||
void stampaVettore(int vect[DIM],size_t size){
|
||||
for(size_t i = 0; i < size; i++ ){
|
||||
cout << vect[i] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
void vettoreInverso(int vect[DIM],size_t size){
|
||||
for(size_t i = size; i > 0; i--){
|
||||
cout << vect[i - 1] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
|
||||
int vect[DIM];
|
||||
|
||||
inserisciVettore(vect,DIM);
|
||||
|
||||
cout << "Vettore originale:";
|
||||
stampaVettore(vect,DIM);
|
||||
cout << endl << endl << endl;
|
||||
|
||||
cout << "Vettore invertito:";
|
||||
vettoreInverso(vect,DIM);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[high];
|
||||
int i = low - 1;
|
||||
|
||||
for (int j = low; j < high; j++) {
|
||||
if (arr[j] < pivot) {
|
||||
i++;
|
||||
swap(arr[i], arr[j]);
|
||||
}
|
||||
}
|
||||
swap(arr[i + 1], arr[high]);
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int pi = partition(arr, low, high);
|
||||
|
||||
quickSort(arr, low, pi - 1);
|
||||
quickSort(arr, pi + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {10, 80, 30, 90, 40, 50, 70};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
quickSort(arr, 0, n - 1);
|
||||
cout << "Sorted array: ";
|
||||
printArray(arr, n);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int contaOccorrenze(int matrice[3][3], int val, int righe, int colonne) {
|
||||
int conta = 0;
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
if (matrice[i][j] == val) {
|
||||
conta++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return conta;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 1}, {6, 1, 7}};
|
||||
int val = 1;
|
||||
cout << "Occorrenze di " << val << ": " << contaOccorrenze(matrice, val, 3, 3) << endl;
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//Patriche Robert Cosmin
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int ricercaVettore(int arr[], int n, int valore) {
|
||||
// Ricerca in entrambi i versi
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (arr[i] == valore) return i;
|
||||
}
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
if (arr[i] == valore) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {10, 20, 30, 40, 50};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
int valore = 30;
|
||||
|
||||
int indice = ricercaVettore(arr, n, valore);
|
||||
|
||||
if (indice != -1) {
|
||||
cout << "Valore trovato all'indice: " << indice << endl;
|
||||
} else {
|
||||
cout << "Valore non trovato." << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void ricercaMatrice(int matrice[3][3], int righe, int colonne) {
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
if (matrice[i][j] == 13) {
|
||||
cout << "Trovato 13 in posizione: (" << i << ", " << j << ")" << endl;
|
||||
}
|
||||
if (matrice[i][j] % 17 == 0) {
|
||||
cout << "Trovato multiplo di 17: " << matrice[i][j] << " in posizione: (" << i << ", " << j << ")" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 13, 34}, {17, 5, 23}, {51, 17, 9}};
|
||||
ricercaMatrice(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void scambiaRigheColonne(int matrice[3][3], int righe, int colonne) {
|
||||
int trasposta[3][3];
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
trasposta[j][i] = matrice[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Matrice trasposta (righe scambiate con colonne): " << endl;
|
||||
for (int i = 0; i < colonne; i++) {
|
||||
for (int j = 0; j < righe; j++) {
|
||||
cout << trasposta[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
scambiaRigheColonne(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void selectionSort(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int minIdx = i;
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[j] < arr[minIdx]) {
|
||||
minIdx = j;
|
||||
}
|
||||
}
|
||||
swap(arr[i], arr[minIdx]);
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[] = {64, 25, 12, 22, 11};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
selectionSort(arr, n);
|
||||
cout << "Sorted array: ";
|
||||
printArray(arr, n);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int sommaCornice(int matrice[5][5], int n) {
|
||||
int somma = 0;
|
||||
|
||||
// Sommare i bordi della cornice
|
||||
for (int i = 0; i < n; i++) {
|
||||
somma += matrice[0][i]; // Prima riga
|
||||
somma += matrice[n - 1][i]; // Ultima riga
|
||||
somma += matrice[i][0]; // Prima colonna
|
||||
somma += matrice[i][n - 1]; // Ultima colonna
|
||||
}
|
||||
|
||||
// Rimuovere i doppioni (gli angoli)
|
||||
somma -= matrice[0][0]; // Angolo in alto a sinistra
|
||||
somma -= matrice[0][n - 1]; // Angolo in alto a destra
|
||||
somma -= matrice[n - 1][0]; // Angolo in basso a sinistra
|
||||
somma -= matrice[n - 1][n - 1]; // Angolo in basso a destra
|
||||
|
||||
return somma;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[5][5] = {{1, 2, 3, 4, 5},
|
||||
{6, 7, 8, 9, 10},
|
||||
{11, 12, 13, 14, 15},
|
||||
{16, 17, 18, 19, 20},
|
||||
{21, 22, 23, 24, 25}};
|
||||
int somma = sommaCornice(matrice, 5);
|
||||
cout << "Somma della cornice: " << somma << endl;
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void sommaScacchiera(int matrice[5][5], int n) {
|
||||
int sommaNera = 0, sommaBianca = 0;
|
||||
|
||||
// In una scacchiera, le celle nere sono quelle che soddisfano la condizione (i + j) % 2 == 0
|
||||
// Le celle bianche sono quelle che soddisfano (i + j) % 2 == 1
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
if ((i + j) % 2 == 0) {
|
||||
sommaNera += matrice[i][j]; // Cella nera
|
||||
} else {
|
||||
sommaBianca += matrice[i][j]; // Cella bianca
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Somma delle celle nere: " << sommaNera << endl;
|
||||
cout << "Somma delle celle bianche: " << sommaBianca << endl;
|
||||
|
||||
if (sommaNera > sommaBianca) {
|
||||
cout << "La somma delle celle nere è maggiore." << endl;
|
||||
} else {
|
||||
cout << "La somma delle celle bianche è maggiore." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[5][5] = {{1, 2, 3, 4, 5},
|
||||
{6, 7, 8, 9, 10},
|
||||
{11, 12, 13, 14, 15},
|
||||
{16, 17, 18, 19, 20},
|
||||
{21, 22, 23, 24, 25}};
|
||||
sommaScacchiera(matrice, 5);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void sommaMassimiColonne(int matrice[3][3], int righe, int colonne) {
|
||||
int somma = 0;
|
||||
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
int maxColonna = matrice[0][j];
|
||||
for (int i = 1; i < righe; i++) {
|
||||
if (matrice[i][j] > maxColonna) {
|
||||
maxColonna = matrice[i][j];
|
||||
}
|
||||
}
|
||||
somma += maxColonna;
|
||||
}
|
||||
|
||||
cout << "Somma degli elementi massimi di ogni colonna: " << somma << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
sommaMassimiColonne(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
//Patriche Robert
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int sommaIndiciPari(int arr[], int size);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
int somma = sommaIndiciPari(numeri, DIM);
|
||||
cout << "Somma degli elementi con indice pari: " << somma << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sommaIndiciPari(int arr[], int size) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (i % 2 == 0) {
|
||||
somma += arr[i];
|
||||
}
|
||||
}
|
||||
return somma;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
//Patriche Robert Cosmin
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
int sum = 0;
|
||||
|
||||
// Inizializzazione con i primi 15 numeri dispari
|
||||
for (int i = 0, num = 1; i < 15; ++i, num += 2) {
|
||||
arr[i] = num;
|
||||
sum += arr[i];
|
||||
}
|
||||
|
||||
// Somma degli elementi
|
||||
cout << "Somma degli elementi: " << sum << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void separaPariDispariCalcoli(int matrice[4][4], int righe, int colonne) {
|
||||
int pari[16], dispari[16];
|
||||
int countPari = 0, countDispari = 0;
|
||||
int sommaPari = 0, maxDispari = INT_MIN;
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
if (matrice[i][j] % 2 == 0) {
|
||||
pari[countPari++] = matrice[i][j];
|
||||
sommaPari += matrice[i][j];
|
||||
} else {
|
||||
dispari[countDispari++] = matrice[i][j];
|
||||
if (matrice[i][j] > maxDispari) {
|
||||
maxDispari = matrice[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Somma dei pari: " << sommaPari << endl;
|
||||
cout << "Massimo dei dispari: " << maxDispari << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
|
||||
separaPariDispariCalcoli(matrice, 4, 4);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void separaPariDispari(int matrice[3][3], int righe, int colonne) {
|
||||
int pari[9], dispari[9];
|
||||
int countPari = 0, countDispari = 0;
|
||||
|
||||
for (int i = 0; i < righe; i++) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
if (matrice[i][j] % 2 == 0) {
|
||||
pari[countPari++] = matrice[i][j];
|
||||
} else {
|
||||
dispari[countDispari++] = matrice[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "Numeri pari: ";
|
||||
for (int i = 0; i < countPari; i++) {
|
||||
cout << pari[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
cout << "Numeri dispari: ";
|
||||
for (int i = 0; i < countDispari; i++) {
|
||||
cout << dispari[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
|
||||
separaPariDispari(matrice, 3, 3);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int arr[15];
|
||||
int min, secondMin;
|
||||
|
||||
// Generazione numeri casuali
|
||||
srand(time(0));
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
arr[i] = rand() % 100 + 1; // Numeri casuali tra 1 e 100
|
||||
}
|
||||
|
||||
min = secondMin = 101; // Assumiamo un valore maggiore di 100
|
||||
|
||||
// Troviamo il minimo e il secondo minimo
|
||||
for (int i = 0; i < 15; ++i) {
|
||||
if (arr[i] < min) {
|
||||
secondMin = min;
|
||||
min = arr[i];
|
||||
} else if (arr[i] < secondMin && arr[i] != min) {
|
||||
secondMin = arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Stampa dei risultati
|
||||
cout << "Minimo: " << min << ", Secondo minimo: " << secondMin << endl;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
//Patriche Robert Cosmin 3Ain
|
||||
/*Data in input una sequenza di lunghezza predeterminata di n numeri interi, inserirli in un vettore.
|
||||
Creare una prima versione che carica il vettore da destra a sinistra, e una seconda versione che carica il vettore da sinistra a destr*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// Funzione per caricare un vettore da destra a sinistra
|
||||
void caricaDaDestra(std::vector<int>& v, int n) {
|
||||
cout << "Inserisci " << n << " numeri interi (da destra a sinistra):" << endl;
|
||||
for (int i = n - 1; i >= 0; --i) {
|
||||
cout << "Elemento " << n - i << ": ";
|
||||
cin >> v[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per caricare un vettore da sinistra a destra
|
||||
void caricaDaSinistra(std::vector<int>& v, int n) {
|
||||
cout << "Inserisci " << n << " numeri interi (da sinistra a destra):" << endl;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cout << "Elemento " << i + 1 << ": ";
|
||||
cin >> v[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per stampare il vettore
|
||||
void stampaVettore(const std::vector<int>& v) {
|
||||
cout << "Contenuto del vettore: ";
|
||||
for (const int& elem : v) {
|
||||
cout << elem << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cout << "Quanti numeri vuoi inserire? ";
|
||||
st:cin >> n;
|
||||
|
||||
// Creazione del vettore
|
||||
vector<int> vettore(n);
|
||||
|
||||
// Versione 1: Caricamento da destra a sinistra
|
||||
caricaDaDestra(vettore, n);
|
||||
stampaVettore(vettore);
|
||||
|
||||
// Versione 2: Caricamento da sinistra a destra
|
||||
caricaDaSinistra(vettore, n);
|
||||
stampaVettore(vettore);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
Leggere un array di interi di
|
||||
6 posizioni, leggere un ulteriore numero intero, una
|
||||
funzione deve restituire quanti numeri memorizzati
|
||||
nell’array sono inferiori e quanti superiori dell’ultimo numero letto.
|
||||
*/
|
||||
#include <iostream>
|
||||
#define DIM 6
|
||||
using namespace std;
|
||||
|
||||
int supInf(int arr[], size_t size, int num, int sup , int inf){
|
||||
|
||||
for(size_t i = 0; i<size; i++){
|
||||
if(arr[i] > num){
|
||||
sup++;
|
||||
}
|
||||
else if(arr[i] < num){
|
||||
inf++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
cout << endl << "Ci sono " << sup << " numeri maggiori a quello che hai inserito"<< endl;
|
||||
cout << "Ci sono "<< inf << " numeri inferiori a quello che hai inserito";
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
|
||||
int bobby[DIM];
|
||||
int superiore, inferiore;
|
||||
int x;
|
||||
cout <<"Inserisci un numero: ";
|
||||
cin >> x ;
|
||||
|
||||
|
||||
for(size_t i = 0; i<DIM; i++){
|
||||
cout << "Inserisci valore " << i+1 << ":"<< endl;
|
||||
cin >> bobby[i];
|
||||
|
||||
}
|
||||
supInf(bobby, DIM, x, superiore, inferiore);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
void bubbleSortArray(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
for (int j = 0; j < n - i - 1; j++) {
|
||||
if (arr[j] > arr[j + 1]) {
|
||||
// Scambio
|
||||
int temp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
void bubbleSortMatrice(int matrice[][6], int righe, int colonne) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
for (int i = 0; i < righe - 1; i++) {
|
||||
for (int k = 0; k < righe - i - 1; k++) {
|
||||
if (matrice[k][j] > matrice[k + 1][j]) {
|
||||
int temp = matrice[k][j];
|
||||
matrice[k][j] = matrice[k + 1][j];
|
||||
matrice[k + 1][j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#define ESC "\x1b"
|
||||
#define RIGHE 9
|
||||
#define COLONNE 9
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[][COLONNE], int righe, int colonne);
|
||||
void clockWise(int matrice[][COLONNE], int righe);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
for (int r = 0; r < 12; r++) {
|
||||
clockWise(matrice, righe);
|
||||
cout << ESC << "[H";
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
if (r <= righe / 2 && c == colonne / 2) {
|
||||
matrice[r][c] = 1;
|
||||
} else {
|
||||
matrice[r][c] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << matrice[r][c] << " ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clockWise(int matrice[][COLONNE], int righe) {
|
||||
for (int c = 0; c < righe/2; c++) {
|
||||
int temp = matrice[c][righe / 2];
|
||||
matrice[c][righe / 2] = matrice[c][c];
|
||||
matrice[c][c] = matrice[righe / 2][c];
|
||||
matrice[righe / 2][c] = matrice[righe - 1 - c][c];
|
||||
matrice[righe - 1 - c][c] = matrice[righe - 1 - c][righe / 2];
|
||||
matrice[righe - 1 - c][righe / 2] = matrice[righe - 1 - c][righe - 1 - c];
|
||||
matrice[righe - 1 - c][righe - 1 - c] = matrice[righe / 2][righe - 1 - c];
|
||||
matrice[righe/2][righe - 1 - c] = matrice[c][righe - 1 - c];
|
||||
matrice[c][righe - 1 - c] = matrice[c][righe / 2];
|
||||
matrice[c][righe - 1 - c] = temp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 13/12/2024
|
||||
|
||||
Contare i numeri pari e quelli dispari di una Matrice.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define RIGHE 4
|
||||
#define COLONNE 4
|
||||
#define MINIMO -9
|
||||
#define MASSIMO 9
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
int contaPari(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
int contaDispari(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
|
||||
cout << "Matrice: " << endl;
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
cout << "I numeri pari sono: " << contaPari(matrice, righe, colonne) << endl;
|
||||
cout << "I numeri dispari sono: " << contaDispari(matrice, righe, colonne) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
srand(time(NULL));
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
matrice[r][c] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << setw(5) << matrice[r][c];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
int contaPari(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
int contatorePari = 0;
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
if (matrice[r][c] % 2 == 0) {
|
||||
contatorePari++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return contatorePari;
|
||||
}
|
||||
|
||||
int contaDispari(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
int contatoreDispari = 0;
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
if (matrice[r][c] % 2 != 0) {
|
||||
contatoreDispari++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return contatoreDispari;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
// Funzione per aggiungere un elemento in coda
|
||||
void aggiungiInCoda(int arr[], int *n, int valore) {
|
||||
arr[*n] = valore;
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
// Funzione per aggiungere un elemento in testa
|
||||
void aggiungiInTesta(int arr[], int *n, int valore) {
|
||||
for (int i = *n; i > 0; i--) {
|
||||
arr[i] = arr[i - 1];
|
||||
}
|
||||
arr[0] = valore;
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
// Funzione per cancellare un elemento dalla coda
|
||||
void cancellaDallaCoda(int arr[], int *n) {
|
||||
if (*n > 0) {
|
||||
(*n)--;
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per cancellare un elemento dalla testa
|
||||
void cancellaDallaTesta(int arr[], int *n) {
|
||||
for (int i = 0; i < *n - 1; i++) {
|
||||
arr[i] = arr[i + 1];
|
||||
}
|
||||
(*n)--;
|
||||
}
|
||||
|
||||
// Funzione per fare shifting a destra
|
||||
void shiftDx(int arr[], int *n) {
|
||||
for (int i = *n; i > 0; i--) {
|
||||
arr[i] = arr[i - 1];
|
||||
}
|
||||
(*n)++;
|
||||
}
|
||||
|
||||
// Funzione per fare shifting a sinistra
|
||||
void shiftSx(int arr[], int *n) {
|
||||
for (int i = 0; i < *n - 1; i++) {
|
||||
arr[i] = arr[i + 1];
|
||||
}
|
||||
(*n)--;
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Funzione per calcolare la somma degli elementi
|
||||
int somma(int arr[], int n) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
somma += arr[i];
|
||||
}
|
||||
return somma;
|
||||
}
|
||||
|
||||
// Funzione per calcolare il prodotto degli elementi
|
||||
int prodotto(int arr[], int n) {
|
||||
int prodotto = 1;
|
||||
for (int i = 0; i < n; i++) {
|
||||
prodotto *= arr[i];
|
||||
}
|
||||
return prodotto;
|
||||
}
|
||||
|
||||
// Funzione per trovare il valore minimo
|
||||
int min(int arr[], int n) {
|
||||
int minimo = arr[0];
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] < minimo) {
|
||||
minimo = arr[i];
|
||||
}
|
||||
}
|
||||
return minimo;
|
||||
}
|
||||
|
||||
// Funzione per trovare il valore massimo
|
||||
int max(int arr[], int n) {
|
||||
int massimo = arr[0];
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] > massimo) {
|
||||
massimo = arr[i];
|
||||
}
|
||||
}
|
||||
return massimo;
|
||||
}
|
||||
|
||||
// Funzione per calcolare la media
|
||||
float media(int arr[], int n) {
|
||||
return (float)somma(arr, n) / n;
|
||||
}
|
||||
|
||||
// Funzione per verificare se l'array è palindromo
|
||||
bool isPalindrome(int arr[], int n) {
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
if (arr[i] != arr[n - 1 - i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Funzione per verificare se l'array è ordinato in modo crescente
|
||||
bool isSorted(int arr[], int n) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (arr[i] < arr[i - 1]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Funzione per cercare un elemento
|
||||
int ricerca(int arr[], int n, int valore) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (arr[i] == valore) {
|
||||
return i; // Indice dell'elemento trovato
|
||||
}
|
||||
}
|
||||
return -1; // Elemento non trovato
|
||||
}
|
||||
|
||||
// Funzione per fare il mirroring di un array
|
||||
void mirroring(int arr[], int n) {
|
||||
for (int i = 0; i < n / 2; i++) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[n - 1 - i];
|
||||
arr[n - 1 - i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per scambiare posizioni pari e dispari
|
||||
void scambiaPariDispari(int arr[], int n) {
|
||||
for (int i = 1; i < n; i += 2) {
|
||||
int temp = arr[i - 1];
|
||||
arr[i - 1] = arr[i];
|
||||
arr[i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per ruotare l'array a destra
|
||||
void ruotaDx(int arr[], int n) {
|
||||
int ultimo = arr[n - 1];
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
arr[i] = arr[i - 1];
|
||||
}
|
||||
arr[0] = ultimo;
|
||||
}
|
||||
|
||||
// Funzione per ruotare l'array a sinistra
|
||||
void ruotaSx(int arr[], int n) {
|
||||
int primo = arr[0];
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
arr[i] = arr[i + 1];
|
||||
}
|
||||
arr[n - 1] = primo;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
void insertionSortArray(int arr[], int n) {
|
||||
for (int i = 1; i < n; i++) {
|
||||
int key = arr[i];
|
||||
int j = i - 1;
|
||||
|
||||
while (j >= 0 && arr[j] > key) {
|
||||
arr[j + 1] = arr[j];
|
||||
j = j - 1;
|
||||
}
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
void insertionSortMatrice(int matrice[][6], int righe, int colonne) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
for (int i = 1; i < righe; i++) {
|
||||
int key = matrice[i][j];
|
||||
int k = i - 1;
|
||||
while (k >= 0 && matrice[k][j] > key) {
|
||||
matrice[k + 1][j] = matrice[k][j];
|
||||
k = k - 1;
|
||||
}
|
||||
matrice[k + 1][j] = key;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#define MAX 100
|
||||
#define MIN 0
|
||||
#define CAPACITY 10
|
||||
using namespace std;
|
||||
|
||||
void invertiArray(int array[], int lunghezza) {
|
||||
int inizio = 0;
|
||||
int fine = lunghezza - 1;
|
||||
while (inizio < fine) {
|
||||
// Scambio degli elementi
|
||||
int temp = array[inizio];
|
||||
array[inizio] = array[fine];
|
||||
array[fine] = temp;
|
||||
|
||||
// Aggiorna gli indici
|
||||
inizio++;
|
||||
fine--;
|
||||
}
|
||||
}
|
||||
|
||||
void stampaArray(int array[], int lunghezza) {
|
||||
for (int i = 0; i < lunghezza; i++) {
|
||||
printf("%d ", array[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void riempiarray(int array[], size_t lunghezza){
|
||||
for(size_t j=0; j < lunghezza; j++)
|
||||
array[j]=rand()%(MAX-MIN+1)+MIN;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int array[CAPACITY];
|
||||
|
||||
riempiarray(array,CAPACITY);
|
||||
|
||||
printf("Array originale: ");
|
||||
stampaArray(array, CAPACITY);
|
||||
|
||||
invertiArray(array, CAPACITY);
|
||||
|
||||
printf("Array invertito: ");
|
||||
stampaArray(array, CAPACITY);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*Cristian Ronzoni 3Ain
|
||||
generare una matrice antidiagonale
|
||||
con valori casuali */
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#define DIA 5
|
||||
#define MAX 9
|
||||
#define MIN 1
|
||||
using namespace std;
|
||||
|
||||
void antiDia(int arr[DIA] [DIA]){
|
||||
for(size_t i = 0; i<DIA; ++i){
|
||||
for(size_t j = 0; j<DIA; ++j){
|
||||
if(i+j == DIA -1){
|
||||
arr[i] [j] = rand() % ((MAX-MIN)+1) +MIN;
|
||||
}
|
||||
else
|
||||
arr[i] [j] = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void stampa(int arr[DIA] [DIA]){
|
||||
|
||||
for(size_t i = 0; i<DIA; ++i){
|
||||
for(size_t j = 0; j<DIA; ++j){
|
||||
|
||||
cout << setw(3) << arr[i] [j] << " ";
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
int main(void){
|
||||
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
int arr[DIA] [DIA];
|
||||
|
||||
antiDia(arr);
|
||||
stampa(arr);
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
// Cristian Ronzoni 3Ain
|
||||
// Genera una matrice diagonale con valori casuali
|
||||
// Righe nel ciclo esterno, colonne nel ciclo interno (righe i, colonne j)
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#define LATO 5
|
||||
#define MIN 20
|
||||
#define MAX 60
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
void riempiMatrice(int arr[LATO][LATO]) {
|
||||
for (size_t i = 0; i < LATO; ++i) {
|
||||
for (size_t j = 0; j < LATO; ++j) {
|
||||
if (i == j)
|
||||
arr[i][j] = rand() % (MAX - MIN + 1) + MIN;
|
||||
else
|
||||
arr[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void stampaMatrice(int arr[LATO][LATO]) {
|
||||
for (size_t i = 0; i < LATO; ++i) {
|
||||
for (size_t j = 0; j < LATO; ++j) {
|
||||
cout << setw(3)<< arr[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
|
||||
|
||||
|
||||
int array[LATO][LATO];
|
||||
|
||||
|
||||
riempiMatrice(array);
|
||||
cout << "Matrice diagonale generata:\n";
|
||||
stampaMatrice(array);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Antidiagonale
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
if (i + j == nrow - 1) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Antisimmetrica
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
int value = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
matrix[i][j] = value;
|
||||
matrix[j][i] = -value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Diagonale
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
if (i == j) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Identità
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
if (i == j) {
|
||||
matrix[i][j] = 1;
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Nulla
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Simmetrica
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
int value = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
matrix[i][j] = value;
|
||||
matrix[j][i] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 13/12/2024
|
||||
|
||||
Genera una Matrice e poi ne stampa la Trasposta.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define RIGHE 3
|
||||
#define COLONNE 3
|
||||
#define MINIMO -9
|
||||
#define MASSIMO 9
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void matriceTrasposta(int matrice[RIGHE][COLONNE], int temp[COLONNE][RIGHE], int righe, int colonne);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int temp[COLONNE][RIGHE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
|
||||
cout << "Matrice: " << endl,
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
matriceTrasposta(matrice, temp, righe, colonne);
|
||||
|
||||
cout << "Matrice trasposta: " << endl;
|
||||
stampaMatrice(temp, righe, colonne);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
srand(time(NULL));
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
matrice[r][c] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << setw(5) << matrice[r][c];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void matriceTrasposta(int matrice[RIGHE][COLONNE], int temp[COLONNE][RIGHE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
temp[c][r] = matrice[r][c];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Triangolare Inferiore
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
if (i >= j) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Matrice Triangolare Superiore
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 4
|
||||
#define NCOL 4
|
||||
#define VMIN -50
|
||||
#define VMAX 50
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
if (i <= j) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
} else {
|
||||
matrix[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
//Cristian Ronzoni 3AIN
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define MAX 60
|
||||
#define MIN 20
|
||||
|
||||
using namespace std;
|
||||
|
||||
int riempiArr(int arr[], size_t size){
|
||||
|
||||
|
||||
|
||||
for(size_t i = 0; i<size; i++){
|
||||
|
||||
arr[i] = rand() % ((MAX - MIN)+1) +MIN;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//righe nel ciclo esterno, colonne nel ciclo interno (righe i, colonne j)
|
||||
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
int array[3] [2]{
|
||||
{3,5},
|
||||
{2,7},
|
||||
{4,9},
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
/*Fare un algoritmo che calcoli la media
|
||||
e rilevi i numeri maggiori e minori alla media stessa*/
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#define NUM 7
|
||||
using namespace std;
|
||||
void riempiMatrice(int arr[NUM][NUM]);
|
||||
void stampaMatrice(int arr[NUM][NUM]);
|
||||
void maxMatrice(int arr[NUM][NUM]);
|
||||
void minMatrice(int arr[NUM][NUM]);
|
||||
|
||||
|
||||
void calcolaMediaMatrice(int arr[NUM][NUM]){
|
||||
float media = 0;
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
for(size_t j = 0; j<NUM; ++j){
|
||||
media += arr[i][j];
|
||||
}
|
||||
}
|
||||
media = media / (NUM*NUM);
|
||||
cout << "La media della matrice e':"<< media;
|
||||
}
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
int bobby[NUM][NUM];
|
||||
riempiMatrice(bobby);
|
||||
stampaMatrice(bobby);
|
||||
calcolaMediaMatrice(bobby);
|
||||
maxMatrice(bobby);
|
||||
minMatrice(bobby);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void riempiMatrice(int arr[NUM][NUM]){
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
for(size_t j = 0; j<NUM; ++j){
|
||||
arr[i][j] = rand() % 40;
|
||||
}
|
||||
}
|
||||
}
|
||||
void stampaMatrice(int arr[NUM][NUM]){
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
for(size_t j = 0; j<NUM; ++j){
|
||||
cout << setw(3) << arr[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void maxMatrice(int arr[NUM][NUM]){
|
||||
int max = arr[0][0];
|
||||
for(size_t i = 0; i<NUM ; ++i){
|
||||
for(size_t j = 0; j<NUM; ++j){
|
||||
if(arr[i][j] > max)
|
||||
max = arr[i][j];
|
||||
}
|
||||
}
|
||||
cout << endl << "Il numero più grande della matrice e':"<<max;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void minMatrice(int arr[NUM][NUM]){
|
||||
int min = arr[0][0];
|
||||
for(size_t i = 0; i<NUM; ++i){
|
||||
for(size_t j = 0; j<NUM; ++j){
|
||||
if(arr[i][j] < min){
|
||||
min = arr[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << endl << "Il numero più piccolo della matrice e':" << min;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#define MAX 5
|
||||
#define MIN 1
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
|
||||
for(size_t i = 0; i < RIGHE; i++)
|
||||
{
|
||||
for(size_t j = 0; j < RIGHE; j++)
|
||||
{
|
||||
matrice[i][j]=rand()%(MAX-MIN+1)+MIN;
|
||||
cout << setw(6) << matrice[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
int moltiplicaMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE, size_t valore){
|
||||
int prodotto = 1;
|
||||
for(size_t k = 0; k < RIGHE; k++)
|
||||
{
|
||||
for(size_t l = 0; l < COLONNE; l++)
|
||||
{
|
||||
prodotto *= matrice[k][l];
|
||||
}
|
||||
}
|
||||
return prodotto*valore;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
int matrice[3][3];
|
||||
const int RIGHE = 3;
|
||||
const int COLONNE = 3;
|
||||
int valore;
|
||||
|
||||
riempiMatrice(matrice, RIGHE, COLONNE);
|
||||
|
||||
cout << endl << endl;
|
||||
cout << "inserisci il numero che moltiplica gli elementi della matrice" << endl << "-> ";
|
||||
cin >> valore;
|
||||
|
||||
cout << moltiplicaMatrice(matrice, RIGHE, COLONNE, valore);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#define MAX 100
|
||||
#define MIN 1
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
|
||||
for(size_t i = 0; i < RIGHE; i++)
|
||||
{
|
||||
for(size_t j = 0; j < RIGHE; j++)
|
||||
{
|
||||
matrice[i][j]=rand()%(MAX-MIN+1)+MIN;
|
||||
cout << setw(6) << matrice[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
int numeroMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
|
||||
int conteggio = 0;
|
||||
for(size_t k = 0; k < RIGHE; k++)
|
||||
{
|
||||
for(size_t l = 0; l < COLONNE; l++)
|
||||
{
|
||||
if(matrice[k][l] == 13)
|
||||
conteggio++;
|
||||
}
|
||||
}
|
||||
return conteggio;
|
||||
}
|
||||
|
||||
int multipliMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
|
||||
int conteggio = 0;
|
||||
for(size_t k = 0; k < RIGHE; k++)
|
||||
{
|
||||
for(size_t l = 0; l < COLONNE; l++)
|
||||
{
|
||||
if(matrice[k][l]%17 == 0)
|
||||
conteggio++;
|
||||
}
|
||||
}
|
||||
return conteggio;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
int matrice[3][3];
|
||||
const int RIGHE = 3;
|
||||
const int COLONNE = 3;
|
||||
int valore;
|
||||
|
||||
riempiMatrice(matrice, RIGHE, COLONNE);
|
||||
|
||||
cout << endl << endl;
|
||||
|
||||
cout << numeroMatrice(matrice, RIGHE, COLONNE) << endl;
|
||||
cout << multipliMatrice(matrice, RIGHE, COLONNE) << endl;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
#define MAX 10
|
||||
#define MIN 0
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[][3], size_t RIGHE, size_t COLONNE){
|
||||
for(size_t i = 0; i < RIGHE; i++)
|
||||
{
|
||||
for(size_t j = 0; j < COLONNE; j++){
|
||||
matrice[i][j]=rand()%(MAX-MIN+1)+MIN;
|
||||
cout << setw(6) << matrice[i][j];
|
||||
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void contaOccorrenze(int matrice[][3], size_t RIGHE, size_t COLONNE, size_t valore){
|
||||
int conteggio = 0;
|
||||
for(size_t k = 0; k < RIGHE; k++)
|
||||
{
|
||||
for(size_t l = 0; l < COLONNE; l++)
|
||||
{
|
||||
if(matrice[k][l] == valore)
|
||||
conteggio++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cout << "il numero e' presente: " << conteggio;
|
||||
}
|
||||
|
||||
|
||||
int main(void){
|
||||
srand(time(NULL));
|
||||
int const RIGHE = 3;
|
||||
int const COLONNE = 3;
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int valore;
|
||||
|
||||
riempiMatrice(matrice, RIGHE, COLONNE);
|
||||
|
||||
cout << endl << endl;
|
||||
cout << "inserisci il valore di cui contare le occorrenze nella matrice" << endl << "-> ";
|
||||
cin >> valore;
|
||||
|
||||
contaOccorrenze(matrice, RIGHE, COLONNE, valore);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Moltiplicazione tra due matrici
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 2
|
||||
#define NCOL 2
|
||||
#define VMIN -5
|
||||
#define VMAX 5
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void powerMatrix(int matrix[][NCOL], int result[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix[NROW][NCOL];
|
||||
int result[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix, NROW, NCOL);
|
||||
|
||||
cout << "Matrix: " << endl;
|
||||
printMatrix(matrix, NROW, NCOL);
|
||||
|
||||
powerMatrix(matrix, result, NROW, NCOL);
|
||||
|
||||
cout << "Power of the Matrix: " << endl;
|
||||
printMatrix(result, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void powerMatrix(int matrix[][NCOL], int result[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
result[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
for (int k = 0; k < ncol; k++) {
|
||||
result[i][j] = result[i][j] + matrix[i][k] * matrix[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Moltiplicazione tra due matrici
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 2
|
||||
#define NCOL 2
|
||||
#define VMIN -5
|
||||
#define VMAX 5
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void multiplyMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix1[NROW][NCOL];
|
||||
int matrix2[NROW][NCOL];
|
||||
int result[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix1, NROW, NCOL);
|
||||
fillMatrix(matrix2, NROW, NCOL);
|
||||
|
||||
cout << "First Matrix: " << endl;
|
||||
printMatrix(matrix1, NROW, NCOL);
|
||||
|
||||
cout << "Second Matrix: " << endl;
|
||||
printMatrix(matrix2, NROW, NCOL);
|
||||
|
||||
multiplyMatrices(matrix1, matrix2, result, NROW, NCOL);
|
||||
|
||||
cout << "Product of the Matrices: " << endl;
|
||||
printMatrix(result, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void multiplyMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
result[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
for (int k = 0; k < ncol; k++) {
|
||||
result[i][j] = result[i][j] + matrix1[i][k] * matrix2[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
void quickSortArray(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int pivot = arr[high];
|
||||
int i = (low - 1);
|
||||
|
||||
for (int j = low; j < high; j++) {
|
||||
if (arr[j] <= pivot) {
|
||||
i++;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int temp = arr[i + 1];
|
||||
arr[i + 1] = arr[high];
|
||||
arr[high] = temp;
|
||||
|
||||
int pi = i + 1;
|
||||
|
||||
quickSortArray(arr, low, pi - 1);
|
||||
quickSortArray(arr, pi + 1, high);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
void quickSortMatrice(int matrice[][6], int righe, int colonne, int low, int high) {
|
||||
if (low < high) {
|
||||
int pivot = matrice[high][colonne];
|
||||
int i = (low - 1);
|
||||
|
||||
for (int j = low; j < high; j++) {
|
||||
if (matrice[j][colonne] <= pivot) {
|
||||
i++;
|
||||
int temp = matrice[i][colonne];
|
||||
matrice[i][colonne] = matrice[j][colonne];
|
||||
matrice[j][colonne] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int temp = matrice[i + 1][colonne];
|
||||
matrice[i + 1][colonne] = matrice[high][colonne];
|
||||
matrice[high][colonne] = temp;
|
||||
|
||||
int pi = i + 1;
|
||||
|
||||
quickSortMatrice(matrice, righe, colonne, low, pi - 1);
|
||||
quickSortMatrice(matrice, righe, colonne, pi + 1, high);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
// Programma che verifica se n è presente all’interno della matrice
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#define LATO 3
|
||||
using namespace std;
|
||||
void riempi(int arr[LATO] [LATO]);
|
||||
void stampa(int arr[LATO] [LATO]);
|
||||
void check(int arr[LATO][LATO],int n);
|
||||
|
||||
int main (void){
|
||||
srand(time(NULL));
|
||||
|
||||
int num;
|
||||
int bobby[LATO][LATO];
|
||||
|
||||
cout << "Inserisci un numero";
|
||||
cin >> num;
|
||||
riempi(bobby);
|
||||
stampa(bobby);
|
||||
check(bobby, num);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void stampa(int arr[LATO] [LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
cout << setw(3) << arr[i] [j] ;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void riempi(int arr[LATO] [LATO]){
|
||||
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
arr[i][j] = rand() % 10;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void check(int arr[LATO][LATO], int n){
|
||||
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
if(arr[i][j] == n || arr[i++][j] == n || arr[i][j++] ==n){
|
||||
cout << "Il numero e' presente nella matrice";
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define N 3 // Dimensione della matrice (N x N)
|
||||
|
||||
// Funzione per stampare la matrice
|
||||
void stampaMatrice(int matrice[N][N]) {
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
printf("%d ", matrice[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Inversione degli elementi della matrice
|
||||
void inverteMatrice(int matrice[N][N]) {
|
||||
int totaleElementi = N * N;
|
||||
for (int i = 0; i < totaleElementi / 2; i++) {
|
||||
int riga1 = i / N;
|
||||
int col1 = i % N;
|
||||
int riga2 = (totaleElementi - 1 - i) / N;
|
||||
int col2 = (totaleElementi - 1 - i) % N;
|
||||
|
||||
// Scambio degli elementi
|
||||
int temp = matrice[riga1][col1];
|
||||
matrice[riga1][col1] = matrice[riga2][col2];
|
||||
matrice[riga2][col2] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Rotazione della matrice di 90 gradi in senso orario
|
||||
void ruota90Gradi(int matrice[N][N]) {
|
||||
int temp[N][N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
temp[j][N - 1 - i] = matrice[i][j];
|
||||
}
|
||||
}
|
||||
// Copia la matrice ruotata nella matrice originale
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
matrice[i][j] = temp[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Rotazione della matrice di 180 gradi
|
||||
void ruota180Gradi(int matrice[N][N]) {
|
||||
for (int i = 0; i < N * N / 2; i++) {
|
||||
int riga1 = i / N;
|
||||
int col1 = i % N;
|
||||
int riga2 = (N * N - 1 - i) / N;
|
||||
int col2 = (N * N - 1 - i) % N;
|
||||
|
||||
// Scambio degli elementi
|
||||
int temp = matrice[riga1][col1];
|
||||
matrice[riga1][col1] = matrice[riga2][col2];
|
||||
matrice[riga2][col2] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione principale
|
||||
int main() {
|
||||
int matrice[N][N] = {
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
|
||||
printf("Matrice originale:\n");
|
||||
stampaMatrice(matrice);
|
||||
|
||||
// Inversione degli elementi
|
||||
inverteMatrice(matrice);
|
||||
printf("Matrice con inversione degli elementi:\n");
|
||||
stampaMatrice(matrice);
|
||||
|
||||
// Ripristina la matrice originale per le rotazioni
|
||||
int matriceOriginale[N][N] = {
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
|
||||
// Rotazione di 90 gradi
|
||||
ruota90Gradi(matriceOriginale);
|
||||
printf("Matrice ruotata di 90 gradi:\n");
|
||||
stampaMatrice(matriceOriginale);
|
||||
|
||||
// Rotazione di 180 gradi
|
||||
ruota180Gradi(matrice);
|
||||
printf("Matrice ruotata di 180 gradi:\n");
|
||||
stampaMatrice(matrice);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
/*Programma che stampa una scacchiera di bool
|
||||
tramite una matrice
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#define LATO 7
|
||||
using namespace std;
|
||||
|
||||
void stampaMatrice(bool arr[LATO][LATO]);
|
||||
void scacchiera(bool arr[LATO][LATO]);
|
||||
|
||||
|
||||
|
||||
|
||||
int main(void){
|
||||
|
||||
bool bobby[LATO][LATO];
|
||||
scacchiera(bobby);
|
||||
stampaMatrice(bobby);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void stampaMatrice(bool arr[LATO][LATO]){
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
cout << setw(3)<< arr[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void scacchiera(bool arr[LATO][LATO]){
|
||||
|
||||
for(size_t i = 0; i<LATO; ++i){
|
||||
for(size_t j = 0; j<LATO; ++j){
|
||||
arr[i][j] = (i + j) % 2 == 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 13/12/2024
|
||||
|
||||
Genera una Matrice e poi scamba la prima colonna della Matrice con l'ultima.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define RIGHE 4
|
||||
#define COLONNE 4
|
||||
#define MINIMO -9
|
||||
#define MASSIMO 9
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
int temp;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
|
||||
cout << "Matrice: " << endl,
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
scambiaRiga(matrice, temp, righe, colonne);
|
||||
|
||||
cout << "Matrice trasposta: " << endl;
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
srand(time(NULL));
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
matrice[r][c] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << setw(5) << matrice[r][c];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
temp = matrice[r][0];
|
||||
matrice[r][0] = matrice[r][3];
|
||||
matrice[r][3] = temp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
Classe: 3AIN
|
||||
Data: 13/12/2024
|
||||
|
||||
Genera una Matrice e poi scamba la prima riga della Matrice con l'ultima.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define RIGHE 4
|
||||
#define COLONNE 4
|
||||
#define MINIMO -9
|
||||
#define MASSIMO 9
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
int temp;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
|
||||
cout << "Matrice originale: " << endl,
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
scambiaRiga(matrice, temp, righe, colonne);
|
||||
|
||||
cout << "Matrice scambiata: " << endl;
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
srand(time(NULL));
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
matrice[r][c] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << setw(5) << matrice[r][c];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
temp = matrice[0][c];
|
||||
matrice[0][c] = matrice[3][c];
|
||||
matrice[3][c] = temp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
void selectionSortArray(int arr[], int n) {
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
int minIdx = i;
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (arr[j] < arr[minIdx]) {
|
||||
minIdx = j;
|
||||
}
|
||||
}
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIdx];
|
||||
arr[minIdx] = temp;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
void selectionSortMatrice(int matrice[][6], int righe, int colonne) {
|
||||
for (int j = 0; j < colonne; j++) {
|
||||
for (int i = 0; i < righe - 1; i++) {
|
||||
int minIdx = i;
|
||||
for (int k = i + 1; k < righe; k++) {
|
||||
if (matrice[k][j] < matrice[minIdx][j]) {
|
||||
minIdx = k;
|
||||
}
|
||||
}
|
||||
int temp = matrice[i][j];
|
||||
matrice[i][j] = matrice[minIdx][j];
|
||||
matrice[minIdx][j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
//Cristian Ronzoni 3Ain
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
#define DIM 5 // Dimensione della matrice
|
||||
|
||||
// Funzione per generare una matrice casuale
|
||||
void riempiMatrice(int matrice[DIM][DIM]) {
|
||||
srand(time(0)); // Inizializza il generatore di numeri casuali
|
||||
for (int i = 0; i < DIM; ++i) {
|
||||
for (int j = 0; j < DIM; ++j) {
|
||||
matrice[i][j] = rand() % 10 + 1; // Numeri casuali tra 1 e 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per stampare la matrice
|
||||
void stampaMatrice(int matrice[DIM][DIM]) {
|
||||
for (int i = 0; i < DIM; ++i) {
|
||||
for (int j = 0; j < DIM; ++j) {
|
||||
cout << matrice[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Funzione per calcolare la somma di una riga
|
||||
int sommaRiga(int matrice[DIM][DIM], int riga) {
|
||||
int somma = 0;
|
||||
for (int j = 0; j < DIM; ++j) {
|
||||
somma += matrice[riga][j];
|
||||
}
|
||||
return somma;
|
||||
}
|
||||
|
||||
// Funzione per calcolare la somma di una colonna
|
||||
int sommaColonna(int matrice[DIM][DIM], int colonna) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < DIM; ++i) {
|
||||
somma += matrice[i][colonna];
|
||||
}
|
||||
return somma;
|
||||
}
|
||||
|
||||
// Funzione per calcolare la somma della diagonale principale
|
||||
int sommaDiagonalePrincipale(int matrice[DIM][DIM]) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < DIM; ++i) {
|
||||
somma += matrice[i][i];
|
||||
}
|
||||
return somma;
|
||||
}
|
||||
|
||||
// Funzione per calcolare la somma della diagonale secondaria
|
||||
int sommaDiagonaleSecondaria(int matrice[DIM][DIM]) {
|
||||
int somma = 0;
|
||||
for (int i = 0; i < DIM; ++i) {
|
||||
somma += matrice[i][DIM - 1 - i];
|
||||
}
|
||||
return somma;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int matrice[DIM][DIM];
|
||||
|
||||
// Genera la matrice casuale
|
||||
riempiMatrice(matrice);
|
||||
|
||||
// Stampa la matrice
|
||||
cout << "Matrice generata:" << endl;
|
||||
stampaMatrice(matrice);
|
||||
|
||||
// Calcola e stampa le somme
|
||||
int riga, colonna;
|
||||
cout << "Inserisci il numero della riga (0 a " << DIM-1 << "): ";
|
||||
cin >> riga;
|
||||
cout << "Somma della riga " << riga << ": " << sommaRiga(matrice, riga) << endl;
|
||||
|
||||
cout << "Inserisci il numero della colonna (0 a " << DIM-1 << "): ";
|
||||
cin >> colonna;
|
||||
cout << "Somma della colonna " << colonna << ": " << sommaColonna(matrice, colonna) << endl;
|
||||
|
||||
cout << "Somma della diagonale principale: " << sommaDiagonalePrincipale(matrice) << endl;
|
||||
cout << "Somma della diagonale secondaria: " << sommaDiagonaleSecondaria(matrice) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
Nome: Mario
|
||||
Cognome: Montanari
|
||||
|
||||
Somma tra due matrici
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <ctime>
|
||||
#include <iomanip>
|
||||
|
||||
#define NROW 2
|
||||
#define NCOL 2
|
||||
#define VMIN -5
|
||||
#define VMAX 5
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
||||
void sumMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol);
|
||||
|
||||
int main(void) {
|
||||
int matrix1[NROW][NCOL];
|
||||
int matrix2[NROW][NCOL];
|
||||
int result[NROW][NCOL];
|
||||
|
||||
fillMatrix(matrix1, NROW, NCOL);
|
||||
fillMatrix(matrix2, NROW, NCOL);
|
||||
|
||||
cout << "First Matrix: " << endl;
|
||||
printMatrix(matrix1, NROW, NCOL);
|
||||
|
||||
cout << "Second Matrix: " << endl;
|
||||
printMatrix(matrix2, NROW, NCOL);
|
||||
|
||||
sumMatrices(matrix1, matrix2, result, NROW, NCOL);
|
||||
|
||||
cout << "Sum of the Matrices" << endl;
|
||||
printMatrix(result, NROW, NCOL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void fillMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
srand(time(NULL));
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printMatrix(int matrix[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
cout << endl;
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
cout << setw(5) << matrix[i][j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl << endl << endl;
|
||||
}
|
||||
|
||||
void sumMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol) {
|
||||
for (int i = 0; i < nrow; i++) {
|
||||
for (int j = 0; j < ncol; j++) {
|
||||
result[i][j] = matrix1[i][j] + matrix2[i][j];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#include <iostream>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
|
||||
#define RIGHE 10
|
||||
#define COLONNE 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
|
||||
|
||||
int main(void) {
|
||||
int matrice[RIGHE][COLONNE];
|
||||
int righe = RIGHE;
|
||||
int colonne = COLONNE;
|
||||
|
||||
riempiMatrice(matrice, righe, colonne);
|
||||
stampaMatrice(matrice, righe, colonne);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
matrice[r][c] = (r + 1) * (c + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
|
||||
for (int r = 0; r < righe; r++) {
|
||||
cout << endl;
|
||||
for (int c = 0; c < colonne; c++) {
|
||||
cout << setw(5) << matrice[r][c];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
- Matrice Nulla
|
||||
- Matrice Identità
|
||||
- Matrice Diagonale
|
||||
- Matrice Antidiagonale
|
||||
- Matrice Triangolare Superiore
|
||||
- Matrice Triangolare Inferiore
|
||||
- Matrice Simmetrica
|
||||
- Matrice Antisimmetrica
|
||||
Matrice Trasposta
|
||||
Somma tra due Matrici
|
||||
Moltiplicazione tra due Matrici
|
||||
Potenza di una Matrice
|
||||
Potenza Nilpotente di una Matrice
|
||||
Potenza Idempotente di una Matrice
|
||||
Scambio tra due righe di una Matrice
|
||||
Scambio tra due colonne di una Matrice
|
||||
Gioco del Tris
|
||||
Quadrato Magico
|
|
@ -0,0 +1,36 @@
|
|||
//Patriche Robert Cosmin
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
using namespace std;
|
||||
|
||||
int trovaPosizioneMassimo(int arr[], int size);
|
||||
|
||||
int main() {
|
||||
const int DIM = 15;
|
||||
int numeri[DIM];
|
||||
srand(time(0));
|
||||
|
||||
cout << "Array: ";
|
||||
for (int i = 0; i < DIM; i++) {
|
||||
numeri[i] = rand() % 100 + 1;
|
||||
cout << numeri[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
int posizione = trovaPosizioneMassimo(numeri, DIM);
|
||||
cout << "La posizione del valore massimo è: " << posizione << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int trovaPosizioneMassimo(int arr[], int size) {
|
||||
int massimo = arr[0], posizione = 0;
|
||||
for (int i = 1; i < size; i++) {
|
||||
if (arr[i] > massimo) {
|
||||
massimo = arr[i];
|
||||
posizione = i;
|
||||
}
|
||||
}
|
||||
return posizione;
|
||||
}
|
Loading…
Reference in New Issue