943,639 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 52017
  • C++ RSS
Apr 7th, 2004
1

Creating dynamic array structures

Expand Post »
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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Lippy is offline Offline
1 posts
since Apr 2004
Apr 7th, 2004
0

Re: Creating dynamic array structures

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.
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004
Jun 16th, 2004
0

Re: Creating dynamic array structures

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:
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 17th, 2004
1

Re: Creating dynamic array structures

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).
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Mar 17th, 2006
0

Re: Creating dynamic array structures

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;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
RootUser001 is offline Offline
1 posts
since Mar 2006
Mar 17th, 2006
0

Re: Creating dynamic array structures

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: trouble with counting letter from file, please help.
Next Thread in C++ Forum Timeline: save ARRay elements to a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC