Skip to content

Commit 7bd4207

Browse files
committed
chapter 4 exercises about pointers
1 parent 71a652e commit 7bd4207

9 files changed

+980
-7
lines changed

4_0_variable_length_string.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef char * arrayString;
99
void append(arrayString &str, char ch);
1010
void concatenate(arrayString &str1, arrayString str2);
1111
char characterAt(arrayString str, int position);
12-
int getLength(arrayString str);
12+
int length(arrayString str);
1313

1414
int main()
1515
{
@@ -42,12 +42,12 @@ int main()
4242

4343
char characterAt(arrayString str, int position)
4444
{
45-
return str[position];
45+
return str[position-1];
4646
}
4747

4848
void append(arrayString &str, char ch)
4949
{
50-
int oldLen = getLength(str);
50+
int oldLen = length(str);
5151
arrayString newStr = new char[oldLen + 2];
5252

5353
for (int i = 0; i < oldLen; i++)
@@ -62,8 +62,8 @@ void append(arrayString &str, char ch)
6262

6363
void concatenate(arrayString &str1, arrayString str2)
6464
{
65-
int len1 = getLength(str1);
66-
int len2 = getLength(str2);
65+
int len1 = length(str1);
66+
int len2 = length(str2);
6767
int newLen = len1 + len2;
6868
arrayString newStr = new char[newLen+1];
6969
for (int i = 0; i < len1; i++)
@@ -79,7 +79,7 @@ void concatenate(arrayString &str1, arrayString str2)
7979
str1 = newStr;
8080
}
8181

82-
int getLength(arrayString str)
82+
int length(arrayString str)
8383
{
8484
int len = 0;
8585
while(str[len] != 0)

4_1_find_modes_variable_list.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#include <iostream>
2+
#define D_MAX_SIZE 256
3+
4+
/*
5+
Find modes without using hash maps, just arrays.
6+
*/
7+
8+
using std::cin;
9+
using std::cout;
10+
using std::endl;
11+
12+
void append(int* &arr, int num, int arrSize);
13+
int * findModes(int arr[], const int ARR_SIZE);
14+
void printModes(int modesList[]);
15+
16+
int main()
17+
{
18+
int num = 0;
19+
int arraySize = 1;
20+
21+
22+
int* numList = new int[1];
23+
24+
cout << "This program finds mode of a list of integers.\n";
25+
cout << "Type in numbers to and press enter to fill the list, finish by entering -1.\n";
26+
cout << "You need to type at least 1 number: \n";
27+
28+
cin >> num;
29+
cin.ignore();
30+
numList[0] = num;
31+
32+
cin >> num;
33+
cin.ignore();
34+
35+
36+
37+
while (num != -1 && arraySize < D_MAX_SIZE)
38+
{
39+
append(numList, num, arraySize);
40+
arraySize++;
41+
cin >> num;
42+
cin.ignore();
43+
}
44+
45+
cout << "\nThe numbers in the list are:\n[";
46+
47+
for (int i = 0; i < arraySize; i++)
48+
{
49+
cout << numList[i] << ",";
50+
}
51+
cout << "]\n";
52+
53+
54+
55+
int * modesList = findModes(numList, arraySize);
56+
57+
printModes(modesList);
58+
59+
60+
cin.get();
61+
return 0;
62+
}
63+
64+
void append(int* &arr, int num, int arrSize)
65+
{
66+
67+
int* newArr = new int[arrSize + 1];
68+
for (int i = 0; i < arrSize; i++)
69+
{
70+
newArr[i] = arr[i];
71+
}
72+
newArr[arrSize] = num;
73+
delete[] arr;
74+
arr = newArr;
75+
}
76+
77+
78+
int * findModes(int arr[], const int ARR_SIZE)
79+
{
80+
int countTable[ARR_SIZE*2] = {0}; // Saves data like [value, count, value, count, value, count].
81+
int * modesList = nullptr;
82+
int * tempModesList = new int[ARR_SIZE+1]; // First value is nr of modes, other values or the modes.
83+
tempModesList[0] = 0;
84+
85+
86+
int maxCount = 0;
87+
int val;
88+
int nrOfValues = 0; // Nr of different values in the array, in the examples above that is only 10 (numbers from 0 to 9).
89+
int nrOfModes = 0;
90+
91+
for (int i = 0; i < ARR_SIZE; i++)
92+
{
93+
val = arr[i];
94+
int j = 0;
95+
while(val != countTable[2*j] && j < nrOfValues) // Check if value is in array
96+
{
97+
j++;
98+
}
99+
if (j == nrOfValues) // If value not in array, add value in array
100+
{
101+
countTable[2*j] = val;
102+
nrOfValues++;
103+
}
104+
else // Else increase the count
105+
{
106+
countTable[2*j+1]++;
107+
if (maxCount < countTable[2*j+1])
108+
{
109+
maxCount = countTable[2*j+1];
110+
}
111+
}
112+
}
113+
114+
// Get the modes
115+
for (int i = 0; i < ARR_SIZE; i++)
116+
{
117+
if (maxCount == countTable[2*i+1])
118+
{
119+
tempModesList[++nrOfModes] = countTable[2*i];
120+
tempModesList[0] = nrOfModes;
121+
}
122+
}
123+
if (nrOfModes >= nrOfValues)
124+
{
125+
tempModesList[0] = 0;
126+
nrOfModes = 0;
127+
}
128+
129+
modesList = new int[nrOfModes+1];
130+
131+
for (int i = 0; i <= nrOfModes; i++)
132+
{
133+
modesList[i] = tempModesList[i];
134+
}
135+
136+
delete[] tempModesList;
137+
return modesList;
138+
}
139+
140+
141+
void printModes(int modesList[])
142+
{
143+
const int NR_OF_MODES = modesList[0];
144+
145+
if (!NR_OF_MODES)
146+
{
147+
cout << "The array has no modes!\n";
148+
return;
149+
}
150+
151+
else if (1 == NR_OF_MODES )
152+
{
153+
cout << "The mode is: " << modesList[1] << endl;
154+
}
155+
156+
else
157+
{
158+
cout << "The modes are: " << modesList[1];
159+
for (int i = 2; i < NR_OF_MODES; i++)
160+
{
161+
cout << ", " << modesList[i];
162+
}
163+
164+
cout << " and " << modesList[NR_OF_MODES] << "\n";
165+
}
166+
167+
}
168+

4_2_substring.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <iostream>
2+
#define D_ARRAY_SIZE 12
3+
4+
5+
using std::cout;
6+
using std::cin;
7+
using std::endl;
8+
9+
10+
typedef char * arrayString;
11+
12+
void append(arrayString &str, char ch);
13+
void concatenate(arrayString &str1, arrayString str2);
14+
char characterAt(arrayString str, int position);
15+
arrayString substring(arrayString str, int startPos, int len);
16+
int length(arrayString str);
17+
18+
int main()
19+
{
20+
arrayString str = new char[D_ARRAY_SIZE];
21+
22+
for(int i = 0; i < D_ARRAY_SIZE-1;i++)
23+
{
24+
str[i] = 'a' + i;
25+
}
26+
str[D_ARRAY_SIZE-1] = 0;
27+
28+
29+
30+
int startPos = 3;
31+
int len = ;
32+
33+
arrayString newStr = substring(str, startPos, len);
34+
35+
cout << "The string: " << str << endl;
36+
cout << "The substring: " << newStr << endl;
37+
38+
cin.get();
39+
return 0;
40+
41+
}
42+
43+
44+
arrayString substring(arrayString str, int startPos, int len)
45+
{
46+
arrayString newStr = new char[len+1];
47+
48+
startPos--;
49+
50+
int strLength = length(str);
51+
52+
for (int i = 0; i < len; i++)
53+
{
54+
newStr[i] = str[i+startPos];
55+
}
56+
newStr[len] = 0;
57+
return newStr;
58+
}
59+
60+
61+
char characterAt(arrayString str, int position)
62+
{
63+
return str[position];
64+
}
65+
66+
void append(arrayString &str, char ch)
67+
{
68+
int oldLen = length(str);
69+
arrayString newStr = new char[oldLen + 2];
70+
71+
for (int i = 0; i < oldLen; i++)
72+
{
73+
newStr[i] = str[i];
74+
}
75+
newStr[oldLen] = ch;
76+
newStr[oldLen + 1] = 0;
77+
delete[] str;
78+
str = newStr;
79+
}
80+
81+
void concatenate(arrayString &str1, arrayString str2)
82+
{
83+
int len1 = length(str1);
84+
int len2 = length(str2);
85+
int newLen = len1 + len2;
86+
arrayString newStr = new char[newLen+1];
87+
for (int i = 0; i < len1; i++)
88+
{
89+
newStr[i] = str1[i];
90+
}
91+
for (int i = len1; i < newLen; i++)
92+
{
93+
newStr[i] = str2[i - len1];
94+
}
95+
newStr[newLen] = 0;
96+
delete[] str1;
97+
str1 = newStr;
98+
}
99+
100+
int length(arrayString str)
101+
{
102+
int len = 0;
103+
while(str[len] != 0)
104+
{
105+
len++;
106+
}
107+
return len;
108+
}
109+

0 commit comments

Comments
 (0)