task Problem set Methode

Pages: 12
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?
Tipps?
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?
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?
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 ?
Can somebody help?
Somebody there ?
Topic archived. No new replies allowed.
Pages: 12