This assignment will give you a chance to perform some simpl

This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. Start the assignment by creating a .cpp file with an empty main function, then add statements to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. Make sure that the tasks appear in your code in the same order that they are listed here. No documentation is required for this part of the assignment. Create two integer variables named x and y Create an int pointer named p1 Store the address of x in p1 Use only p1 (not x) to set the value of x to 99 Using cout and x (not p1), display the value of x Using cout and the pointer p1 (not x), display the value of x Store the address of y into p1 Use only p1 (not y) to set the value of y to -300 Create two new variables: an int named temp, and an int pointer named p2. Make p2 point to x. Use only temp, p1, and p2 (not x or y) to swap the values in x and y (this will take a few statements. Don\'t use a swap function) Write a function with the following signature: void noNegatives(int *x). The function should accept the address of an int variable. If the value of this integer is negative then it should set it to zero Invoke the function twice: once with the address of x as the argument, and once with the address of y. Use x or y for the argument (not p1 or p2) Use p2 to display the values in x and y (this will require both assignment statements and cout statements). You can use x and y in assignment statements, but not in your cout statement. this should produce the output x is: 0 y is: 99 Create an int array named \'a\' with two elements. Make p2 point to the first element of a. Use only p2 and x (not a) to initialize the first element of a with the value of x. Use only p2 and y (not a) to initialize the second element of a with the value of y Using cout and p2 only, display the address of the first element in a Using cout and p2 only, display the address of the second element in a Use p1, p2, and temp to swap the values in the two elements of array \'a\'. (first point p1 at a[0], then point p2 at a[1], then do not use \"a\" again. After this the swapping steps should look very similar to step 10. Don\'t use a swap function.) Display the values of the two elements. (The first element should be 99, the second 0). Write a function named \'swap\' that accepts two pointers to integers as arguments, and then swaps the contents of the two integers. Do not use any reference parameters. Invoke your swap function with the addresses of x and y (using the address-of operator), then print their values. (x should be 99, y should be 0). Invoke your swap function with the address of the two elements in array \'a\', then print their values. (a[0] should be 0, a[1] should be 99) Assignment 6.2 [20 points] Rewrite your High Scores program so that it uses Dynamic Memory Allocation to create the names and scores arrays. You will only need to make slight modifications to your main function if you wrote your original program correctly using the function signatures required for that assignment. Your function signatures for this assignment should be exactly the same as the signatures required in the original High Scores assignment. Your modified high scores program should start out by asking the user how many scores will be entered. It should allocate appropriate arrays, and then proceed just like the original High Scores assignment. The output from your program should look approximately like this: How many scores will you enter?: 4 Enter the name for score #1: Suzy Enter the score for score #1: 9900 Enter the name for score #2: Kim Enter the score for score #2: 1000000 Enter the name for score #3: Armando Enter the score for score #3: 822 Enter the name for score #4: Tim Enter the score for score #4: 514 Top Scorers: Kim: 1000000 Suzy: 9900 Armando: 822

Solution

//C++ Assignment 1
#include <iostream>
using namespace std;
  
//Write a function with the following signature: void noNegatives(int *x).
//The function should accept the address of an int variable.
//If the value of this integer is negative then it should set it to zero
void noNegatives(int *x)
{
if(*x < 0)
*x = 0;
}

//Write a function named \'swap\' that accepts two pointers to integers as arguments, and then swaps the contents of the two integers.
//Do not use any reference parameters.
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}

int main()
{
//Create two integer variables named x and y
int x, y;
//Create an int pointer named p1
int *p1;
//Store the address of x in p1
p1 = &x;
//Use only p1 (not x) to set the value of x to 99
*p1 = 99;
//Using cout and x (not p1), display the value of x
cout<<\"x is: \"<<x<<endl;
//Using cout and the pointer p1 (not x), display the value of x
cout<<\"*p1 is: \"<<*p1<<endl;
//Store the address of y into p1
p1 = &y;
//Use only p1 (not y) to set the value of y to -300
*p1 = -300;
//Create two new variables: an int named temp, and an int pointer named p2.
int temp, *p2;
//Make p2 point to x.
p2 = &x;
//Use only temp, p1, and p2 (not x or y) to swap the values in x and y (this will take a few statements. Don\'t use a swap function)
temp = *p1;
*p1 = *p2;
*p2 = temp;
//Invoke the function twice: once with the address of x as the argument, and once with the address of y.
//Use x or y for the argument (not p1 or p2)
noNegatives(&x);
noNegatives(&y);
//Use p2 to display the values in x and y (this will require both assignment statements and cout statements).
//You can use x and y in assignment statements, but not in your cout statement. this should produce the output
p2 = &x;
cout<<\"x = \"<<*p2<<endl;
p2 = &y;
cout<<\"y = \"<<*p2<<endl;
  
//Create an int array named \'a\' with two elements.
int a[2];
//Make p2 point to the first element of a.
p2 = a;
//Use only p2 and x (not a) to initialize the first element of a with the value of x.
*p2 = x;
//Use only p2 and y (not a) to initialize the second element of a with the value of y
*(p2+1) = y;
//Using cout and p2 only, display the address of the first element in a
cout<<\"Address of first element in a is: \"<<p2<<endl;
//Using cout and p2 only, display the address of the second element in a
cout<<\"Address of second element in a is: \"<<p2+1<<endl;
//Use p1, p2, and temp to swap the values in the two elements of array \'a\'.
//(first point p1 at a[0], then point p2 at a[1], then do not use \"a\" again.
//After this the swapping steps should look very similar to step 10. Don\'t use a swap function.)
p1 = a;
p2 = a+1;
temp = *p1;
*p1 = *p2;
*p2 = temp;
//Display the values of the two elements. (The first element should be 99, the second 0).
cout<<\"a[0] = \"<<a[0]<<endl;
cout<<\"a[1] = \"<<a[1]<<endl;
//Invoke your swap function with the addresses of x and y (using the address-of operator),
swap(&x, &y);
//then print their values. (x should be 99, y should be 0).
cout<<\"x = \"<<x<<endl;
cout<<\"y = \"<<y<<endl;
//Invoke your swap function with the address of the two elements in array \'a\',
swap(a, a+1);
//then print their values. (a[0] should be 0, a[1] should be 99)
cout<<\"a[0] = \"<<a[0]<<endl;
cout<<\"a[1] = \"<<a[1]<<endl;

return 0;
}

/*
output:

x is: 99
*p1 is: 99
x = 0
y = 99
Address of first element in a is: 0x7ffe2ad08710
Address of second element in a is: 0x7ffe2ad08714
a[0] = 99
a[1] = 0
x = 99
y = 0
a[0] = 0
a[1] = 99
*/

//C++ Assignment 2
#include <iostream>
using namespace std;
  


void assignArrays(string peopleName[], int givenscores[], int size)
{
for (int i = 0; i < size ; i++)
{
  
cout << \"Enter the name for score #\"<< (i+1) << \": \";
cin >> peopleName[i];
cout << \"Enter the score for score #\"<< (i+1) << \": \";
cin >> givenscores[i];
  
}
  
}


void sort(string peopleName[], int givenscores[], int size)
{
  
// sort by givenscores
for (int i = size; i >= 0; i--)
{
  
for (int i = 0; i < size; i++)
{
  
  
if(givenscores[i] < givenscores[i+1])
{
  
int tempgivenScores = givenscores[i+1];
givenscores[i+1] = givenscores[i];
givenscores[i] = tempgivenScores;
  
string temppeopleName = peopleName[i+1];
peopleName[i+1] = peopleName[i];
peopleName[i] = temppeopleName;
  
}
}
}
  
}


void printData(const string peopleName[], const int givenscores[], int size)
{
  
cout << \"\ Top Scorers: \" << endl;
  
  
for (int i = 0; i < size; i++)
{
  
cout << peopleName[i] << \": \" << givenscores[i] << endl;
  
}
  
}


int main()
{
  
int *pointer = NULL;
  
//memory dynamic allocation
pointer = new int;
  
//size of array is dynamic and is entered by user at console
cout << \"How many scores will be entered? \";
cin >> *pointer;
const int size = *pointer;
  
//declaration of array\'s peopleName and givenscores & dynamic memory allocation using new keyword
int *givenscores = 0;
givenscores = new int[size];
string *peopleName = new string[*pointer];

//Entry point of function\'s-> invoke three function here
assignArrays(peopleName, givenscores, size);
sort(peopleName, givenscores, size);
printData(peopleName, givenscores, size);

//memory freed or deallocated using delete keyword
delete [] givenscores;
  
//pointer changed to \'0\'
givenscores = 0;
}

/*
output:

How many givenscores will be entered? 4
Enter the name for score #1: Suzy
Enter the score for score #1: 9900
Enter the name for score #2: Kim
Enter the score for score #2: 1000000
Enter the name for score #3: Armado
Enter the score for score #3: 822
Enter the name for score #4: Tim
Enter the score for score #4: 514

Top Scorers:
Kim: 1000000
Suzy: 9900
Armado: 822
Tim: 514

*/

This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related
This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related
This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related
This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related
This assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site