08 Jun Need help with C# assignment. Doc submitted
This project is designed to help you master pointers. To that end, you’ll get the most out of it by working through the problems by hand. Only after that should you resort to running the programs (and stepping through them with the debugger) to check your understanding. Remember, on the final exam you’ll have to be able to do problems like this by hand.
This “project” is more like a homework. There are five problems. In problems that ask you to change code, make the few changes necessary to fix the code without changing its overall approach. For example, don’t fix the program in problem 1a by changing it to
int main()
{
cout << “10 20 30” << endl;
}
1. The subparts to this problem involve errors in the use of pointers.
a. This program is supposed to write10 20 30, one per line. Find all of the bugs and show a fixed version of the program: int main()
b. {
c. int arr[3] = { 5, 10, 15 };
d. int* ptr = arr;
e.
f. *ptr = 10; // set arr[0] to 10
g. *ptr + 1 = 20; // set arr[1] to 20
h. ptr += 2;
i. ptr[0] = 30; // set arr[2] to 30
j.
k. while (ptr >= arr)
l. {
m. ptr–;
n. cout << *ptr *lt;< endl; // print values
o. }
p. }
q.
r. ThefindMax function is supposed to find the maximum item in an array and set thepToMax parameter to point to that item so that the caller knows its location. Explain why this function won’t do that, and show how to fix it. Your fix must be to the function only; you must not change the main routine below in any way, yet as a result of your fixing the function, the main routine below must work correctly. void findMax(int arr[], int n, int* pToMax)
s. {
t. if (n <= 0)
u. return; // no items, no maximum!
v.
w. pToMax = arr;
x.
y. for (int i = 1; i < n; i++)
z. {
aa. if (arr[i] > *pToMax)
bb. pToMax = arr + i;
cc. }
dd. }
ee.
ff. int main()
gg. {
hh. int nums[4] = { 5, 3, 15, 6 };
ii. int* ptr;
jj.
kk. findMax(nums, 4, ptr);
ll. cout << “The maximum is at address ” << ptr << endl;
mm. cout << “It’s at index ” << ptr – nums << endl;
nn. cout << “Its value is ” << *ptr << endl;
oo. }
pp.
qq. ThecomputeCube function is correct, but the main function has a problem. Explain why it may not work and show how to fix it. Your fix must be to the main function only; you must not changecomputeCube in any way. void computeCube(int n, int* ncubed)
rr. {
ss. *ncubed = n * n * n;
tt. }
uu.
vv. int main()
ww. {
xx. int* ptr;
yy. computeCube(5, ptr);
zz. cout << “Five cubed is ” << *ptr << endl;
aaa. }
bbb.
ccc. Thestrequal function is supposed to return true if and only if its two C string arguments have exactly same text. What are the problems with the implementation of the function, and how can they be fixed? // return true if two C strings are equal
ddd. bool strequal(const char str1[], const char str2[])
eee. {
fff. while (str1 != 0 && str2 != 0)
ggg. {
hhh. if (str1 != str2) // compare corresponding characters
iii. return false;
jjj. str1++; // advance to the next character
kkk. str2++;
lll. }
mmm. return str1 == str2; // both ended at same time?
nnn. }
ooo.
ppp. int main()
qqq. {
rrr. char a[10] = “Bryan”;
sss. char b[10] = “Bryant”;
ttt.
uuu. if (strequal(a,b))
vvv. cout << “They’re the same guy! “;
www. }
xxx.
yyy. This program is supposed to write5 4 3 2 1, but it probably does not. What is the problem with this program? (We’re not asking you to propose a fix to the problem.) int* getPtrToArray(int& m)
zzz. {
aaaa. int anArray[5] = { 5, 4, 3, 2, 1 };
bbbb. m = 5;
cccc. return anArray;
dddd. }
eeee.
ffff. void f()
gggg. {
hhhh. int junk[100];
iiii. for (int k = 0; k < 100; k++)
jjjj. junk[k] = 123400000 + k;
kkkk. }
llll.
mmmm. int main()
nnnn. {
oooo. int n;
pppp. int* ptr = getPtrToArray(n);
qqqq. f();
rrrr. for (int i = 0; i < n; i++)
ssss. cout << ptr[i] << ‘ ‘;
tttt. cout << endl;
uuuu. }
vvvv.
2. For each of the following parts, write a single C++ statement that performs the indicated task. For each part, assume that all previous statements have been executed (e.g., when doing part e, assume the statements you wrote for parts a through d have been executed).
a. Declare a pointer variable namedcat that can point to a variable of type double.
b. Declaremouse to be a 5-element array of doubles.
c. Make thecat variable point to the last element ofmouse.
d. Make the double pointed to bycat equal to 17, using the * operator.
e. Without using thecat pointer, and without using square brackets, set the fourth element (i.e., the one at index 3) of themouse array to have the value 42.
f. Move thecat pointer back by three doubles.
g. Using square brackets, but without using the namemouse, set the third element (i.e., the one at index 2) of themouse array to have the value 33.
h. Without using the* operator, but using square brackets, set the double pointed to bycat to have the value 25.
i. Using the* operator in the initialization expression, declare a bool variable namedb and initialize it to true if the double pointed to bycat is equal to the double immediately following the double pointed to bycat, and false otherwise.
j. Using the== operator in the initialization expression, declare a bool variable namedd and initialize it to true ifcat points to the double at the start of themouse array, and false otherwise.
3.
a. Rewrite the following function so that it returns the same result, but does not increment the variableptr. Your new program must not use any square brackets, but must use an integer variable to visit each double in the array. You may eliminate any unneeded variable. double computeMean(const double* scores, int numScores)
b. {
c. const double* ptr = scores;
d. double tot = 0;
e. while (ptr != scores + numScores)
f. {
g. tot += *ptr;
h. ptr++;
i. }
j. return tot/numScores;
k. }
l.
m. Rewrite the following function so that it does not use any square brackets (not even in the parameter declarations) but does use the integer variablek. // This function searches through str for the character chr.
n. // If the chr is found, it returns a pointer into str where
o. // the character was first found, otherwise NULL (not found).
p.
q. const char* findTheChar(const char str[], char chr)
r. {
s. for (int k = 0; str[k] != 0; k++)
t. if (str[k] == chr)
u. return &str[k];
v.
w. return NULL;
x. }
y.
z. Now rewrite the function shown in part b so that it uses neither square brackets nor any integer variables. Your new function must not use any local variables other than the parameters.
4. What does the following program print and why? Be sure to explain why each line of output prints the way it does to get full credit. #include <iostream>
5. using namespace std;
6.
7. int* maxwell(int* a, int* b)
8. {
9. if (*a > *b)
10. return a;
11. else
12. return b;
13. }
14.
15. void swap1(int* a, int* b)
16. {
17. int* temp = a;
18. a = b;
19. b = temp;
20. }
21.
22. void swap2(int* a, int* b)
23. {
24. int temp = *a;
25. *a = *b;
26. *b = temp;
27. }
28.
29. int main()
30. {
31. int array[6] = { 5, 3, 4, 17, 22, 19 };
32.
33. int* ptr = maxwell(array, &array[2]);
34. *ptr = -1;
35. ptr += 2;
36. ptr[1] = 9;
37. *(array+1) = 79;
38.
39. cout <<&array[5] – ptr << endl;
40.
41. swap1(&array[0], &array[1]);
42. swap2(array, &array[2]);
43.
44. for (int i = 0; i < 6; i++)
45. cout << array[i] << endl;
46. }
47.
48. Write a function namedremoveS that accepts one character pointer as a parameter and returns no value. The parameter is a C string. This function must remove all of the upper and lower case ‘s’ letters from the string. The resulting string must be a valid C string. Your function must declare no more than one local variable in addition to the parameter; that additional variable must be of a pointer type. Your function must not use any square brackets and must not use thestrcpy library function. int main()
49. {
50. char msg[50] = “She’ll be a massless princess.”;
51. removeS(msg);
52. cout << msg; // prints he’ll be a male prince.
53. }
54.
Prepare your solutions to these homework problems as a single Word document named hw.doc or hw.docx, or a plain text file named hw.txt. Put that file in a zip file, and follow theturn in Project 6 link to turn in the zip file.
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Writedemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
