2 Replies - 4874 Views - Last Post: 27 November 2011 - 04:40 PM Rate Topic: -----

#1 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Classes and arrays

Posted 27 November 2011 - 03:43 PM

Create a PlotNumbers class that has five data members to store five integer values from the user. It should have a 5-parameter constructor that can set the data members to the values provided as arguments, or a default value of 10. The class must also have a plot member function that uses a two-dimensional array to display the numbers as vertical bars. For example, the output for five numbers 2, 4, 3, 6, 3 would be
*
*
* *
* * * *
* * * * *
* * * * *
Write a client program that demonstrates the above class. It should plot two graphs, one for a PlotNumbers object created with default values, and another for the object that gets its five values from the user.

My code so far:

#include <iostream>
#include <string>
using namespace std;

class PlotNumbers
{
private:
		
	int num1,num2,num3,num4,num5;

	int plot(int rows[],int columns[]);
	
public:

	PlotNumbers(int n1,int n2,int n3,int n4,int n5)
	{
		num1 = n1;
		num2 = n2;
		num3 = n3;
		num4 = n4;
		num5 = n5;
	}

	PlotNumbers()
	{
		int allnum = 10;
		num1 = allnum;
		num2 = allnum;
		num3 = allnum;
		num4 = allnum;
		num5 = allnum;
	}
};

int main()
{
	int rows,columns;

	cout << "Enter the number of rows" << endl;
	cin >> rows;



I am confused now..abt how to have classes and arrays together.
THE STARS in the question are messed up.. it is supposed to be lined up as asked in the question

Is This A Good Question/Topic? 0
  • +

Replies To: Classes and arrays

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Classes and arrays

Posted 27 November 2011 - 04:18 PM

The plot member function should be declared under the public visibility specifier, since clients need to access it. The formal parameter list should be empty. Your requirements say "a plot member function that uses a two-dimensional array." That doesn't mean that is should accept arrays as arguments. It simply means a 2D array should be declared and used in its implementation.

class PlotNumbers {
	public:
		void plot();
};



The 2D array could be filled with asterisks where the size of column 1 is num1, the size of column 2 is num2, so on and so forth. Notice that the histogram is displayed in sorted order. If you want to fulfil that requirement, consider creating an auxillary array that contains num1 through num2 in sorted order, and then you can proceed to create the 2D array from it.

class PlotNumbers {

	private:
		int num1, num2, num3;
	public:
		void plot() {
			int intList[3];
			sort(intList);
			int max = maxElement(intList);
			int hist[max][sizeof(intList)/sizeof(int)];
			//fill histogram
			display(hist);
		}
};


Was This Post Helpful? 0
  • +
  • -

#3 qwerty159  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 43
  • Joined: 22-November 10

Re: Classes and arrays

Posted 27 November 2011 - 04:40 PM

View Postblackcompe, on 27 November 2011 - 04:18 PM, said:

The plot member function should be declared under the public visibility specifier, since clients need to access it. The formal parameter list should be empty. Your requirements say "a plot member function that uses a two-dimensional array." That doesn't mean that is should accept arrays as arguments. It simply means a 2D array should be declared and used in its implementation.

class PlotNumbers {
	public:
		void plot();
};



The 2D array could be filled with asterisks where the size of column 1 is num1, the size of column 2 is num2, so on and so forth. Notice that the histogram is displayed in sorted order. If you want to fulfil that requirement, consider creating an auxillary array that contains num1 through num2 in sorted order, and then you can proceed to create the 2D array from it.

class PlotNumbers {

	private:
		int num1, num2, num3;
	public:
		void plot() {
			int intList[3];
			sort(intList);
			int max = maxElement(intList);
			int hist[max][sizeof(intList)/sizeof(int)];
			//fill histogram
			display(hist);
		}
};



@blackcompe ..So u just basically finished the class PlotNumbers for me right ? And Let me try further and i will
post the code again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1