Creating dynamic array structures

Closed Thread

Join Date: Apr 2004
Posts: 1
Reputation: Lippy is an unknown quantity at this point 
Solved Threads: 0
Lippy Lippy is offline Offline
Newbie Poster

Creating dynamic array structures

 
1
  #1
Apr 7th, 2004
I am learning C++ using Stephen Pratas "C++ Primer Plus" (excellent book btw). However i am stuck on exercise 6 chapter 4, where in essence i just have to use new to allocate the array of structures dynamically. I can allocate a single structure dynamically, however i still do not know how to initialise all the members at one time, only each member seperately by using cin e.g. i can only do:

cin.get(structure1->name, 20);

and i do not know how to initialise a whole dynamically allocated structure like i can with a normal 1 e.g. i cant do this:

structure1 object1 =
{
"Balh blah blah".
1.88.
5
}

Can please someone help. thnx in advance
Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 7
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: Creating dynamic array structures

 
0
  #2
Apr 7th, 2004
structure object1 = {1,2,3,4,5,6,etc};
structure object2={"something","anything", "another thing"};

Now if u wanted "something" from object 2 to correspond to a number from object one you can use parralell arrays.

But a matrix would be better.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Quick reply to this message  
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: Creating dynamic array structures

 
0
  #3
Jun 16th, 2004
Ok--try this (i'm not sure it's perfect...13 yr olds can make mistakes)

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
struct nod
{
int info;
nod *adr;
};
nod *vf,*sf;// *vf-is the the first element in the list
// *sf- is the last element
void add(int val,nod* &vf)
{
if (!vf) //lista este goala
{
vf=(nod*)malloc(sizeof(nod));
vf->info=val;
sf=vf;
}
else
{
nod *c=(nod*)malloc(sizeof(nod));
c->info=val;
c->adr=NULL;
sf->adr=c;
sf=c;
}
}
void list(nod *vf)
{
nod *c;
c=vf;
while (c)
{
printf("%i ",c->info);
c=c->adr;
}
}
/*void insert(int vc,int vi,nod *vf)
{
nod *c;
c=vf;
while (c->info!=vc) c=c->adr;
nod *e=(nod*)malloc(sizeof(nod));
e->info=vi;
e->adr=c->adr;
c->adr=e;
}*/ //it inserts a nod in the list after looking for a value-vc
// you don't need the insert(...) function
void main()
{
clrscr();
int n;//it reads the numer of elements which will be in the list
printf("n=");scanf("%i",&n);
for (int i=0;i<n;i++)
{
int a;//value you read-- i assumed it was a list
printf("data:");// with int information-and nothing else
scanf("%d",&a);
add(a,vf);
list(vf);//writes the list on stdout (screen)
}


Hope this helps :cheesy:
Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: Creating dynamic array structures

 
1
  #4
Jun 17th, 2004
Not sure I understand the request, but using new for dynamic arrays goes like this:

void CreateArray( int numberToCreate )
{
// this allocates space and calls the constructor on each element.
AnObject* theDynamicArray = new AnObject[ numberToCreate ];

// example of use
for (int i = 0; i < numberToCreate; i++)
DoSomething( theDynamicArray[ i ] ); // gets passed as AnObject, not AnObject*

delete [] theDynamicArray; // destructor of each element gets called.
}

The constructor would be responsible for initializing the object, of course, but if you want to load some initial data in from the console or a resource or something, you'd have to do that post-construction. Like, in the DoSomething() routine. Also, a routine like DoSomething() should take AnObject as a reference so it can be changed (and so it doesn't need to be copied by the compiler).
Quick reply to this message  
Join Date: Mar 2006
Posts: 1
Reputation: RootUser001 is an unknown quantity at this point 
Solved Threads: 0
RootUser001 RootUser001 is offline Offline
Newbie Poster

Re: Creating dynamic array structures

 
0
  #5
Mar 17th, 2006
I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise. I sorted that out and I'm posting my source code so you may continue with the following chapters. If someone can come up with a nicer or more elegant solution please post.

#include <iostream>
using namespace std;

int main()
{
struct CandyBar //struct declaration
{
char *brand;
float weight;
int calories;
};

//dynamic array declaration
CandyBar *Candies = new CandyBar[3];
CandyBar *CandyPointer = &Candies[0]; //pointer to a CanyBar data object

//structure initialization one by one
(*CandyPointer).brand = "Mocha Munch";
CandyPointer->weight = 2.3;
CandyPointer->calories = 350;

CandyPointer = &Candies[1];
(*CandyPointer).brand = "Chilly Willy";
CandyPointer->weight = 2.2;
CandyPointer->calories = 200;

CandyPointer = &Candies[2];
(*CandyPointer).brand = "Lemon Rush";
CandyPointer->weight = 2.1;
CandyPointer->calories = 200;

//data output to the screen

//first element of the array
CandyPointer = &Candies[0];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

//second element of the array
CandyPointer = &Candies[1];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

//third element of the array
CandyPointer = &Candies[2];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

cin.get();
return 0;
}
Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Creating dynamic array structures

 
0
  #6
Mar 17th, 2006
>I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise.
This thread is two *years* old. If you have a question, start a new thread rather than resurrect one that's already begun to decay. We can't stand the smell of necrothreads.
I'm here to prove you wrong.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC