Răspuns :
#include <iostream>
#include <fstream>
using namespace std;
ofstream fout("tema5.out");
int S, n;
struct Produs{
int pret, cantitate;
}x[8];
int main()
{
cout << "Suma = "; cin >> S;
cout << "Nr. de produse: "; cin >> n;
for(int i=0; i<n; ++i){
cout << endl << "Produsul " << i+1 << endl;
cout << "Pret: "; cin >> x[i].pret;
cout << "Cantitate: "; cin >> x[i].cantitate;
}
cout << endl;
for(int i=0; i<n; ++i)
cout << x[i].pret << ' ' << x[i].cantitate << endl;
for(int i=0; i<n-1; ++i)
for(int j=i+1; j<n; ++j)
if(x[i].pret > x[j].pret){ //nu ai scris daca crescator sau descrescator, modifici tu dupa cerinta
Produs aux = x[i];
x[i] = x[j];
x[j] = aux;
}
cout << endl << "Produsele ordonate crescator dupa pret sunt: " << endl;
for(int i=0; i<n; ++i)
cout << x[i].pret << ' ' << x[i].cantitate << endl;
int sum = 0;
for(int i=0; i<4; ++i)
sum += x[i].pret;
if(sum <= S)
cout << endl << "Produsele pot fi cumparate" << endl;
else
cout << endl << "Produsele nu pot fi cumparate" << endl;
for(int i=0; i<n-1; ++i)
for(int j=i+1; j<n; ++j)
if(x[i].cantitate < x[j].cantitate){
Produs aux = x[i];
x[i] = x[j];
x[j] = aux;
}
for(int i=0; i<n; ++i)
fout << x[i].pret << ' ' << x[i].cantitate << endl;
return 0;
}