Initializing an array using pointers

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:51 PM

see for adding into the array, i know how to approach this when its just a simple array such as the likes of this
int FindDuplicates[20]; // my little array
int UserInput; // the number from the user to compare
int count = 0; // the number of numbers entered

do
{
    cout << "Please enter a number between 10 and 100: "; // print out what I would like
    cin >> UserInput; // get the number from the user

    if (UserInput >= 10 && UserInput <= 100)
    count++;
} while (count < 20);


the whole pointer thing is throwing me way off though.

is there an example i could use that would use the data from multiple arrays such as name, address, postcode etc. where it would be 'add a new name: ' *user input*, 'add a new address: ' *user input* etc
Was This Post Helpful? 0
  • +
  • -

#17 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 - 09:16 PM

like if i was to do this...

void Analyser::addTransactions(Transaction* transactions, int numTransactions)
{

	Transaction* newTran = new Transaction;
	
	for(int i = 0; i < numTransactions; i++)
	{
		cout << "Enter a StockSymbol: ";
		cin >> Transaction.stockSymbol;

		cout << "Enter a Buyer Name: ";
		cin >> Transaction.buyerName;

		cout << "Enter a Buyer Account: ";
		cin >> Transaction.buyerAccount;

		cout << "Enter the number of Shares: ";
		cin << Transaction.numShares;

		cout << "Enter the Price per Share: ";
		cin >> Transaction.pricePerShare;

}
}



thats wrong, but to me, i don't know why it would be wrong?
Was This Post Helpful? 0
  • +
  • -

#18 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 - 09:29 PM

another method i just thought of would be this:

void Analyser::addTransactions(Transaction* transactions, int numTransactions)
{
	Transaction transaction[numTransactions]; <-- this is throwing an error
 
for(int i = 0; i < numTransactions; i++)
{
    Setup::printTransactions(&transaction[i], numTransactions);
}
 
for (i = 0; i < numTransactions; i++) <-- the first i is throwing an error
{
    cout<<"Enter a Stock Symbol: " << transaction[i].stockSymbol <<endl;
    cout <<"Enter a Buyer Name: "<< transaction[i].buyerName <<endl;
    cout <<"Enter a Buyer Account: " << transaction[i].buyerAccount << endl;
	cout <<"Enter the Number of Shares: " << transaction[i].numShares << endl;
	cout <<"Enter the Price per Share: " << transaction[i].PricePerShare << endl;
}
}

This post has been edited by JackOfAllTrades: 19 March 2013 - 01:25 PM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#19 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,510
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 09:31 PM

The function buildTransactions already creates an array of transactions with data. Why do you need to change it?
Was This Post Helpful? 0
  • +
  • -

#20 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 - 09:41 PM

this is the buildTransaction class:

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


how would i use that to add data to the transactions array?

void Analyser::addTransactions(Transaction* transactions, int numTransactions)


i would assume from this that all i would really need to have as user input would be the amount of shares, and the price per share.

This post has been edited by d0nni: 18 March 2013 - 09:47 PM

Was This Post Helpful? 0
  • +
  • -

#21 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,510
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 10:03 PM

I would not make it a function of Analyser necessarily.

// add one transaction
static Transaction* addTransaction(Transaction* oldTransactions, int &numTransactions, Transaction &addedTransaction)
{
  // make new and bigger array
  Transaction *transactions = new Transaction[numTransactions+1];

  // copy current data
  for(int idx = 0; idx < numTransactions; idx++)
  {
    transactions[idx] = oldTransactions[idx];
  }

  // add new record
  transactions[numTransactions] = addedTransaction;
  numTransactions++;
  
  // delete old array
  delete[] oldTransactions;

  return transactions;
}



We probably need more information on what the classes are supposed to do.
Was This Post Helpful? 0
  • +
  • -

#22 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 - 10:30 PM

aye thats weird too cause the addTransaction is a void so it cannot return anything. you've been amazing tonight for me, but how would you approach it granted its a void and that is has to use the analyser cause in the main it has function call
 	Analyser analyser(transactions, numTransactions);
	analyser.addTransactions(moreTransactions, numTransactions);

Was This Post Helpful? 0
  • +
  • -

#23 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,510
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 18 March 2013 - 11:35 PM

View Postd0nni, on 19 March 2013 - 05:30 AM, said:

aye thats weird too cause the addTransaction is a void so it cannot return anything. you've been amazing tonight for me, but how would you approach it granted its a void and that is has to use the analyser cause in the main it has function call
 	Analyser analyser(transactions, numTransactions);
	analyser.addTransactions(moreTransactions, numTransactions);



The addTransactions function would be similar but different in some respects. There is an array pointed to by a variable in the class, and an array pointed to by a parameter of the function. These two arrays should be combined into one. You need to create an array big enough for both sets of data. Copy the data. Tidy up.


// add transactions

void Analyser::addTransactions(Transaction* moreTransactions, int numMoreTransactions)
{
  // make new and bigger array
  // using a temporary pointer
  // what will be the size?

  // copy current data (pointed to in class) to new array
  // copy data from moreTransactions array also to new array

  // delete old array (in the class)

  // update the pointer to array in the class  
  // update the number of transactions in the class

}


.

This post has been edited by #define: 18 March 2013 - 11:36 PM

Was This Post Helpful? 0
  • +
  • -

#24 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 - 11:55 PM

lol im in way over my head here - been tryin to get this going for the past 20 hrs too lol. just can't seem to wrap my head round it at all. and just getting exhausted now lol.
Was This Post Helpful? 0
  • +
  • -

#25 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,510
  • Joined: 19-February 09

Re: Initializing an array using pointers

Posted 19 March 2013 - 09:56 AM

The first part is to create the bigger dynamic array in memory, so have a go at that.

05	  // make new and bigger array
06	  // using a temporary pointer
07	  // what will be the size?




Dynamic Memory
Was This Post Helpful? 0
  • +
  • -

#26 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 12 August 2013 - 11:38 PM

#Define - i was flicking through old posts of mine and never thanked you for your help on this. so yeah, thanks for spending your time walking me through this problem :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2