#ifndef Prog_h
#define Prog_h

class Prog
{
public:
	int Array[];
	int N;
	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);
};

#endif

is there something to add in my header? or wrong?i'm making a program for these functions, i just saw it to my friend notes:

[void ReadList(int Array[ ], int N)
This will read in a list of N values, where N is already known

void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN)
Array is a one-dimensional array of integers and N is the number of elements in that array. Both of these are input parameters to the function. The function must calculate 1) the average of the N integers in Array and return the result in Ave, 2) the average of the positive numbers (> 0) and return the result in AveP, and 3) the average of the negative numbers (< 0) and return the result in AveN.

int Large (int Array[], int N)
Array is a one-dimensional array of signed integers and N is the number of elements in that array. The function returns the value of the largest element in the array.

void Display(int Array[ ], int N, int Ave, int AveP, int AveN, int Max)
This will display the list of values (nicely formatted) together with the averages and the largest value.

but theres a condition that i must have a file for .h and .cpp...

#include <iostream>

using namespace std;

int main()
{
	int Array[10]= {4, -30, 0, 7, 42, -20, 18, 400, -123, -6};

	cout << Display(int Array[], int N, int Ave, int AveP, int AveN, int Max)

	return 0;
}

I also make this for main .cpp means that i will use three files. another one is for my four functions. i'm not yet finish in doing those functions cause i dont know if im doing ryt in header. could somebody help me?

First off Array[] and N should be private members, because that is the information that you are trying to hide.

#ifndef Prog_h
#define Prog_h

class Prog
{
private
        int Array[];
	int N;
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);
};

#endif

My next suggestion is that you make this a 3-file program like it suggests.

Name the header for instance prog.h like you have it, then you need to make a .cpp file called prog.cpp and put the function definitions that you declared in your public section of the class. Lastly you have your main .cpp file (named differently from prog.cpp) which has your int main() function that executes whatever you want it to do with your class. Hope this helps.

thanks for your help... can i ask again to you if i need something regarding to this program... great help... :)

where should i declare this:

int Array[10]= {4, -30, 0, 7, 42, -20, 18, 400, -123, -6};

into my main .cpp or in prog.cpp?

#ifndef Prog_h
#define Prog_h

class Prog
{
public:
	int Array[];
	int N;
	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);
};

#endif

is there something to add in my header? or wrong?i'm making a program for these functions, i just saw it to my friend notes:

but theres a condition that i must have a file for .h and .cpp...

#include <iostream>

using namespace std;

int main()
{
	int Array[10]= {4, -30, 0, 7, 42, -20, 18, 400, -123, -6};

	cout << Display(int Array[], int N, int Ave, int AveP, int AveN, int Max)

	return 0;
}

I also make this for main .cpp means that i will use three files. another one is for my four functions. i'm not yet finish in doing those functions cause i dont know if im doing ryt in header. could somebody help me?

Sup.

You can't have "int Array[]" in a class. You need to put a size in there, like int Array[9]. The reason is because an array is a block of memory, and the compiler needs to know much memory EACH instantiation of a class will use, for various reasons. If you don't know what the size is going to be until the user enters input, well then you have to use a dynamic array.

As for where to put the numbers in... you can do it in either place
-Greywolf

is this okay? : :

public:
	void ReadList(int Array[N], int N);

You would want to make a default contructor, a constructor puts the class together and initializes the values, for instance. Also another practice I have learned, you want to change the names of the parameters for your functions to something similar but not the same as your private members, it may cause issues later on.

Private:
     int * Array; // set for array

Public:
     Prog(int theArray [], int n = 0) // by making n = 0, you set a default value to be used if the user fails to provide one

// declaration of Prog() in prog.cpp file

#include "prog.h" // make sure to include your header file

Prog::Prog(int theArray [], int n)
{
     N = n; // set N equal to n value passed or default
     int = new int [N]; // allocate proper amount of memory for array
}

And no you want your ReadList to be like this

public:
     void ReadList() // You don't need to pass anything since you are just going to display the information and the function is called implicitly.

Edited 5 Years Ago by MasterGberry: n/a

What I showed is how you would represent this with a class, but based on your assignment I now realize that you are taking the wrong approach to this. They don't want you to make a class it appears. They want a int main function with some functions and thats it.

// Prog.h -- Using integer arrays

#include <iostream>

using namespace std;

// Prototypes
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);

int main()
{
   // your program goes here along with any function calls
   return 0;
}

void ReadList(int Array[], int N)
{
    // function definition
}
void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN)
{
    // function definition
}

int Large (int Array[], int N)
{
    // function definition
}

void Display(int Array[], int N, int Ave, int AveP, int AveN, int Max)
{
    // function definition
}

do you mean that there's no more need for class? is it okay that i will make my main cpp in another .cpp file and just call other functions for the output?

You do not need a class, the assignment is actually specifically written so you do not use one. You only need one cpp file which is your main. Sorry that I didn't read your assignment sooner.

You can separate your prototypes into a header if your instructor wants you to have a header file, so lines 3-11 in MasterG's example (you could put your functions in a separate .cpp file from the main, you just have to make sure you include the header in both files)

sorry but i didn't tell you about this thing as early:

Program Requirements:
1. Each class must use two separate files: a header file (.h) that contains the class’s definition – data and function prototype; a C++ file (.cpp or .c++) that contains the implementation details – function definitions.
2. Each file must have a head comment that at least includes the program name, your full name and a short description about this file.
3. The main function must use a separate file.

this the condition so i'm thinking to use class but my prof didn't teach it to us. so i'm confusing what should i do...

It's not a problem, it's what I was trying to say in my last post anyway.

you could put your functions in a separate .cpp file from the main, you just have to make sure you include the header in both files

Lines 3-11 above go in the header, lines 13-17 go in the main.cpp file (be sure to include Prog.h in main) and the remainder can go into prog.cpp. To make life easier, include them all in a project or compile both cpp files at the same time.

sorry i didn't tell this to you as early, there are some conditions for the program:

1. Each class must use two separate files: a header file (.h) that contains the class’s definition – data and function prototype; a C++ file (.cpp or .c++) that contains the implementation details – function definitions.
2. Each file must have a head comment that at least includes the program name, your full name and a short description about this file.
3. The main function must use a separate file.

so I'm making three files...

It's really not difficult. Your 3 files are className.h, className.cpp, and main.cpp.

You would organize them as follows:

//Sample.h, the header file, contains class declaration
#ifndef SAMPLE_H
#define SAMPLE_H

class Sample {
 private:
   int m_prvInt;
 public:
   Sample();          //default constructor
   void setInt(int);  //"setter"
};

#endif //SAMPLE_H
//Sample.cpp, implementation file, contains definitions of member methods
#include "Sample.h"

//implement default constructor (version 1)
Sample::Sample() {
  m_prvInt = 0;
}  //end default constructor

/*
//implement default constructor (version 2)
Sample::Sample() : m_prvInt(0) {}
*/

//implement "setter"
void Sample::setInt(int newInt) {
  m_prvInt = newInt;
}
//main.cpp, your main application file, gets everything started at run-time
#include "Sample.h"

int main() {

  /* ... other code ... */

  Sample mySampleObj;
  mySampleObj.setInt(15);

  /* ... other code ... */
  return 0;
}

Once you have all your files written, just make sure that className.cpp (Sample.cpp) and main.cpp are both included in your project.

Edited 5 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.