Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use C++ This assignment will give you a chance to perform some simple tasks with

ID: 3753872 • Letter: U

Question

Use C++

This assignment will give you a chance to perform some simple tasks with pointers. The tasks are only loosely related to each other. Start the assignment by copying the code below and pasting it into a .cpp file, then add statements to accomplish each of the tasks listed. Some of the tasks will only require a single C++ statement, others will require more than one.

No documentation is required for this part of the assignment.

Your output should look exactly like this (the blanks should be replaced with the correct values):

Assignment 5.2 [20 points]

Write a program that records high-score data for a fictitious game. The program will ask the user to enter the number of scores, create two dynamic arrays sized accordingly, ask the user to enter the indicated number of names and scores, and then print the names and scores sorted by score in descending order.

The output from your program should look exactly like this (given the same input):

Additional Requirements

The data must be stored in two dynamic arrays: an array of strings named names, and an array of ints named scores. These arrays must be declared and allocated using "new" in the main function.

The user input of the names and scores should be done in a function named initializeArrays(). It should have the following signature:

You must also write two more functions: one to sort both arrays in descending order by score, and one to display the final list of names and scores. They should have the following signatures.

The main function should be very short. It should just get the number of scores, allocate the two arrays, and then invoke these three functions.

Some of you may not have studied sorting algorithms. Sorting is covered in lesson 9, section 9.6. You can copy as much code from that lesson as you like. Do not use C++'s sort() function.

Submit Your Work

Name your source code files according to the assignment numbers (a1_1.cpp, a4_2.cpp, etc.). Execute the programs and copy/paste the output that is produced by your programs into the bottom of the corresponding source code file, making it into a comment. Use the Assignment Submission link to submit the source files. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the programs

Explanation / Answer

//C++ Assignment
#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++ code


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


void initializeArrays(string names[], int scores[], int size)
{
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 sortData(string names[], int scores[], int size)
{
  
// sort by scores
for (int i = size; i >= 0; i--)
{
  
for (int i = 0; i < size; i++)
{
  
  
if(scores[i] < scores[i+1])
{
  
int tempscores = scores[i+1];
scores[i+1] = scores[i];
scores[i] = tempscores;
  
string tempnames = names[i+1];
names[i+1] = names[i];
names[i] = tempnames;
  
}
}
}
  
}


void displayData(const string names[], const int scores[], int size)
{
  
cout << " Top Scorers: " << endl;
  
  
for (int i = 0; i < size; i++)
{
  
cout << names[i] << ": " << scores[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 you enter? ";
cin >> *pointer;
const int size = *pointer;
  
//declaration of array's names and scores & dynamic memory allocation using new keyword
int *scores = 0;
scores = new int[size];
string *names = new string[*pointer];

//Entry point of function's-> invoke three function here
initializeArrays(names, scores, size);
sortData(names, scores, size);
displayData(names, scores, size);

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

/*
output:

How many scores 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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote