Hello all. I'm currently studying computer science, and I need some insight concerning data structures.

i'm asked to create the necessary data structure to hold the population and saving factor of all cities.

CITY: Population: Saving factor:
Nicosia 200 0.4
Limassol 120 0.6
Larnaka 65 0.7
Paphos 30 0.75
Paralimni 15 0.8

And this is my code:

#include <iostream.h>
#include <sstream>
using namespace std;

struct Cyprus {
  string city;
  int population;
  float factor;
  } nicosia, limassol, larnaka, paphos, paralimni;


int main ()
{

nicosia.population = 200 ;
nicosia.factor = 0.4 ;
limassol.population = 120 ;
limassol.factor = 0.6 ;
larnaka.population = 65 ;
larnaka.factor = 0.7 ;
paphos.population = 30 ;
paphos.factor = 0.75;
paralimni.population = 15 ;
paralimni.factor = 0.8 ;
}

Is this correct or wrong? why?

Recommended Answers

All 5 Replies

Instead of creating an instance for each city you can just create an array of those structures. for example struct Cyprus cities[4]; How to populate the array may depend on the instructions your professor gave you. My guess is that your program should read the data from a text file, buy that may be 100% wrong.

>>is this correct or wrong? why?

what if you wanted to add more cities, say 100 more. How much code do you think you
would have to write ? Thus a good code, not only cares about its present status, it also cares about its future status as well.

Instead of creating an instance for each city you can just create an array of those structures. for example struct Cyprus cities[4]; How to populate the array may depend on the instructions your professor gave you. My guess is that your program should read the data from a text file, buy that may be 100% wrong.

No, the exercise just says to "create the necessary data structure to hold the population and saving factor of all cities."

If I brought what i wrote up there in, would it be correct or wrong? This is what I need to know.

Thanks *alot* for your answer.

We don't know how your teacher grades.. and what you have learned so far that you could apply. So saying yes it is correct would be a bold statement.

But this is indeed a data structure that holds the information listed. However you forgot to initialize the 'city' part of the structure. On the other hand if you just need to store the population and saving factor you could remove the string city.

Following are some Data Structure Sample questions:

1. What is data structure?

Answer: A data structure is a way of organizing data that considers not only the items stored, but also their relationship to each other. Advance knowledge about the relationship between data items allows designing of efficient algorithms for the manipulation of data.

2. List out the areas in which data structures are applied extensively?

Answer: The name of areas are:

* Compiler Design,
* Operating System,
* Database Management System,
* Statistical analysis package,
* Numerical Analysis,
* Graphics,
* Artificial Intelligence,
* Simulation

3. What are the major data structures used in the following areas : RDBMS, Network data model & Hierarchical data model.

Answer: The major data structures used are as follows:

* RDBMS - Array (i.e. Array of structures)
* Network data model - Graph
* Hierarchical data model - Trees

4. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Answer: The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.

5. Minimum number of queues needed to implement the priority queue?

Answer: Two. One queue is used for actual storing of data and another for storing priorities.

6. What is the data structures used to perform recursion?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.