I'm having a problem with my program. This program will display the numbers and get the average of them, average of positive and negative numbers, and also display the largest element.

I have 3 files. For header, other functions and main function.

Prog.h

#ifndef Prog
#define Prog

class Prog
{
public:
	void ReadList(int Array[], int N);
	void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN);
	int Large (int Array[], int N);
	void Display(int Array[], int N, int Ave, int AveP, int AveN, int Max);

private:
	int N;
	int Array[10];

}

#endif

Prog.cpp

#include <iostream>
#include <fstream>

#include "Prog.h"

using namespace std;


void ReadList(int Array[], int N)
{
	ifstream fin;
	fin.open ("file1.dat");
	for(N=0; N<10; N++)
		fin >> Array[N];
	fin.close();

	ofstream fout;
	fout.open("file2.dat");
		fout << Array[N];
	fout.close();

	cout << Array[N];
}

void Avgs (int Array[], int N, int &Ave, int &AveP, int &AveN)
{
	int sum=0;
	for(N=0; N<10; N++)
	{
		sum = sum + Array[N];
	}
	
	Ave= sum/10;
	
	int N2, N3;
	int pos[10], nega[10], temp;
	int sum_P=0, sum_N=0;
	for(N=0; N<10; N++)
	{
		if (Array[N]>0)
		{
			pos[N2]=Array[N];
			sum_P= sum_P + pos[N2];
			N2++;
		}
		else if (Array[N]<0)
		{
			nega[N3]=Array[N];
			sum_N= sum_N + nega[N3];
			N3++;
		}
		else
		{
			temp++;
		}
	}

	AveP= sum_P/N2;
	AveN= sum_N/N3;

	cout << " " << Ave << " " << AveP << " " << AveN;
	return;
}

int Large (int Array[], int N)
{
	int Max=0;
	for(N=0; N<10; N++)
	{
		if(Array[N]>Max)
		{
			Max=Array[N];
		}
	}
	
	cout << " " << Max;
	return Max;
}

void Display(int Array[ ], int N, int Ave, int AveP, int AveN, int Max)
{
	int a, b;

	ReadList(Array[], N);
	Avgs (Array[], b, Ave, AveP, AveN);
	Large (Array[], N);

	return;
}

Main.cpp

#include <iostream>

#include "Prog.h"

using namespace std;

int main()
{
	int c, d, e, f;

	cout << Display(Array[], N, c, d, e, f);

	return 0;
}

I'm having a problem with:

using namespace std;

\prog.cpp(6): warning C4094: untagged 'class' declared no symbols
\prog.cpp(6): error C2143: syntax error : missing ';' before 'using'

And also in my main.cpp...

ReadList(Array[], N);
	Avgs (Array[], b, Ave, AveP, AveN);
	Large (Array[], N);

\prog.cpp(84): error C2059: syntax error : ']'
\prog.cpp(85): error C2059: syntax error : ']'
\prog.cpp(86): error C2059: syntax error : ']'

int main()
{
	int c, d, e, f;

	cout << Display(Array[], N, c, d, e, f);

	return 0;
}

\main.cpp(11): error C2065: 'Array' : undeclared identifier
\main.cpp(11): error C2059: syntax error : ']'
\main.cpp(11): error C3861: 'Display': identifier not found

i don't know how should i take this error... thank you

i think the error is the missing ; at the end of the class declaration in the header file.

class Prog
{
public:
	void ReadList(int Array[], int N);
	void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN);
	int Large (int Array[], int N);
	void Display(int Array[], int N, int Ave, int AveP, int AveN, int Max);

private:
	int N;
	int Array[10];

}

And,

to define a member function, you give it as:

eg:

void Prog::ReadList()
{
    // statements here
}

Edited 5 Years Ago by myk45: n/a

ReadList(Array[], N);
Avgs (Array[], b, Ave, AveP, AveN);
Large (Array[], N);

When invoking functions, you do not need the [], as "Array" itself is the pointer.

If you write:

void MyFunc(int a[],etc) in your function definition, the compiler converts it to
void MyFunc(int * a, etc)
void Prog::ReadList(int Array[], int N)
{
    // statements here
}

i tried to do like this but having an error of:

prog.cpp(9): error C2039: 'ReadList' : is not a member of '`global namespace''

But there are some requirements for the program to use 3 files, .h for the class- definition and function prototype, .cpp- with those 4 func and, main.cpp- the main cpp file. Now, I'm done in dealing those errors, and no more errors... but the my another problem is I don't know how to execute and cout the results in main.cpp.

Prog.h

#ifndef Prog_h
#define Prog_h

class Prog
{
public:
	Prog();
	void ReadList(int Array[], int N);
	void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN);
	int Large (int Array[], int N);
	void Display(int Array[], int N, int Ave, int AveP, int AveN, int Max);

private:
	int N;
	int Array[10];

};

#endif

Prog.cpp

#include <iostream>
#include <fstream>

using namespace std;

#include "Prog.h"

void Prog::ReadList(int Array[], int N)
{
	ifstream fin;
	fin.open ("file1.dat");
	for(N=0; N<10; N++)
		fin >> Array[N];
	fin.close();

	ofstream fout;
	fout.open("file2.dat");
		fout << Array[N];
	fout.close();

	cout << Array[N];
}

void Prog::Avgs (int Array[], int N, int &Ave, int &AveP, int &AveN)
{
	int sum=0;
	for(N=0; N<10; N++)
	{
		sum = sum + Array[N];
	}
	
	Ave= sum/10;
	
	int N2, N3;
	int pos[10], nega[10], temp;
	int sum_P=0, sum_N=0;
	for(N=0; N<10; N++)
	{
		if (Array[N]>0)
		{
			pos[N2]=Array[N];
			sum_P= sum_P + pos[N2];
			N2++;
		}
		else if (Array[N]<0)
		{
			nega[N3]=Array[N];
			sum_N= sum_N + nega[N3];
			N3++;
		}
		else
		{
			temp++;
		}
	}

	AveP= sum_P/N2;
	AveN= sum_N/N3;

	cout << " " << Ave << " " << AveP << " " << AveN;
}

int Prog::Large (int Array[], int N)
{
	int Max=0;
	for(N=0; N<10; N++)
	{
		if(Array[N]>Max)
		{
			Max=Array[N];
		}
	}
	
	cout << " " << Max;
	return Max;
}

void Prog::Display(int Array[ ], int N, int Ave, int AveP, int AveN, int Max)
{

	ReadList(Array, N);
	Avgs (Array, N, Ave, AveP, AveN);
	Large (Array, N);

}

main.cpp

#include <iostream>

using namespace std;

#include "Prog.h"

int main()
{
	


	return 0;
}

my prof didn't teach it to me and also the class and I find hard, anybody can teach me how to cout results...

my prof didn't teach it to me

That's what that thing made of paper and cardboard that's propping up the leg of your ping pong table is for. Failing that, Google.

and also the class and I find hard, anybody can teach me how to cout results..

Can you clarify what you mean by this?
For example:

int a = 10;
std::cout<<"a= "<<a<<std::endl; //skip the std:: part if you have a using statement

i don't know how to cout the the results of those functions to the main.cpp file

You have to declare a Prog object in main. Your class is defined in an awkward way where you're passing private member variables around (they are contained in the object, so you don't have to do that).

So without having to pass everything around, it might look like:

Prog myProg;
myProg.ReadList();  //since A is a member of your class, why pass it in
myProg.Avgs();
//etc.
This article has been dead for over six months. Start a new discussion instead.