Jump to content
JohnyCNAM

Urgent ajutor...c++

Recommended Posts

Posted (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 by nedo
Posted

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;

Posted

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;
}


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...