0% found this document useful (0 votes)
47 views6 pages

Program P78

The document contains several C++ programs that demonstrate working with arrays, strings, matrices and functions for string manipulation. Program P83_b shows how to count the number of spaces in a string by getting user input, finding the string length, initializing a counter, and iterating through the string to increment the counter when a space is encountered.

Uploaded by

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

Program P78

The document contains several C++ programs that demonstrate working with arrays, strings, matrices and functions for string manipulation. Program P83_b shows how to count the number of spaces in a string by getting user input, finding the string length, initializing a counter, and iterating through the string to increment the counter when a space is encountered.

Uploaded by

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

// Program P78

// Suma componentelor variabilei V de tip Vector


// Numar fix de componente
#include <iostream>
using namespace std;
int main()
{
typedef int Vector[5];
Vector V;
int i, S;
cout <<"Dati 5 numere intregi:" << endl;
for (i=0; i<5; i++) cin>>V[i];
cout << "Ati introdus:" << endl;
for (i=0; i<5; i++) cout << V[i] << ' ';
cout << endl;
S=0;
for (i=0; i<5; i++) S=S+V[i];
cout << "Suma= " << S;
return 0;
}

//Program P79
// Suma componentelor variabilei V de tip Vector
// Numar flotant de componente
#include <iostream>
using namespace std;
int main()
{
const int nmax=100;
typedef int Vector[nmax];
Vector V;
int n, i, S;
cout << "Dati n= "; cin >> n;
cout << "Dati "<<n<<" numere intregi:" << endl;
for (i=0; i<n; i++) cin >> V[i];
cout << "Ati introdus:" << endl;
for (i=0; i<n; i++) cout << V[i] << ' ';
cout << endl;
S=0;
for (i=0; i<n; i++) S=S+V[i];
cout << "Suma= " << S;
return 0;
}

// Program P80
// Sortarea prin metoda bulelor
#include <iostream>
using namespace std;
int main()
{
const int nmax= 100;
typedef int Tablou[nmax];
Tablou A, B;
int n, i, j;
int x;
cout<<"Dati numarul de componente n= ";
cin>>n;
cout<<"Dati componentele tabloului A: \n";
for (i=0; i<n; i++) cin>>A[i];
for (i=0; i<n; i++) B[i]=A[i];
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)
if (B[i]>B[j])
{ x=B[i]; B[i]=B[j]; B[j]=x;}
cout<<"Tabloul initial: \n";
for (i=0; i<n; i++) cout<<A[i]<<" ";
cout << endl;
cout<<"Tabloul sortat: \n";
for (i=0; i<n; i++) cout<<B[i]<<" ";
cout << endl;
return 0;
}
// Program P81
// Suma componentelor variabilei M de tip Matrice
#include<iostream>
using namespace std;
int main()
{
typedef double Matrice[3][4];
Matrice M;
int i, j;
double S;
cout << "Dati componentele M[i,j]: " << endl;
for (i=0; i<3; i++)
for (j=0; j<4; j++)
{
cout << "M[" <<i<< ',' << j << "]= ";
cin >> M[i][j];
}
cout << "Ati introdus:" << endl;
for (i=0; i<3; i++)
{
for (j=0; j<4; j++) cout << M[i][j] << ' ';
cout << endl;
}
S=0;
for (i=0; i<3; i++)
for (j=0; j<4; j++)
S=S+M[i][j];
cout << "Suma= " << S << endl;
return 0;
}

// Program P82_d
/* Functii predefinite pentru prelucrarea */
/* sirurilor de tip tablou unidimensional */
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
typedef char Nume[20];
Nume N;
typedef char Prenume[10];
Prenume P;
strcpy(N, "Munteanu");
cout << N << " " << strlen(N) << endl;
strcpy(P, "Mihai");
cout << P << " " << strlen(P) << endl;
strcat(N, " ");
cout << strcat(N, P) << " " << strlen(N) << endl;
return 0;
}

// Program P83_a
// Siruri de caractere de tip string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string N; // Nume
string P; // Prenume
string NP; // Nume si Prenume
N = "Olaru"; // atribuire
P = "Andrei"; // atribuire
NP = N + " " + P; // concatenare si atribuire
cout << N << endl;
cout << P << endl;
cout << NP << endl;
return 0;
}
// Program P83_b
// Numarul de spatii intr-un sir
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Sir; // Sirul citit de la tastatura
int L; // Lungimea sirului citit
int n; // Numarul de spatii in sirul citit
int i;
Repeta:
cout << "Dati sirul de caractere:" << endl;
getline(cin, Sir); // Citirea sirului
L = Sir.length(); // Lungimea sirului
cout << "Lungimea sirului = " << L << endl;
n = 0;
for (i=0; i < L; i++)
if (Sir[i] == ' ') n++;
cout << "Numarul de spatii in sir = " << n << endl;
cout << endl;
if (Sir != "Sfarsit") goto Repeta;
return 0;
}

#include <iostream>
using namespace std;
int main()
{
//Declarăm și citim cele două numere
int a, b;
cin >> a >> b;

//Interschimbăm valorile celor două variabile


int aux = a;
a = b;
b = aux; //Dacă am fi scris b = a, b ar fi luat noua valoare a lui a (nu cea inițială), care este însuși b.
//Afișăm variabilele după interschimbare
cout << a << " " << b;
return 0;
}

You might also like