Hi all it's me again with another question as I got further in my book. The
chapter I am in covers structres, abstract data and classes. I only read
through to the end of the coverage on structures. Trying to comprehend this
is harder than I thought it was going to be. I should of just skipped this
chapter and went right into pointers since they seem to be easier to use.
But anyways here i smy question:
you define a structure example:
struct phonerec
{
string fname;
string lname;
string number;
}phone[100];
If the above is right from what I understand phone will hold 100 fname,100
lname and 100 number?
to store data in in phone I would have to do somethinglike this if I
understood it all right.
phone[0].fname = "test value 1";
phone[0].lname = "test value 3";
phone[0].number = "123-456-4323"
Now from what I have read you can not use I/O streams to access structures,
only members of structures. so lets say that all 101 records are filled in
phone. If I wanted to output it all to a file I can do something like this:
outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << phone[x].fname;
outfile <<" ";
outfile << phone[x].lname;
outfile <<" ";
outfile << phone[x].number;
outfile << endl;
}
Is there an easier way to output the data from the structure to the file?
I guess I should of just typed in my code into the compiler to actually see
if this works myself first and if it didn't figue out why it didn't but
ususally when I read something and go over it I get the point it's when I
try to do something I didn't read on that I have problems with it.
Now the next question I have for the more experienced programmers is with
the above data structure, what I have defined above the same as the
following below and if so how is one more efficient than the other:
string fname[100];
string lname[100];
string number[100];
//assume all 101 of each have data in them
outfile.open("datafile.dat");
{
for (x=0; x<=100; x++)
outfile << fname[x];
outfile <<" ";
outfile << lname[x];
outfile <<" ";
outfile << number[x];
outfile << endl;
}
If all of the above is correct then I have understood the concept of how a
data structure works and is in the simple form, I did not yet read up on
structures within structures yet, this chapter is going to take me a little
longer than any of the other chapters in the book to grasp. I do truly wish
I could just say forget it and forget it even exists. Although structures,
Data Abstraction and classes were created for the purposes of making coding
easier but more complex than other alternatives.
Thank you for all replies to this message,
Shawn Mulligan 1 2701
kazack wrote: Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on structures. Trying to comprehend this is harder than I thought it was going to be.
Actually the idea behind structures is very simple:
Things that belong together should stay together.
Example: A date.
A date always consists of a day, month and year. A day alone, a month
alone a year alone does not specify a date. Only the triplet of all of
them does.
Thus I group them together:
struct Date
{
int Day;
int Month;
int Year;
}
So whenever I talk about a 'Date', I mean the triplet of Day/Month/Year.
And whenever I pass I date around, I pass the whole triplet around.
I should of just skipped this chapter and went right into pointers since they seem to be easier to use.
Suprises are waiting for for.
In fact, pointers are not much of use, unless you have structures.
But anyways here i smy question:
you define a structure example:
struct phonerec { string fname; string lname; string number; }phone[100];
If the above is right from what I understand phone will hold 100 fname,100 lname and 100 number?
In principle you are right. Deep in the internals of memory there are 100
fname, 100 lname and 100 number. But you should change your thinking.
phone consists of 100 phonerecs.
Each phonerec consists of 1 fname, 1 lname and 1 number. to store data in in phone I would have to do somethinglike this if I understood it all right.
phone[0].fname = "test value 1"; phone[0].lname = "test value 3"; phone[0].number = "123-456-4323"
Yep. There it is again:
phone all phones (there are 100 of them, so which one?)
phone[0] aha, the phonerec with index 0. But a phonerec consists
of fname, lname and number, So which one?
phone[0].number Aha. The number field from the phonerec with index 0 Now from what I have read you can not use I/O streams to access structures, only members of structures.
That's not entirely true, but it makes no real difference.
The thing is: A structure is a collection of fields. All of them form
the structure. The stream I/O routines on the other hand are designed
to work with basic data types, like int, double etc.. But a struct
isn't such a basic data type, it consists of them eventually, but it isn't.
so lets say that all 101 records are filled in phone. If I wanted to output it all to a file I can do something like this:
outfile.open("datafile.dat"); { for (x=0; x<=100; x++) outfile << phone[x].fname; outfile <<" "; outfile << phone[x].lname; outfile <<" "; outfile << phone[x].number; outfile << endl; } Is there an easier way to output the data from the structure to the file?
No. That's the way you do it. You could write a function for outputing one
record and call that function in the loop. That would be a better organization
most of the time, but basically it's just the same thing: you output each
field individually (Who says that you need to output the whole structure?
What about formatting of the individual fields? What about seperating text?) I guess I should of just typed in my code into the compiler to actually see if this works myself first and if it didn't figue out why it didn't but ususally when I read something and go over it I get the point it's when I try to do something I didn't read on that I have problems with it.
Reading is just one thing. You actually need to practice programming.
Knowing about if, for, while and all the other keywords is not enough.
Beeing able to combine things is what programming is all about. It is
like plaing chess. Just knowing how the pieces can move is not enough
to make you a chess player. Now the next question I have for the more experienced programmers is with the above data structure, what I have defined above the same as the following below and if so how is one more efficient than the other:
At your level just forget ybout efficiency right now. Learn to walk,
then learn to run, then learn to run fast, then try to win the NY marathon.
Not the other way round.
string fname[100]; string lname[100]; string number[100];
Those are 3 arrays. There is no connection between them. //assume all 101 of each have data in them
They don't. An array
string fname[100]
has exactly 100 elements. Their valid indices are 0 .. 99
(count them, there are exactly 100 numbers in 0 .. 99) outfile.open("datafile.dat"); { for (x=0; x<=100; x++) outfile << fname[x]; outfile <<" "; outfile << lname[x]; outfile <<" "; outfile << number[x]; outfile << endl; }
If all of the above is correct then I have understood the concept of how a data structure works and is in the simple form, I did not yet read up on structures within structures yet,
That's not a big deal:
// A Date consists of a Day, a Month, a Year
struct Date
{
int Day;
int Month;
int Year;
};
// A Name consists of a FirstName, a LastName
struct Name
{
string FirstName;
string LastName;
};
// a person is described by
// a Name (which contains a firstname and a lastname
// a Birthdate ( which consists of a day, a month, a year
struct Person
{
Name TheName;
Date BirthDate;
};
this chapter is going to take me a little longer than any of the other chapters in the book to grasp. I do truly wish I could just say forget it and forget it even exists.
Don't.
For one it's your entry point into a whole new world of dynamic data structures.
Also they make coding easier and simpler.
Also maintenance is easier.
Consider that you want to upgrade your program which uses aboves Person structure.
You want to add an address to the person. Simple just add a structure
struct Address
{
string Town;
string Street;
int Streetnumber;
}
and upgrade the person:
struct Person
{
Name TheName;
Date BirthDate;
Address TheAddress;
};
And throughout the whole program whenever you have a person object,
you also have it's address. If you wouldn't have used structures, you
would have needed to add the address to all the function calls and
parameter lists.
Although structures, Data Abstraction and classes were created for the purposes of making coding easier but more complex than other alternatives.
Not really. In the end everything becomes simpler. I don't need to worry any
longer to pass Day/Month/Year to a function. I just pass it a Date and know
that it receives everything needed to descrive a date.
--
Karl Heinz Buchegger kb******@gascad.at This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: achrist |
last post by:
Here's a program:
#include <stdio.h>
int main(int argc, char **argv)
{
double an_array = {0.0, 1.0, 2.0, 3.0, 4.0};
typedef struct a_struct {double v0, v1, v2,v3, v4;} a_type;
typedef...
|
by: theshowmecanuck |
last post by:
As a matter of academic interest only, is there a way to programmatically
list the 'c' data types? I am not looking for detail, just if it is
possible, and what function could be used to...
|
by: Shwetabh |
last post by:
Hi everyone.
My question is, why are data structures implemented only with struct
data type?
Why not union when it is more efficient as compared with structures?
Thanks in advance
|
by: emma middlebrook |
last post by:
Hi
Straight to the point - I don't understand why System.Array derives
from IList (given the methods/properties actually on IList).
When designing an interface you specify a contract. Deriving...
|
by: jack |
last post by:
Hi guys,
I am working on a project which requires an implementation of discrete
event simulation in C using linked lists. I would greatly appreciate if
someone could provide with some sources...
|
by: Steven |
last post by:
Hi I'm new to js so this prob is a simple question but I haven't been
able to solve it.
I have 2 text fields which let me pick a graphic file and want to
display then in 2 image place holders so...
|
by: sethukr |
last post by:
Hi everybody,
While running the following program in GCC, i'm very much screwed.
main()
{
char *ptr1;
char arr;
int i;
char *ptr2;
|
by: Art Cummings |
last post by:
Good morning all. I just finished an assignment using structures. In this
assignment I used an array of structures. What I would have liked was to
use an array of structures with a function. ...
|
by: Marcin Kasprzak |
last post by:
Hello Guys,
Silly question - what is the most elegant way of compiling a code similar
to this one?
<code>
typedef struct a {
b_t *b;
} a_t;
typedef struct b {
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |