Mathematics

1. Assume that the aIDress of b[0] is 5200 in memory. What output would be produced by the following program (if the output is an aIDress, you need to write out the exact aIDress. For example, the aIDress of b[1] is 5214)?
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int b[] = {3,4,2,5,6,7};
int *xPtr;
xPtr = &b[2];
cout << xPtr << endl;

cout << *xPtr << endl;

cout << &b[3] << endl;

cout << b[4] << endl;
*xPtr = 9;
cout << *xPtr << endl;
return 0;
}

2 What is the output of the following program? Explain how the function leftShift shown below works and why we need the variable temp? (Hint: if you don’t understand what the program do, please run it and print out what the value of temp when the function is called).

#include <iostream>
using namespace std;
void leftShift(int *);

int main()
{
int a[] = {3, 6, 7, 9, 4, 5, 2, 4, 2, 1};
for (int i = 1; i < 5; i++)
{
cout << “Step “ << i << “:n”;
leftShift(a);
}

return 0;
}
void leftShift(int *array)
{
int temp = *array;
for (int i = 0; i < 9; i++)
*(array+i) = *(array+i+1);
*(array+9) = temp;
for (int i = 0; i<10; i++)
cout << *(array+i) << ” “;
cout << endl;
}

3 Given the following definitions and assignments, what do these expressions evaluate to (assume each of the following problems is independent).
int a[] = {2, 4, 0, 3, 5, 9, 12, 6}, n;
int *aPtr = &a[3];

(a) n = *aPtr + 1; n = _____________

(b) n = *(aPtr + 2); n = _____________

(c) n = *(aPtr – 2); n = _____________

(d) n = *(++aPtr); n = _____________

(e) n = –(*aPtr); n =______________

(f) n = a[++(*aPtr )]; n =______________

4 Point out whether each of the following program segments is valid or invalid. If it is invalid, explain why. Assume the following declarations:
double a = 8.1, b = 2.5;
const double *aPtr = &a;
double * const bPtr = &b;

(a) *aPtr = &b;

(b) *aPtr = 6.5;

(c) aPtr = &b;

(d) aPtr = b;

(e) *bPtr = 3.4;

(f) bPtr = &a;

5 What is the output by the following program segment?
char x[ ] = “Today is a nice day ”;
char y[50], z[50];
strcpy(y, x);
cout << “The string x is: “ << x
<< “nThe string y is: “ << y << endl;
strncpy(z, x, 9);
z[9] = ‘?’;
cout << “The string z is: “ << z;
strcat(z, y);
cout << “nThe string z is: “ << z
<< “nThe length of z is: “ << strlen(z) << endl;

6 Given the class definition:
class Grade
{
public:
void setGrade(int r)
{
result = r;
}
int getGrade()
{
return result;
}
void printGrade()
{
cout << “Your result is” << result << endl;
}
private:
int result;
};
Write a main program to perform three tasks: (a) declare an object; (b) assign a value to the data member; (c) output the value.

Programming (hand in C++ code only)

7 Write a function that accepts a pointer to a string as an argument and returns the number of words contained in the string, that is, the function prototype is
int wordCount(char *);
If the string argument is “it is a nice day” the function should return 5. Demonstrate the function in a program that asks the user to input a string and then passes it to the function. (Hint: you need to use cin.getline() to receive the input string). The sample output is:
Enter a sentence: it is a nice day
The sentence has 5 words.
Don’t use strtok function. You can assume that the sentence has no empty spaces at the beginning and at the end.

8 Write a program to prompt user to enter a password. A valid password must have at least 6 characters and must consist of at least one lower character (ASCII code 97-122), one capital character (ASCII code 65-90) and one digital number (ASCII code 49-57). If the user enter a password shorter than 6 characters, or no capital or lower or special character, output the error message and let the user try again. Then let the user enter the password again to verify whether the user has remembered the password. If the two entered passwords are not the same, output error message and try again. If verification is OK, output “Your password is: password”. Use do-while, break and continue to make your code efficient.
Sample output:
Enter your password (e to exit): abcd
Password must be at least six characters! Try again.
Enter your password (e to exit): abcdef
Password must be one lower or capital or digital number! Try again
Enter your password (e to exit): Abcde7
Re-enter your password: abcde7
Two passwords are different. Try again.
Enter your password (e to exit): Abcde7
Re-enter your password: Abcde7
Your password is: Abcde7

9 Define a class called Point which consists two private data x and y with double type and three public member functions: a constructor Point(), void setPoint(double, double), void printPoint(). In the function setPoint, you need to verify if the values of x and y are located between 0 and100. If not set the value to be 0.0. In the printPoint(), print the following message:
(x, y) = (XXX.X, XX.X)
where X is a digit. In your main program, use keyboard to input the values of x and y (one time is OK). Then call setPoint() and printPoint().

&nbsp;

&nbsp;

"Is this question part of your assignment? We can help"

ORDER NOW