S24 PF AFour Part One

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Programming Fundamentals

Session: Spring 2024


COPY WILL RESULT IN ZERO MARKS FOR ALL INVOLVED
EVEN A COPY OF A SINGLE CODE RESULTS IN ZERO MARKS IN
THE WHOLE ASSIGNMENT
CHATGPT USE RESULTS IN ZERO MARKS
NOT OWN WORK ZERO MARKS (CAN’T EXPLAIN)
THIS FIRST PART IS DUE HANDWRITTEN ONLY

Assignment no Three Part One: DRY RUN & Functions


Due HANDWRITTEN IN CLASS ONLY
Deadline: Mon June 24, 2024
Marks of EACH CODE are Shown
MARKS ARE LISTED for EACH CODE
YOU GET AS MANY MARKS AS MANY DRY RUNS & FUNCTIONS YOU
SUBMIT BY THE DEADLINE
THEREFORE NO EXTENSION WILL BE GIVEN IN THE DEADLINE
Functions, Pointers, 2D Arrays and 2D Dynamic arrays (DRY RUN OF ALL)
Topic

Objective/ • Using Pointers as Variables and as the Head of Dynamic Arrays


Outcome • 2D Arrays and Double Pointers
• Solving Complex Coding Problems
• DRY RUN
SET ONE

Problem statement:
In this Set One, you will dry run the following Pieces of Code

Identify any errors and correct these error


Code One [10 marks]
1. int main()
2. {
3. int * p = nullptr;
4. int amount = 0;
5. /*
6. asssume file values.txt has the following values:
7. 1.1 2.2 3.3 4.4 5.5
8. */
9. ifstream R("values.txt");
10. int v = 0;
11.
12. while(R >> v)
13. {
14. int * temp = new int[amount + 1];
15. for(int i = 0; i < amount; i++)
16. {
17. temp[i] = p[i];
18. }
19. p = temp;
20. amount ++;
21. p[amount] = v;
22. }
23. R.close();
24. return 0;
25. }
Code Two [15 marks]
1. void copy(int * src,int * dst, int n)
2. {
3. for(int i = 0; i < n; i++)
4. {
5. dst[i] = src[i];
6. }
7. }
8. int * grow(int * array, int & n, int inc)
9. {
10. int * result = new int[n + inc];
11. copy(array, result, n);
12. delete [] array;
13. n += inc;
14. return result;
15. }
16.
17. int main()
18. {
19. int * p = nullptr;
20. int amount = 0;
21. /*
22. asssume file values.txt has the following values:
23. 1.1 2.2 3.3 4.4 5.5
24. */
25. ifstream R("values.txt");
26. int v = 0;
27.
28. while(R >> v)
29. {
30. P = grow(p, amount, 1);
31. p[amount] = v;
32. }
33. R.close();
34. return 0;
35. }
Code Three [25 marks]
1. int length(char * str)
2. {
3. int len = 0;
4. while(str[len++] != '\0');
5. return len;
6. }
7. void copy(char * src, char * dst)
8. {
9. int n = length(src);
10. for(int i = 0; i < n; i++)
11. {
12. dst[i] = src[i];
13. }
14. }
15. void append(char * src, char * s)
16. {
17. int ls = length(s);
18. int lsrc = length(src);
19. for(int i = lsrc; i < ls + lsrc; i++)
20. {
21. src[i] = s[lsrc - i];
22. }
23. }
24.
25. char * grow(char * array, int & n, int inc)
26. {
27. char * result = new char[n + inc];
28. copy(array, result);
29. delete [] array;
30. n += inc;
31. return result;
32. }
33.
34. int main()
35. {
36. char * words = nullptr;
37. int wc = 0;
38. /*
39. asssume file words.txt has the following values:
40. This that there accommodations fundamentals
41. */
42. ifstream R("words.txt");
43. char w[30] = {'\0'};
44.
45. while(R >> w)
46. {
47. int len = length(w);
48. words = grow(words, wc, len);
49. append(words, w);
50. wc++;
51. }
52. R.close();
53. return 0;
54. }
Code Four [5 marks]
1. int * sp = new int[2];
2. sp[0] = sp[1] = 5;
3. int ** dp = new int *[2];
4. dp[0] = new int [3];
5. dp[1] = new int [3];
6. dp[0][0] = dp[0][1] = dp[0][2] = 5;
7. dp[1][0] = dp[1][1] = dp[1][2] = 10;

Code Five [40 marks]


1. int ** read(ifstream &, int, int);
2. void read(ifstream &, int *, int);
3. void allocate(int **, int m, int n);
4. int * allocate(int n);
5. void display(int *, int);
6. void display(int **, int, int);
7.
8. int main()
9. {
10. int ** matrix = nullptr;
11. int noOfRows = 0;
12. int noOfColumns = 0;
13. /*
14. asssume file matrix.txt has the following values:
15. 34
16. 11 12 13 14
17. 21 22 23 24
18. 31 32 33 34
19. */
20. ifstream R("matrix.txt");
21. int v = 0;
22. R >> noOfRows >> noOfColumns;
23. matrix = read(R, noOfRows, noOfColumns);
24. R.close();
25. display(matrix, noOfRows, noOfColumns);
26. return 0;
27. }
28. int ** read(ifstream & rd, int rn, int cn)
29. {
30. int ** array2D = nullptr;
31. allocate(array2D, rn, cn);
32. for(int i = 0; i < rn; i++)
33. {
34. read(rd, array2D[i], cn);
35. }
36. return array2D;
37. }
38. void allocate(int **array2D, int m, int n)
39. {
40. array2D = new int * [m];
41. for(int i = 0; i < m; i++)
42. {
43. array2D[i] = allocate(n);
44. }
45. }
46. int * allocate(int n)
47. {
48. int * result = new int[n];
49. return result;
50. }
51. void display(int * array, int n)
52. {
53. for(int i = 0; i < n; i++)
54. {
55. cout << array[i] << " ";
56. }
57. }
58. void display(int **array2D, int m, int n)
59. {
60. for(int i = 0; i < m; i++)
61. {
62. display(array2D[i], n);
63. }
64. cout << endl;
65. }
66.
67. void read(ifstream & rd, int * array, int n)
68. {
69. for(int i = 0; i < n; i++)
70. {
71. rd >> array[i];
72. }
73. }
SET TWO

Problem statement:
In this Set One, you will write the code of ALL functions used in the main (Function
prototypes are given)

Marks for each function are shown in the comments.

Use of any of the following is zero marks in the whole Set TWO

• Use of Data Type string


• Use of any of CSTring Library Functions e.g. strcpy, strlen, strcmp, strcat etc.
• Use of getLine in any form
• Use of getc, getch and any other single character reading functions
• Reading a CString Character by Character
• Display a CString Character by Character

CODE ONE [70 MARKS]


Must use the given prototypes and the main
Identify any errors and correct these errors will get you 10 marks
If Your Code after changes works correctly = 10 marks
#include <iostream>
#include <fstream>
using namespace std;

char * copy(char * &); // 5 marks


void copy(char ** &, char ** &, int); // 5 marks
int length(const char * &); // 5 marks
void read(ifstream &, char ** &, int); // 10 marks
void allocate(char * &, int); // 5 marks
char ** allocate(int, int); // 5 marks
void display(char ** &, int); // 5 marks
char ** grow(char ** &, int, int, int); // 10 marks

int main()
{
char ** words = nullptr;
int noOfWords = 0;
/*
asssume file "words.txt" has the following words:

The government has proposed a series of punitive measures in the


annual budget for FY2024-25 to punish individuals and businesses
who do not file income tax returns, including the possibility of
being barred from foreign travel.
*/
ifstream R("words.txt");
read(R, words, noOfWords);
R.close();
display(words, noOfWords);
return 0;
}

CODE TWO [95 Marks]


Must use the given prototypes, function read and the main
Identify any errors and correct these errors will get you 10 marks
If Your Code after changes works correctly = 10 marks
void initialize(char * s, char c); // 5 marks
void addRows(int **, int &, int, int); // 10 marks
void shrink(char *); // 5 marks
bool isFound(char *, char, int); // 5 marks
void removeChar(char *, char); // 10 marks
int pow(int, int); // 5 marks
int cstring2int(char *); // 10 marks
void addColumns(int *, int, int); // 5 marks
int ** read(ifstream, int, int);
void display(int **, int rn, int cn); // 5 marks
int length(const char *);
void copy(int *, int *, int); // 5 marks
void copy(int **, int **, int, int); // 5 marks
void delete2D(int **, int, int);
char * copy(char *);
int getDigit(char); // 5 marks

int main()
{
int ** matrix = nullptr;
int noOfRows = 0;
int noOfColumns = 0;
/*
asssume file "matrix.txt" has the following values:

11 12 13 14 15,
21 22 23 24 25,
31 32 33 34 35,
41 42 43 44 45,

*/
ifstream R("matrix.txt");
matrix = read(R, noOfRows, noOfColumns);
R.close();
display(matrix, noOfRows, noOfColumns);
return 0;
}
int ** read(ifstream rd, int rn, int cn)
{
int ** array2D = nullptr;
char * str = new char[10];
initialize(str, '\0');
int m = 0;
int n = 0;
while(rd >> str)
{
if(m == 0 && n == 0)
{
addRows(array2D, rn, 0, 1);
}
shrink(str);
int ind = 0;
bool found = isFound(str, '.', ind);
if(found)
{
removeChar(str, '.');
}
int value = cstring2int(str);
addColumns(array2D[m], cn, 1);
array2D[m][n] = value;
n++;
if(found)
{
addRows(array2D, rn, cn, 1);
m++;
}
}
}

You might also like