7 Replies - 2859 Views - Last Post: 07 October 2008 - 05:56 PM Rate Topic: -----

#1 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Structures and Arrays

Posted 06 October 2008 - 08:29 PM

Ok I have an assignment due.. and I need help tackling this assignment! Here is the info I have.. Im just really confused on how to start this project...

For these two labs you are going to construct functionality to create a simple address book. Conceptually the address book uses a structure to hold information about a person and an array of structures to hold multiple persons (people).

Visually think of the address book like so:



When you add a person to the Address Book you add a structure with the information about the person to the end of the array:



When you get a person, you get the first person in the address book. With each successive call to get a person, you get the next person in the array. For instance the first call to get a person you will get "Joe Smith" when you make the second call to get a person you would get "Jane Doe" so on and so forth. After you get the last person from the array the next call to get a person will start over at the beginning ("Joe Smith" in this case).

Details:
1.) Create a project called addressBook
2.) Add a header file and cpp file for your project
3.) All of your definitions should go in the header file.
4.) In the header file create the definition for your structure. Call it PERSON.
5.) Your structure should have fields for first name, last name, address, and optionally a phone number.
6.) Inside the cpp file you will create the functionality for your address book
7.) Inside the cpp file declare a global array of 10 PERSONS to hold all of the records in your address book call it people. Use a const called MAXPEOPLE to set the size of the array. Put the const in the header file.
8.) You are probably going to want to declare an integer variable to keep track of where you are at in the array.
9.) Create functions addPerson, getPerson
10.) These functions should take as arguments a reference to a PERSON structure.
11.) The addPerson method should copy the structure passed to it to the end of the array
12.) the getPerson should start at array element 0 and with each successive call return the next person in the array.
13.) Create overloaded findPerson functions. One function should take only the persons last name
14.) The other function should take both the persons last and first names.
15.) All code for the functions should be in the cpp file
16.) From main write functionality that will test your address book code

All of the functions that are part of the address book should take a reference to a PERSON structure as one of its arguments. This is not necessarily the only argument for each function but should be one of them.

Is This A Good Question/Topic? 0
  • +

Replies To: Structures and Arrays

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Structures and Arrays

Posted 06 October 2008 - 08:35 PM

The site's policy is to show some effort before you're helped, but I'll give you an idea of how to proceed.

class AddressBook
{
     //What exactly is an address book?
    //it is a listing of people's names, phone #
    // and addresses
    //So we're looking at a three dimensional array, yuck right?

   //STL Maps would be ideal here since you can associate second
    // tier information with the person's name as the key of the Map


//*******ALTERNATE IDEA*******
   //Define a struct or another class called Person, it will
   //contain person's name, address, etc... see below
}

struct Person //you can make an array of these
{
      char* name; // or string, depends on what you feel like
      int phoneNum;
      string address; // or a char pointer/array, again up to you
}



Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Structures and Arrays

Posted 06 October 2008 - 08:36 PM

View Postkillakev, on 6 Oct, 2008 - 11:29 PM, said:

Ok I have an assignment due.. and I need help tackling this assignment! Here is the info I have.. Im just really confused on how to start this project...

Do you know how to make a struct?

I would suggest that you start there & if you need help with making one, I used one in my store code snippet.

If you have any questions or errors, please ask!
Was This Post Helpful? 0
  • +
  • -

#4 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Re: Structures and Arrays

Posted 07 October 2008 - 10:38 AM

#include <iostream>
using namespace std;
bool addPerson (PERSON p);
bool getPerson (PERSON &p);
bool findPerson (char *frame, PERSON &p);
bool findPerson (char *frame, char *lName, PERSON &p);
void printBook();

struct PERSON
{
	char fName[100];
	char lName[100];
}

const int MAXPEOPLE 3
PERSON people[MAXPEOPLE];
int head = 0;
int tail = 0;

bool addPerson (PERSON p)
{
	if (head) = MAXPEOPLE)
	{
		return false;
	}
	people [head] = p;
	head ++;
	return true;
}

bool getPerson(PERSON &p)
{
	if (head == 0)
		return false;
	if(tail) = MAXPEOPLE)
	{
		tail = 0;
	}
	P= people[tail];
	tail ++;
	return true;
}

bool findPerson(char *lName, PERSON &p)
{
	for (int i=0; i<=head; i++)
	{
		if(!stricmp(lName, people[i].lName)
		{
			p = people[];
			return true;
		}
	}
	return false;
}
void printBook()
{
	for (int i=0; i<=head; i++)
	{
		cout<<people[i].fName<<endl;
		cout<<people[i].lName<<endl;
	}
}


int main()
{
	PERSON peeps[]= {{"Joe", "Blow"}, {"Sam", "Smith"}, {"Bill","Jones"}};
	bool status = addPerson(peeps[0]);
	PERSON p;
	bool status= getPerson(p);
	if (status == true)
		cout<<p.lName<<" "<<p.fName;
	else;
}



ok heres what I have done so far.. I tried the code and its not working can you go over for it me thnx
Was This Post Helpful? 0
  • +
  • -

#5 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Structures and Arrays

Posted 07 October 2008 - 11:55 AM

i dunno what the first poster is talking about 3d arrays for, it seems the assignment wants a single dimension array of structures/objects.. this is actually a pretty easy assignment... you are doing this in c++ right ? i think they wanted seperate files for .h and implementation and driver file..

This post has been edited by Sadaiy: 07 October 2008 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Structures and Arrays

Posted 07 October 2008 - 01:35 PM

I mentioned 3D arrays for a reason. Let's say the assignment does not specify what type of data structure to use. Let's say the OP only had dealt with arrays of primitives. He or she may decide that the best way to go is with a 3D array to store information. It is an example of the planning process that helps produce better code.
Was This Post Helpful? 0
  • +
  • -

#7 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Re: Structures and Arrays

Posted 07 October 2008 - 05:50 PM

View PostSadaiy, on 7 Oct, 2008 - 11:55 AM, said:

i dunno what the first poster is talking about 3d arrays for, it seems the assignment wants a single dimension array of structures/objects.. this is actually a pretty easy assignment... you are doing this in c++ right ? i think they wanted seperate files for .h and implementation and driver file..


It only requires a header file... Yes i am doing this for c++ and I would like some input on my code and why it returns so many errors
Was This Post Helpful? 0
  • +
  • -

#8 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: Structures and Arrays

Posted 07 October 2008 - 05:56 PM

Line 2 says it wants header and a CPP file... you can't put int main() in a header file it must be in the cpp, so put your code for your header also so i can see all the code... you didn't put up your class/ head file code.. also i think you should put the struct in the header..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1