Initializing an array using pointers

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 26689 Views - Last Post: 12 August 2013 - 11:38 PM Rate Topic: -----

#1 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Initializing an array using pointers

Posted 18 March 2013 - 10:20 AM

Hi, im working through some online tutorials at the moment and ive gotten stuck when it comes to the pointers/arrays.
i have a Struct called transaction containing
#pragma once

#include <string>
using std::string;

///////////////////////////////////////////////////////////////////////////////
// Stock Buy Transaction                  								     //
///////////////////////////////////////////////////////////////////////////////

/**
 * Stock sell transaction
 */
struct Transaction
{
	string stockSymbol;		// String containing the stock symbol, e.g. "AAPL"
	string buyerName;		// String containing the buyer's name e.g. "Mr Brown"
	int buyerAccount;		// Integer containing an eight digit account code
	int numShares;			// Integer containing the number of sold shares
	int pricePerShare;		// Integer containing the buy price per share
};


im looking to initialize this struct using
Analyser::Analyser(Transaction* transactions, int numTransactions)


problem is i dont have a clue how? ://>

i have this
	/**
	 * Build a random transaction
	 */	 
	static Transaction* buildTransactions(int numTransactions)
	{
		int maxShareVolume = 100000;
		int maxSharePrice = 1000;

		Transaction *transactions = new Transaction[numTransactions];

		for(int idx = 0; idx < numTransactions; idx++)
		{
			transactions[idx].stockSymbol = pickRandomStockSymbol();

			std::string buyerName = pickRandomBuyer();
			transactions[idx].buyerName = buyerName;
			transactions[idx].buyerAccount = lookupBuyerAccount(buyerName);

			transactions[idx].numShares = 1 + rand() % maxShareVolume;
			transactions[idx].pricePerShare = 1 + rand() % maxSharePrice;		
		}

		return transactions;
	}

within a file they called setup. im not quite sure how relivant this is?

very new to c++ so if anyone could help it would be great.

Is This A Good Question/Topic? 0
  • +

Replies To: Initializing an array using pointers

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 01:57 PM

The buildTransactions builds an dynamic array with numTransactions elements.

04  static Transaction* buildTransactions(int numTransactions)



The dynamic array

09    Transaction *transactions = new Transaction[numTransactions];




It inserts some data into the array as well.


You can create a Transaction pointer in a calling function, to point to the array. The array can then be passed to your Analyser variable/object at construction.

  const int NUMBER = 3;

  Transaction *transactions;

  transactions = buildTransactions(NUMBER);

  Analyser analyser(transactions, NUMBER);

  ...

  delete[] transactions;


Was This Post Helpful? 0
  • +
  • -

#3 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 02:16 PM

I had a feeling if it was to initialize the array it would have to be in the form of a loop and not a constant int?

how would i write this if it was to initialize it within the
Analyser::Analyser(Transaction* transactions, int numTransactions)

constructor?

i think what the pointer is to do is to point to the first of the array and run thought it? that was my understanding of it anyway?

I emailed the author of the problem and they replied with this "As to the constructor, it basically accepts an array of Transaction instances. They are been passed to the constructor using a pointer which points to the start of the array (i.e the first element in the array) and a counter which tells you the size of the array. By incrementing the pointer you can pointer to the other elements in the array."
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 05:06 PM

View Postd0nni, on 18 March 2013 - 09:16 PM, said:

I had a feeling if it was to initialize the array it would have to be in the form of a loop and not a constant int?


If you look at the function buildTransactions it creates an array of size = numTransactions. I used a constant it doesn't have to be a constant, it makes some sense though. In the function the array is also initialized with some random data.

View Postd0nni, on 18 March 2013 - 09:16 PM, said:

how would i write this if it was to initialize it within the
Analyser::Analyser(Transaction* transactions, int numTransactions)



constructor?


Do you you mean in the call to the constructor or in the constructor function?
Do you mean create the array or initializing the array, or both?
Why would you want to that? That limits the use of the Analyse class.

View Postd0nni, on 18 March 2013 - 09:16 PM, said:

i think what the pointer is to do is to point to the first of the array and run thought it? that was my understanding of it anyway?


Just as easy to use [] as in buildTransactions :

11	    for(int idx = 0; idx < numTransactions; idx++)
12	    {
13	        transactions[idx].stockSymbol = pickRandomStockSymbol();




View Postd0nni, on 18 March 2013 - 09:16 PM, said:

I emailed the author of the problem and they replied with this "As to the constructor, it basically accepts an array of Transaction instances. They are been passed to the constructor using a pointer which points to the start of the array (i.e the first element in the array) and a counter which tells you the size of the array. By incrementing the pointer you can pointer to the other elements in the array."


If you are determined to use a pointer something like :

Analyser::Analyser(Transaction* transactions, int numTransactions)
{
  for(int idx = 0; idx < numTransactions; idx++, transactions++)
  {
    cout << transactions->stockSymbol << endl;
  }  
}


Was This Post Helpful? 0
  • +
  • -

#5 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 05:27 PM

see the question is based on pointers and arrays, thats why trying to stick to that.

"
Analyser(Transaction* transactions, int numTransactions);

Constructor that accepts a pointer to an array of Transaction instances. The size of the array is specified. The function can assume that a validly sized array will be passed."

thats the first part of the question being asked. i just dont really understand what they are asking. i had figured it would have been constructed similar to the build transactions.

i had
Analyser::Analyser(Transaction* transactions, int numTransactions)
{
  for(int idx = 0; idx < numTransactions; idx++, transactions++) 
but its what to throw into it which i dont understand, i donno if its just me or its the wording of the question.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 05:53 PM

It depends what you want to do with your Analyse class.

You could just store the pointer address to the array and the number of elements, then use them later in the class.
Was This Post Helpful? 0
  • +
  • -

#7 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 06:00 PM

i think thats all i have to do at the moment in the analyse class, cause there are two other classes after which are setTransaction and addTransaction. the set transaction completely replaces any transactions with the new transactions placed. and the addTransaction just adds a new transaction onto the array.
Was This Post Helpful? 0
  • +
  • -

#8 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 06:47 PM

what way would the analyse class be if it was to just store the pointer address to the array and the number of elements?
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 07:16 PM

Have a go at writing some of the class and adding in the member variables.

Classes
Was This Post Helpful? 0
  • +
  • -

#10 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 07:37 PM

im just so confused lol. The way im reading the guideline "As to the constructor, it basically accepts an array of Transaction instances. They are been passed to the constructor using a pointer which points to the start of the array (i.e the first element in the array) and a counter which tells you the size of the array. By incrementing the pointer you can pointer to the other elements in the array."

if i was to break it down this is my understanding of it.

Analyser::Analyser(Transaction* transactions, int numTransactions) <-- that is the class accepting the array of transaction instances, being passed using the pointer.

for(int idx = 0; idx < numTransactions; idx++, transactions++) <-- this is it pointing to the start of the array, and incrementing the pointer as it goes through the array.

{ } after the for. i donno what im supposed to throw in. my immediate thought would be to collect the variables from the transaction struct, and throw them here. sort of like this:

string stockSym = stockSymbol; // String containing the stock symbol, e.g. "AAPL"
string buyerNM = buyerName; // String containing the buyer's name e.g. "Mr Brown"
int buyerACC = buyerAccount; // Integer containing an eight digit account code
int numShar = numShares; // Integer containing the number of sold shares
int PPS = pricePerShare; // Integer containing the buy price per share

assuming that by doing so that would allow me to edit the array from the analyser.cpp class?
Was This Post Helpful? 0
  • +
  • -

#11 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 08:01 PM

Analyser::Analyser(Transaction* transactions, int numTransactions)
{
	string stoSym;
	string buyNm;
	int buyAcc;
	int numShare;
	int PPS;

	 for(int idx = 0; idx < numTransactions; idx++, transactions++)
  {

	transactions[idx].stockSymbol = stoSym;
	transactions[idx].buyerName = buyNm;		// String containing the buyer's name e.g. "Mr Brown"
	transactions[idx].buyerAccount = buyAcc;		// Integer containing an eight digit account code
	transactions[idx].numShares = numShare;			// Integer containing the number of sold shares
	transactions[idx].pricePerShare = PPS;

  } 
}


??? lol :/
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 08:14 PM

The array of transactions stays outside of the class, but the pointer allows access to the array.



class Analyser 
{
  private:
    Transaction* m_transactions;
    int m_numTransactions;

  public:
    Analyser::Analyser(Transaction* transactions, int numTransactions);

    void display();

};


// constructor : 
//   copy pointer to array 
//   copy number of elements in the array
Analyser::Analyser(Transaction* transactions, int numTransactions) 
{
  m_transactions = transactions;
  m_numTransactions = numTransactions;
}





I imagine you would only need to edit one element/transaction at a time.

How do you want to indicate which transaction to edit? By an index value to the array?
Was This Post Helpful? 0
  • +
  • -

#13 d0nni  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-December 10

Re: Initializing an array using pointers

Posted 18 March 2013 - 08:19 PM

i would imagine so, kinda have like have a cout << "Enter the Transaction you would like to edit: "; with a cin of the transaction number so would be like transaction[idx+1] to index it to that transaction?
Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 08:37 PM

You should look at the differences between deep copy and shallow copy and see which one applies to your assignment.

#define shows an example of a shallow copy, a deep copy involves physically coping the memory rather than just copying the pointer address. Deep copying is probably what the assignment requires to preserve encapsulation.
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,505
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 08:40 PM

I would have a function in the class something like :

  void edit(int index);




So you can ask for the index outside of the class and pass it to the function. This gives you an option to use the function after searching for a stock perhaps.

If you display the transactions you could display an index value.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2