c++ help with some practice problems. 1 - Look at the following code. double val
ID: 3831957 • Letter: C
Question
c++ help with some practice problems.
1 - Look at the following code.
double value = 29.7;
double *ptr = &value;
Write a cout statement that uses the ptr variable to display the contents of the value variable.
2 - Write code that dynamically allocates an array of 20 integers, then uses a loop to allow the user to enter values for each element of the array.
3 - Look at the following function definition.
void getNumber(int &n){
cout << "Enter a number: ";
cin >> n;
}// end getNumber
In this function, the parameter n is a reference variable. Rewrite the function so that n is a pointer.
4 - Each of the following denitions and program segments has errors. Rewrite them without errors.
a) int x, *ptr;
&x = ptr;
b) int x, *ptr;
*ptr = &x;
c) int x, *ptr;
ptr = &x;
ptr = 100; // Store 100 in x
cout << x << endl;
d) int numbers[] = {10, 20, 30, 40, 50};
cout << "The third element in the array is ";
cout << *numbers + 3 << endl;
e) int values[20], *iptr;
iptr = values;
iptr *= 2;
f) float level;
int fptr = &level;
5 - The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int pointerFun(int &x, int &y)
{
int temp = x;
x = y * 10;
y = temp * 10;
return x + y;
}
6 - Write a program that dynamically allocates an array large enough to hold a user-dened number of test scores (max size of 10 is sufficient for testing). Once all the scores (scores should be randomly generated numbers from 1 to 20) are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. You should have a function that displays the contents of the array. The program should display the array before it is sorted, sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation. Use the bubble sort to sort your array in ascending order.
Explanation / Answer
1.
#include <iostream>
using namespace std;
int main()
{
double value = 29.7;
double *ptr = &value;
cout << *ptr << endl;
return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
int *arr = new int [20];
for(int i = 0; i < 20; i++)
{
cout << "Enter entry "<< (i+1) << ": ";
cin >> arr[i];
}
delete [] arr;
return 0;
}
3.
#include <iostream>
using namespace std;
void getNumber(int *n){
cout << "Enter a number: ";
cin >> *n;
}// end getNumber
int main()
{
int n;
getNumber(&n);
cout << n << endl;
return 0;
}
4
a. int x, *ptr;
x = *ptr;
.
b.
int x, *ptr;
ptr = &x;
c.
int x, *ptr;
ptr = &x;
*ptr = 100; // Store 100 in x
cout << x << endl;
d.
int numbers[] = {10, 20, 30, 40, 50};
cout << "The third element in the array is ";
cout << *(numbers + 3) << endl;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.