data structure

Hello !
Can any one please tell me how is data structure useful.
for example
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
struct product 
{ int weight;
  float price;
};
int main()
{ product fruit;
fruit.weight=5;
cout<<fruit.weight;
return 0;
}


This program can be easily made as
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
int main()
{ int fruit;
fruit=5;
cout<<fruit;
return 0;
}

Think a bit further...what if you have 7 fruits, each with its own weight, price, colour, texture, etc.? Then your program can not be "easily made". Of if you had to pass a fruit - with weight, price, colour, texture etc. - as an argument to a function?

Data structures are useful in a huge number of ways, most notably in that they collate related data in one structure (make, model and colour of a car, for example).

Answering your question is a bit like answering "why are cars useful?"
Can you please give an example of a program using stuctures with seven fruits having different properties showing it as important as a car is ?
Last edited on
I don't know if you find game development interesting, but here is an easy one related to it. Let's say you want to give a function called save the information about a player. Consider the following function declaration:

bool savePlayerData(std::string playerName, bool isMale, bool isAlive, int health, int level, int strength, int dexterity, int agility, int attack, int defense, int numberOfEnemiesKilled, int numberOfDeaths, int xCoordOfSavePoint, int yCoordOfSavePoint, /* ...lots of potential other data that you need to know about the player... */);

That means that you'll have to keep track of that information separately for the player. Now, what about monsters....and what if you have 20 of them? 200? Lets say that there are 25 data members you need for each monster. Imagine keeping track of 200 arrays of 25 members each. Now imagine writing the functions to use them. That's going to become a major pain in your butt very quickly.

Now, consider that you put all of those data members into a structure. Now you can rewrite that function prototype to this:
bool savePlayerData(Player savePlayer);

A little bit easier? 200 monsters suddenly becomes:
Monster gameMonsters[200];

Much easier to deal with then 25 parallel arrays. And you can do this to make things a lot faster too:
bool savePlayerData(const Player& savePlayer);

That will send the memory address of the Player variable so that only one value needs to be given to savePlayerData. The const part in there means that it can't be modified, so get rid of that if you will need to modify it.

Your question is incredibly broad so I'm going to leave it at that. With practice and learning you'll come to love structs. ^^
But, I though Object-Oriented-programming was more efficient than the structured one, no? I'm so confused right now :(

I'm not quite sure what you mean. I was just explaining a possible use of structures in C/C++. The way I said it would be applicable in either language so I wasn't saying that either was more efficient than the other.

Both C and C++ are very efficient languages, granted that you know how to use them well. I'm learning a lot right now myself.

The only thing that I did talk about that pertains to efficiency is the difference between pass-by-value and pass-by-reference-to-const. Let's say that you have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void passByValue(int x)
{
   x = 5;
}

int main()
{
   int myX = 4;
   passByValue(myX);
   cout << myX << endl;

   return 0;
}


What will be output? ...the answer is 4. When you call passByValue you're actually sending the value of myX, not myX itself. So a new local int variable gets created on the stack for passByValue and it is given the value 4, which is then assigns the value 5 over. When control leaves the function x is destroyed and control returned to main where myX is still equal to 4.

Now let's take this code:
1
2
3
4
void passByReference(int& x)
{
   x = 5;
}


If you used this in the same way as I did passByValue you'll now see that myX is equal to 5 afterwards. The & is the address-of-operator or reference operator:
http://cplusplus.com/doc/tutorial/pointers.html

When you're just sending an integer over it's really no difference in terms of speed from what I can tell. You're generally sending a 32-bit value over regardless. But in the case of a large structure with many different variables, sending a memory address is a *lot* faster than creating a new structure and copying over every value of it.

Does that help?
Topic archived. No new replies allowed.