So, I tried doing this exercise, I compiled it and there were no errors nor warnings so it seemed fine to me.
This is the exercise:
1° Part
Create a list of entries made but the couple (key, info) represented with a vector of records. The record then creates the type entry which is made by "key" (integer) and "info" (C string dynamically allocated). For this type we have the following functions:
- read(e) // Let us insert and entry with the keyboard.
- write(e) // Let us print the entry on the screen.
- copy(e1,e2) // Copies e2 in e1.
- equal(e1, e2) // Let us know if the entries are equals.
- greater_than(e1, e2) // Let us know if the entry e1 is greater than entry e2.
Hypotesis: Two entries are equal only if both the values "key" and "info" are equal.
An entry is greater than another if the integer "key" of the first entry is greater than the integer "key" of the second entry.
The list is represented with a vector of elements of a set max dimension.
With this data struct we have the following functions:
- read(l,dim) // Let us initialize the list and insert the elements with the keyboard.
- write(l,dim) // Let us print the list on the screen.
- in(l,dim,x,pos) // Let us know if the element "x" exist in the list. If yes "pos" let us know the position of the first occurrence.
- sort(l,dim) // Sort the list (ascending order).
2° Part
Add to the list the following functions:
- full(l,dim) // Let us know if the list is full.
- empty(l,dim) // Let us know if the list is empty.
- insert(l,dim,e) // Let us insert an element in the list in the first free position.
- insert_sort(l,dim,e) // Applied to a sorted list. Let us insert the element "e" respecting the order.
- remove(l,dim,e) // Let us delete the entry "e" from the list if it exists.
- is_sorted(l,dim) // Let us know if the list is sorted (ascending order).
Skip the 2° part for now.
This is my code(I put everything in a project):
Entry.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <cstring>
using namespace std;
struct Entry {
int Key;
char *Info;
};
void create_Entry (Entry &);
void destroy_Entry (Entry &);
void read_Entry (Entry &);
void write_Entry (const Entry &);
void copy_Entry (Entry &, const Entry &);
bool equal_Entry (const Entry &, const Entry &);
bool greater_than_Entry (const Entry &, const Entry &);
|
Entry.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include "Entry.h"
void create_Entry (Entry &E) {
E.Key = 0;
E.Info = new char[0];
}
void destroy_Entry (Entry &E) {
delete [] E.Info;
E.Key = 0;
}
void read_Entry (Entry &E) {
char buffer[100];
cout << "Enter the key(must be a integer): ";
cin >> E.Key;
cout << "Enter the info: ";
cin.getline(buffer, 100);
if(E.Info) {
delete [] E.Info;
}
E.Info = new char[strlen(buffer)+1];
strcpy(E.Info, buffer);
}
void write_Entry (const Entry &E) {
cout << "Key: " << E.Key << "\nInfo: " << E.Info << "\n";
}
void copy_Entry (Entry &E1, const Entry &E2) {
if (strlen(E1.Info) != strlen(E2.Info)) {
delete [] E1.Info;
E1.Info = new char[strlen(E2.Info)+1];
}
strcpy(E1.Info, E2.Info);
E1.Key = E2.Key;
}
bool equal_Entry (const Entry &E1, const Entry &E2) {
return(E1.Key == E2.Key && (!strcmp(E1.Info, E2.Info)));
}
bool greater_than_Entry (const Entry &E1, const Entry &E2) {
return(E1.Key > E2.Key);
}
|
List.h
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "Entry.h"
const int SIZE = 10;
typedef Entry T;
typedef T List[SIZE];
void read_List(List &, int &);
void write_List(const List &, const int);
bool in_List(const List &, const int, const T &, int &);
void sort_List(List &, const int);
void swap(T &, T &);
void destroy_List(List &, int &);
|
List.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "List.h"
void read_List(List &L, int &n) {
cin >> n;
for (int i = 0; i < n; i++) {
create_Entry(L[i]);
read_Entry(L[i]);
}
}
void write_List(const List &L, const int n) {
for (int i = 0; i < n; i++) {
write_Entry(L[i]);
cout << "\n";
}
}
bool in_List(const List &L, const int n, const T &x, int &pos) {
bool found = false;
for (int i = 0; i < n; i++) {
if(equal_Entry(L[i], x)) {
found = true;
pos = i;
break;
}
}
return found;
}
void swap(T &x, T &y) {
T temp;
copy_Entry(temp, x);
copy_Entry(x, y);
copy_Entry(y, temp);
}
void sort_List(List &L, const int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < (n - i - 1); j++) {
if (greater_than_Entry(L[j], L[j+1])) {
swap(L[j], L[j+1]);
}
}
}
}
void destroy_List(List &L, int &n) {
for (int i = 0; i < n; i++) {
destroy_Entry(L[i]);
}
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include "List.h"
int main(int argc, char** argv) {
List L;
Entry E;
int dim = 0, pos = 0;
create_Entry(E);
cout << "Insert the number of elements and the entries:\n";
read_List(L, dim);
cout << "List:\n";
write_List(L, dim);
cout << "Insert the element to search:\n";
if (in_List(L, dim, E, pos)) {
cout << "The element exist. The first occurence is in position " << pos << "\n";
}
else {
cout << "The element does not exist.\n";
}
cout << "Sorted list:\n";
sort_List(L, dim);
write_List(L, dim);
cout << "Destroying the list...\n";
destroy_List(L, dim);
cout << "Printing the list...\n";
write_List(L, dim);
return 0;
}
|
So, the program starts correctly but when I have inserted the first "key" of the firt "entry" instead of letting me insert the "info" it skips it and makes me insert the second "key" of the second "entry". Did I do wrong the allocation? To me it seems okay.
P.S. Sorry for the wall of code but I need you to know everything I did so that you can help me in this. Thank you!