Question
C++ OUTPUT MUST BE EXACT Assignment 3.1 [40 points] This assignment will give you a chance to perform some simple tasks with pointers. The instructions
C++
OUTPUT MUST BE EXACT
Assignment 3.1 [40 points]
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. Leave p2 pointing to the first element of a. Don't use pointer arithmetic. Hint: don't forget that pointers and arrays are the same thing.
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. Leave p2 pointing to the first element of a. Don't use pointer arithmetic.
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 3.2 [45 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, and then ask the user to enter the indicated number of names and scores.
The output from your program should look exactly like this (given the same input):
How many scores will you enter?: 4 Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score #2: Kim Enter the score for score #2: 9900 Enter the name for score #3: Armando Enter the score for score #3: 8000 Enter the name for score #4: Tim Enter the score for score #4: 514 Top Scorers: Kim: 9900 Armando: 8000 Suzy: 600 Tim: 514
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:
void initializeArrays(string names[], int scores[], int size)
You must also write two more functions: one to sort both arrays in descending order, and one to display the final list of names and scores. They should have the following signatures.
void sortData(string names[], int scores[], int size) void displayData(const string names[], const int scores[], int size)
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started