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

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:

/*The highscore file that needs to be edited*/

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<string>
#include <string.h>
using namespace std;

void initializeArrays(string names[], int scores[], int size);
void sortData(string names[], int scores[], int size);
void displayData(const string names[], const int scores[], int size);

int main()
{
   int size;
   cout << \"How many scores will you enter?: \";
   cin >> size;
   string *names = new string[size];
   int *scores = new int[size];
   initializeArrays(names, scores, size);
   sortData(names, scores, size);
   displayData(names, scores, size);
   system(\"pause\");
   return 0;
}
void initializeArrays(string names[], int scores[], int size)
{ // for initializing arrays.

   for (int i = 0; i<size; i++)
   {
       cout << \"Enter the name for score #\" << i + 1 << \" \";
       cin >> names[i];

       cout << \"Enter the score for score #\" << i + 1 << \" \";
       cin >> scores[i];
   }
}
void displayData(const string names[], const int scores[], int size)
{ // to display the contents of the arrays after sorting.
   cout << \"Top Scorers\ \";
   for (int i = 0; i<size; i++)
   {
       cout << names[i] << \": \" << scores[i] << \"\ \";
   }
}
void sortData(string names[], int scores[], int size) { //used bubble sorting to sort the arrays based on the scores.
   string temp;
   int t;
   for (int i = 0; i<size - 1; i++)
   {
       for (int j = 0; j<size - i - 1; j++)
       {
           if (scores[j]<scores[j + 1])
           {
               temp = names[j];
               names[j] = names[j + 1];
               names[j + 1] = temp;

               t = scores[j];
               scores[j] = scores[j + 1];
               scores[j + 1] = t;
           }
       }
   }

}

Solution

Assignment 1:

#include<iostream>
using namespace std;
void noNegatives(int * x);
void swap(int *x, int *y);
int main(){
int x,y;
int * p1;
p1=&x;
*p1=99;
cout<<\"value of x using x:\"<<x<<endl;
cout<<\"value of x using pointer p1:\"<<*p1<<endl;
p1=&y;
*p1=-300;
int temp;
int *p2;
p2=&x;
cout<<\"value of x and y before swaping :\"<<x<<\"and\"<<y<<endl;
temp=*p2;
*p2=*p1;
*p1=temp;
cout<<\"value of x and y after swaping using temp and pointer:\"<<x<<\"and\"<<y<<endl;
noNegatives(&x);
noNegatives(&y);
p2=&x;
cout<<\" x is\"<<*p2<<endl;
p2=&y;
cout<<\" y is \"<<y<<endl;
int a[2];
p2=&a[0];
*p2=x;
*(p2+1)=y;
cout<<\"address of first element in a is\"<<p2<<endl;
cout<<\"address of second element in a is\"<<p2+1<<endl;
p1=&a[0];
p2=&a[1];
temp=*p1;
*p1=*p2;
*p2=temp;
cout<<\"first elemet is :\"<<a[0]<<endl;
cout<<\"second element is::\"<<a[1]<<endl;
cout<<\"calling swap fun\"<<endl;
swap(&x,&y);
cout<<\"x is \" <<x<<endl;
cout<<\"y is \"<<y<<endl;
swap(&a[0],&a[1]);
cout<<\"value of a[0]\"<<a[0]<<endl;
cout<<\"value of a[1]\"<<a[1]<<endl;
}
void noNegatives(int *x)
{

if(*x<0){
*x=0;}
}
void swap(int *x, int *y)
{

int temp;
temp=*x;
*x=*y;
*y=temp;
}

========================

2nd Assignment

for the second assignment as the question is not clear regarding original High Scores assignment so I am providing the answer for dynamic allocation of array part.

#include<iostream>
using namespace std;
int main()
{

int size;
int i,j;
int p,q,r;
int *score;
char **string;
cout<<\"How many scores will you enter?\";
cin>>size;
score = new int[size];
string = new char*[size];
for( i=0;i<size;i++)
string[i]=new char[15];

for( j=0;j<size;j++){
cout<<\"Enter the name for score#\"<<j+1<<\":\";
cin>>string[j];

cout<<\"Enter the score for score#\"<<j+1<<\":\";
cin>>score[j];
}
}

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