task Problem set Methode
Jul 4, 2018 at 8:42am UTC
I have at the moment also a problem with task 2 now:
Implement the operator + = method. In this method a new array of calendar entries with the required size should be created with each call. In this array, the existing entries (if any) and the new entry should be copied so that they are sorted in ascending order.
Has somebody some tipps how I can sort the Array ?
I have always difficulties with algorithms
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
#ifndef CCALENDAR_H_
#define CCALENDAR_H_
#include "CJulianDate.h"
#include "CCalendarEntry.h"
#include<iostream>
using namespace std;
class CCalendar{
private :
CCalendarEntry* m_entries = 0;
unsigned int m_numberOfEntries = 0;
public :
CCalendar();
~CCalendar();
CCalendar& operator += (const CCalendarEntry& entry);
void print(const CJulianDate& from);
void print();
};
#endif /* CCALENDAR_H_ */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include"CCalendar.h"
CCalendar::CCalendar(){
m_entries = new CCalendarEntry [m_numberOfEntries];
}
CCalendar::~CCalendar(){
delete [] m_entries;
}
CCalendar& CCalendar::operator += (const CCalendarEntry& entry){
}
In this method a new array of calendar entries with the required size should be created with each call
How should I do this?
Do they mean that I should use the array which I have already created in the constructor ?
Or should I create a new one and how ? Again in the constructor?
Jul 4, 2018 at 9:45pm UTC
Tipps?
Jul 5, 2018 at 7:42am UTC
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
CCalendar& CCalendar::operator +=(const CCalendarEntry& entry) {
CCalendarEntry *newEntries = new CCalendarEntry[m_numberOfEntries + 1];
unsigned int tmp = 0;
for (unsigned int i = 0; i<m_numberOfEntries; i++){
if (entry > m_entries){
tmp = entry[i+1];
entry[i+1] = m_entries[i];
m_entries[i] = tmp;
}
}
if (m_entries != nullptr )
delete [] m_entries;
++m_numberOfEntries;
m_entries = newEntries;
return *this ;
}
Would this algorithm work?
Jul 5, 2018 at 3:12pm UTC
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
CCalendar& CCalendar::operator +=(const CCalendarEntry& entry) {
CCalendarEntry *newEntries = new CCalendarEntry[m_numberOfEntries + 1];
for (unsigned int j = 0; j< m_numberOfEntries+1; i++){
for (unsigned int i = 0; i<m_numberOfEntries; i++){
if (newEntries[j+1] > entry[i] ){
newEntries[j+1]= entry[i];
entry[i] = newEntries[j+1];
delete [] newEntries;
}
}
}
if (m_entries != nullptr )
delete [] m_entries;
++m_numberOfEntries;
m_entries = newEntries;
return *this ;
}
Is this algorithm a little better?
Jul 6, 2018 at 12:35pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
CCalendar& CCalendar::operator +=(const CCalendarEntry& entry) {
CCalendarEntry * oldEntries = m_entries;
CCalendarEntry * newEntries = new CCalendarEntry [m_numberOfEntries + 1];
for (unsigned int i=0; i<m_numberOfEntries+1; i++){
newEntries[i] = oldEntries[i];
}
newEntries[m_numberOfEntries+1] = entry;
m_entries = newEntries;
delete [] oldEntries;
m_numberOfEntries++;
}
Somebody there ?
Jul 8, 2018 at 12:50pm UTC
Can somebody help?
Jul 10, 2018 at 6:51am UTC
Somebody there ?
Topic archived. No new replies allowed.