Concetti Chiave
- Il programma C++ utilizza array e include funzioni per sostituire, sommare e scambiare elementi.
- La funzione `Dimensionamento` determina la dimensione dell'array, assicurandosi che non superi il massimo consentito.
- `CaricaVettore` permette di inserire manualmente i valori nell'array fino alla sua dimensione specificata.
- La funzione `Sostituzione` consente di cambiare il valore di un elemento dell'array in una posizione specifica.
- `Scambio` inverte il primo elemento dell'array con un altro elemento in una posizione data, modificando l'ordine degli elementi.
//Programma C++
//Array
//Funzioni: Sostituzione, Somma, Scambio
#include iostream>
using namespace std;
const int MAX=100;
int Dimensionamento();
int CaricaVettore(int v[MAX], int d);
Substitution Function
int Sostituzione(int A1[], int p1, int v1, int d1);
void Somma(int A1[], int p1, int d1);
int Scambio(int A1[], int d1, int p1);
int Dimensionamento()
{
int d;
do{
cout
cin>>d;
} while (dMAX);
return d;
}
int CaricaVettore(int v[MAX], int d)
{
for (int i=0; i
cout
cin>>v;
}
}
int Sostituzione(int A1[], int p1, int v1, int d1) {
for (int c=0;c
for (int y=0; y
if (y==p1) {
A1[y]=v1;
}
}
return A1[c];
}
}
void Somma(int A1[], int p1, int d1) {
cout
int s=0;
for (int b=0;b
cout
}
cout
for (int r=0;r
s=s+A1[r];
}
cout
}
int Scambio(int A1[], int d1, int p1) {
int sp1, spp;
sp1=A1[1];
spp=A1[p1];
A1[1]=spp;
A1[p1]=sp1;
cout
for ( int y=0;y
cout
}
}
int main()
{
int p, v1, vettore[100], dimensione;
dimensione=Dimensionamento();
cout
CaricaVettore(vettore,dimensione);
do{
cout
cin>>p;
} while (p>dimensione);
cout
cin>>v1;
Sostituzione(vettore,p,v1,dimensione);
Somma(vettore,p,dimensione);
Scambio(vettore,dimensione,p);
system("pause");
return 0;
}
Domande da interrogazione
- What is the purpose of the `Sostituzione` function in the C++ program?
- How does the `Somma` function operate within the program?
- What is the role of the `Scambio` function in the array manipulation?
The `Sostituzione` function is designed to replace the value at a specified position in an array with a new value. It iterates through the array and, when it reaches the specified position, it assigns the new value to that position.
The `Somma` function calculates the sum of all elements in the array. It iterates through the array, adding each element to a cumulative sum, which is then printed to the console.
The `Scambio` function swaps the first element of the array with the element at a specified position. It temporarily stores the values, performs the swap, and then outputs the modified array.