Different data structures have advantages and disadvantages. There is no best data structure that can fit the need of all applications.
Algorithm: Finite sequence of unambigious instructions performed to achieve a goal or compute a desired result.
Each algorithm is not a solution, but instead a precisely defined procedure for deriving solutions.
Each algorithm should specify each of the following:
Write the code to return the sum of 1, 2, 3, ...n
#include <iostream>
using namespace std;
int main() {
int n =4; //can change the value of n here, define an integer
int result = 0; //to store the result, initial value is zero
for (int i=1; i<=n; i++){ //For each integer from 1 to n, add their values to result
result = result +i;
}
return 0;
cout << "The result is:" << result << endl; //Output the value of result
}
#include <iostream>
int main() {
int n = 4; // can change the value of n here.
int result = 0; // to store the result, initial value is zero.
// Unambiguously specified finite sequence of steps below
switch (n){
case 0: result = 0;
break;
case 1: result = 1;
break;
case 2: result = 3;
break;
case 3: result = 6;
break;
case 4: result = 10;
break;
default:
std::cout<<"Error! I did not consider this yet. "<<std::endl;
break;
}
std::cout<<"The result is: "<<result<<std::endl;
return result;
}
#include <iostream>
int main() {
int n = 4; // can change the value of n here.
int result = 0; // to store the result, initial value is zero.
// Unambiguously specified finite sequence of steps below
result = n * (n+1) / 2;
std::cout<<"The result is: "<<result<<std::endl;
return result;
}
Data Structure: A coherent organization of related data items for efficient storage and usage, and methods that operate on the data items.
#include <iostream>
#include <string>
using namespace std;
struct Car {
//all members are public by default
int year;
string brand;
double price;
};
int main() {
Car carInventory[100];
//add car 1
carInventory[0].year = 2002;
carInventory[0].brand = "Honda";
carInventory[0].price = 1000.00;
//add car 2
carInventory[1].year = 1984;
carInventory[1].brand = "Porsche";
carInventory[1].price = 40000.00;
//print all
cout<<"The first car is: " << carInventory[0].year << " " << carInventory[0].brand << ", price: " << carInventory[0].price<< endl;
cout<<"The second car is: " << carInventory[1].year << " " << carInventory[1].brand << ", price: " << carInventory[1].price<< endl;
return 0;
}
What's the problem with procedure based programming? - not well organized, data is not protected, okay for a small project in which you're the only user.
Every other function can directly access and modify other variables in structures.
#include <iostream>
#include <string>
using namespace std;
struct Car {
//all members are public by default
int year;
string brand;
double price;
};
class CarRecord {
//all members are private by default
Car carInventory[100];
int size = 0;
public:
void addCar (int year, string brand, double price);
void printAll ();
};
void CarRecord::addCar(int year, string brand, double price) {
carInventory[size].year = year;
carInventory[size].brand = brand;
carInventory[size].price = price;
size++;
}
void CarRecord::printAll() {
for (int i = 0; i < size; i++){
cout<<"Car #" << i << ": " << carInventory[i].year << " " << carInventory[i].brand << ", price: " << carInventory[i].price<< endl;
}
}
int main() {
CarRecord inventory;
//add car 1
inventory.addCar(2002, "Honda", 1000.00);
//add car 2
inventory.addCar(1984, "Porsche", 40000.00);
//print all
inventory.printAll();
return 0;
}
In object oriented programming,
Algorithm: Sequence of clear instructions to achieve a goal
Data Structure: Data items and related methods about the same object.