Help Experts

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
using namespace std;
template <typename T>
class List
{
	int size;
	T *values;
public:
	List(): size(0), values(NULL) {}
	~List()
	{
		delete [] values;
		size=0;
	}
	int getSize() { return size; }
	T at(int index) { return values[index]; }
	void at(int index, T value) { values[index]=value; }
	void insert(T value)
	{
		/*
		1. if size is zero
		a. declare values to be an array of type T having size=1
		b. place value on index zero of values
		c. increment size
		d. return from the function
		2. declare a temporary array of type T havin size equal to 'size + 1'
		3. copy all elements of values into the temporary array
		4. place value at last index of the temporary array
		5. assign the temporaty array to values
		6. increment size
		*/
	}
	void insert(int index, T value)
	{
		/*
		1. if index is less than zero or greater than size
		a. return from the function
		2. if size is equal to zero and index is not equal to one
		a. return from the function
		3. if size is equal to zero and index is equal to zero
		a. declare values to be an array of type T having size=1
		b. place value on index zero of values
		c. increment size
		d. return from the function
		4. declare a temporary array of type T having size equal to 'size + 1'
		5. starting at i=0 and ending at i=one less than index, copy all i elements
		of values onto the i elements of the temporary array
		6. place value at the 'index' index of the temporary array. note: 'index' is
		being passed to the function
		7. starting at i='index' and ending at i=one less than size, copy all i
		elements of values onto i+1 elements of the temporary array
		8. assign the temporary array to values
		9. increment size
		*/
	}
	void remove()
	{
		/*
		1. if size is equal to zero
		a. return from the function
		2. if size is equal to one
		a. assign zero to size
		b. assign NULL to values
		c. return from the function
		3. declare a temporary array of type T having size equal to 'size - 1'
		4. starting at i=0 and ending at i=one less than size-1, copy all i elements
		of values onto the i elements of the temporary array
		5. assign the temporary array to values
		6. decrement size
		*/
	}
	void remove(int index)
	{
		/*
		1. if size is equal to zero
		a. return from the function
		2. if index is less than zero or greater than size
		a. return from the function
		3. if size is equal to one and index is not equal to zero
		a. return from the function
		4. if size is equal to one
		a. assign zero to size
		b. assign NULL to values
		c. return from the function
		5. declare a temporary array of type T having size equal to 'size - 1'
		4. starting at i=0 and ending at i=one less than index, copy all i elements
		of values onto the i elements of the temporary array
		5. starting at i=index and ending at i=one less than size, copy all i+1
		elements of values onto the i elements of the temporary array
		6. assign the temporary array to values
		7. decrement size
		*/
	}
};


Write Code according to the comments.
I also wish for happier life.
You got everything you need in comments.
Last edited on
That wouldn't be help! That would be whacking you over the head with a gold-plated solution that you wouldn't even be able to understand, let alone turn in!

-Albatross
please help me in only one fuction please
You should ask in Beginners forum, as some forum user said (i don't remember witch one):

decompose, decompose...


I'll start for you:
1
2
3
4
5
6
7
8
9
10
11
void insert(T value)
	{
		if(size == 0)//1. if size is zero
                {
		    values = new T[1];//a. declare values to be an array of type T having size=1 
                    values[0] = value//b. place value on index zero of values
		    ++size;//c. increment size
      		    return;//d. return from the function
                }
        ...
       }
Last edited on
please complete the first fuction.
Last edited on
That would go against our principle of "Do not solve homework problems". Sorry omerslam. :(

By the way, you have a step by step solution in English for each of the functions. What's the problem with writing them yourself? <:|

-Albatross
array is my problem.i don't know the concept of array that why i asked for help.please help me in only one function.i will try to do remaining task myself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void insert(T value)
	{
		if(size == 0)
		{
			values = new T[1];
			values[0] = value;
				++size;
			return;
		}
		T* temp = new T[size + 1];
		for(int i = 0; i < size; ++i)
			temp[i] = values[i];//copy elements to temp
		temp[size] = value;//insert value on last position
		delete [] values;//delete values so we can
		values = temp;// point on new array without memory leaks
		++size;//increment size
	}
Last edited on
delete [ ] values;
what is this.tell me about this.
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<iostream>
using namespace std;
template <typename T>
class List
{
	int size;
	T *values;
public:
	List(): size(0), values(NULL) {}
	~List()
	{
		delete [] values;
		size=0;
	}
	int getSize() { return size; }
	T at(int index) { return values[index]; }
	void at(int index, T value) { values[index]=value; }
	void insert(T value)
	{
		if(size==0)
		{
			values=new T[1];
			values[0]=value;
			size++;
			return;
		}
		T *temp=new T[size+1];
		for(i=0;i<size;i++)
			temp[i]=values[i];
		temp[size]=value;
		values=temp;
		size++;
		/*
		1. if size is zero
		a. declare values to be an array of type T having size=1
		b. place value on index zero of values
		c. increment size
		d. return from the function
		2. declare a temporary array of type T havin size equal to 'size + 1'
		3. copy all elements of values into the temporary array
		4. place value at last index of the temporary array
		5. assign the temporaty array to values
		6. increment size
		*/
	}
	void insert(int index, T value)
	{
		if(index<0||index>size)
			return;
		if(size==0&&indez!=1)
			return;
		if(size==0&&index==0)
			values=new T[1];
		values[0]=value;
		size++;
		return;
		T *temp=new T[size+1];
		for(int i=0;i<index-1;i++)
			temp[i]=values[i];
		temp[value];
		for(i=index;i<size-1;i++)
			temp[i+1]=values[i];
		values=temp;
		size++;
		/*
		1. if index is less than zero or greater than size
		a. return from the function
		2. if size is equal to zero and index is not equal to one
		a. return from the function
		3. if size is equal to zero and index is equal to zero
		a. declare values to be an array of type T having size=1
		b. place value on index zero of values
		c. increment size
		d. return from the function
		4. declare a temporary array of type T having size equal to 'size + 1'
		5. starting at i=0 and ending at i=one less than index, copy all i elements
		of values onto the i elements of the temporary array
		6. place value at the 'index' index of the temporary array. note: 'index' is
		being passed to the function
		7. starting at i='index' and ending at i=one less than size, copy all i
		elements of values onto i+1 elements of the temporary array
		8. assign the temporary array to values
		9. increment size
		*/
	}
	void remove()
	{
		if(size==0)
			return;
		if(size==1)
			size=0;
		values=NULL;
		return;
		T *temp=new T[size-1];
		for(i=0;i<size-2;i++)
			temp[i]=values[i];
		values=temp;
		size--;
		/*
		1. if size is equal to zero
		a. return from the function
		2. if size is equal to one
		a. assign zero to size
		b. assign NULL to values
		c. return from the function
		3. declare a temporary array of type T having size equal to 'size - 1'
		4. starting at i=0 and ending at i=one less than size-1, copy all i elements
		of values onto the i elements of the temporary array
		5. assign the temporary array to values
		6. decrement size
		*/
	}
	void remove(int index)
	{
		if(size==0)
			return;
		if(index<0||index>size)
			return;
		if(size==1&&index!=0)
			return;
		if(size==1)
			size=0;
		values=NULL;
		return;
		T *temp=new T[size-1];
		for(i=0;i<index-1;i++)
			temp[i]=values[i];
		for(i=index;size-1;i--)
			temp[i]=values[i+1];
		values=temp;
		size--;
		/*
		1. if size is equal to zero
		a. return from the function
		2. if index is less than zero or greater than size
		a. return from the function
		3. if size is equal to one and index is not equal to zero
		a. return from the function
		4. if size is equal to one
		a. assign zero to size
		b. assign NULL to values
		c. return from the function
		5. declare a temporary array of type T having size equal to 'size - 1'
		4. starting at i=0 and ending at i=one less than index, copy all i elements
		of values onto the i elements of the temporary array
		5. starting at i=index and ending at i=one less than size, copy all i+1
		elements of values onto the i elements of the temporary array
		6. assign the temporary array to values
		7. decrement size
		*/
	}
};
void main()
{
	system("pause");
}


This is my Code.
Check the code according to the comments.
Please Experts Check all the mistakes in the code and correct the mistakes.
Last edited on

delete [ ] values;
what is this.tell me about this.

For every call with 'new' you must call 'delete' to free data that pointer point at.

If you don't call delete on first insert function you'll get memory leak. But if it is insisted to
write code as if it is in comments then do so (even if its wrong). Also you got undeclared identifier 'i' that
you used in loop. I don't know witch compiler you are using (or forced to use) but it might asign default type
as int.

In second insert function part 3 you need to open braces after if statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void insert(int index, T value)
	{
		if(index<0||index>size)
			return;
		if(size==0&&index!=1)
			return;
		if(size==0&&index==0)
                { 
			values=new T[1];
		        values[0]=value;
		        size++;
	        	return;
                }
		T *temp=new T[size+1];
		for(int i=0; i <= index-1; i++) // i <= index - 1 
			temp[i]=values[i];
		temp[index] = value;//temp[value]; <-wrong
		for(i=index;i<=size-1;i++)// i <= size - 1
			temp[i+1]=values[i];
		values=temp;
		size++;
	}

note that you again have memory leak if you don't call delete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void remove()
	{
		if(size==0)
			return;
		if(size==1)
                { //braces added
			size=0;
		        values=NULL;
		        return;
                } 
		T *temp=new T[size-1];
		for(i=0;i<=size-2;i++)//same as insert '<='
			temp[i]=values[i];
		values=temp;
		size--;
}

again memory leak without delete

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void remove(int index)
	{
		if(size==0)
			return;
		if(index<0||index>size)
			return;
		if(size==1&&index!=0)
			return;
		if(size==1)
                { //braces again
			size=0;
		        values=NULL;
		        return;
                }
		T *temp=new T[size-1];
		for(i=0;i<=index-1;i++)
			temp[i]=values[i];
		for(i=index;i <= size-1;i++)
			temp[i]=values[i+1];
		values=temp;
		size--;
}

also leak without delete.

EDIT <offtopic>:

Please Experts Check all the mistakes in the code and correct the mistakes.

I am not an expert, i am also a beginner with c++. I didn't even have any programming language at school, this is my hobby.
So that you know that any code that i provide might not be reliable.
Last edited on
thanks for replying
my last question.make main function for the whole program.
That's not a question, that is a request!

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
int main()
{
	//make list of int
	List<int> li;

	//test first insert function
	li.insert(10);
	li.insert(15);
	li.insert(20);

	// print content
	std::cout << "After first insert function: " << std::endl;
	for(int i = 0; i < li.getSize(); ++i)
	{
		std::cout << "listInt[" << i << "] = " << li.at(i) << std::endl;
	}

	//test second insert, insert num 19 at index 2
	li.insert(2, 19);

	//print content
	std::cout << "After second insert function: " << std::endl;
	for(int i = 0; i < li.getSize(); ++i)
	{
		std::cout << "listInt[" << i << "] = " << li.at(i) << std::endl;
	}

	//print content
	std::cout << "After first remove function (last element removed): " << std::endl;
	li.remove();
	for(int i = 0; i < li.getSize(); ++i)
	{
		std::cout << "listInt[" << i << "] = " << li.at(i) << std::endl;
	}

	//print content
	std::cout << "After second remove function (first element removed): " << std::endl;
	li.remove(0);
	for(int i = 0; i < li.getSize(); ++i)
	{
		std::cout << "listInt[" << i << "] = " << li.at(i) << std::endl;
	}

	std::cout << "Press enter to exit...";
	std::cin.get();
	return 0;
}


You'll not learn nothing this way!
You'll not learn nothing this way!


Which is exactly why you shouldn't be giving him these answers.
Topic archived. No new replies allowed.