JohnyCNAM Posted March 26, 2012 Report Posted March 26, 2012 (edited) sa se citeasca n pozitii din fisierul data.in sa se calculeze media nr pare si sa se afiseze in data.out Edited March 26, 2012 by ionutcristea Quote
nedo Posted March 26, 2012 Report Posted March 26, 2012 (edited) Verifici si tu eventualele erori de tastare pentru ca il scriu direct aici.#include <iostream>#include <vector>#include <fstream>#include <string>using std:ifstream;using std::ofstream;using std::endl;using std::cin;using std::cout;using std::getline;using std::string;int main(int argv, char* argv[]){ ifstream fIn; ofstream fOut; int n; fIn.open(data.in); fOut.open(data.out); cout << "Cate numere doriti sa cititi ?\n"; cin >> n; int counter = 0; // va tine numarul de numere pare; int suma = 0; //va tine suma numerelor pare if(fIn.is_open() && fOut.is_open()) { for(unsigned int i = 0; i <= n;i++) { if(fIn.good()) { string linie; int temp = 0; getline(fIn, linie); temp = atoi(linie.c_str()); if((temp % 2) == 0) { counter++; suma += temp; } } } int medie = suma / counter; fout << medie; fIn.close(); fOut.close(); return 0;} Edited March 26, 2012 by nedo Quote
pr00f Posted March 26, 2012 Report Posted March 26, 2012 N pozi?ii înseamn? media aritmetic? a m numere pare. Tot ce trebuie s? facem e s? citim n numere ?i s? le introducem într-o sum? dac? sunt pare.contor=0;suma=0;for (i=1; i<=n; i++) { f >> n; if (n%2==0) { suma += n; contor++; }media = suma / contor; Quote
nedo Posted March 26, 2012 Report Posted March 26, 2012 aww.. da corect, deci nu mai e necesar counter-ul. Asta se intampla cand scrii cod intre 2 telefoane Quote
Ellimist Posted March 26, 2012 Report Posted March 26, 2012 Aproximativ asa cred ca este.#include <iostream.h>#include <fstream.h>int main(){int nr=0,s=0,n,m,i;cin>>n;fstream f("data.in");ofstream g("data.out");for (i=0;i<n;i++) { f>>m; if (m%2==0) s+=m; nr++; i++; }int med=s/nr;g<<med;f.close();g.close();return 0;} Quote