hi there I have a hard time doing this assignment can you pls help me with a det
ID: 3593193 • Letter: H
Question
hi there
I have a hard time doing this assignment can you pls help me with a detailed program?
thank you
1. Pointer Tasks
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 file named pointerTasks.cpp 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.
Create two integer variables named x and y
Create an int pointer named p1
Store the address of x in p1
Use p1 to set the value of x to 99
Using cout and x, display the value of x
Using cout and the pointer p1, display the value of x
Store the address of y into p1
Use p1 to set the value of y to -300
Create two new variables: an int named temp, and an int pointer named p2
Use temp, p1, and p2 to swap the values in x and y (this will take a few statements)
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 p2 to display the values in x and y (this will require both assignment statements and cout statements)
Create an int array with two elements. The array should be named ‘a’
Use p2 to initialize the first element of a with the value of x
Use p2 to initialize the second element of a with the value of y
Using cout, display the address of the first element in a
Using cout 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]. After this the swapping steps should look very similar to step 10.)
Display the values of the two elements. (The first element should be 99, the second 0).
Write a function named ‘swap’ that accepts two integer pointers as arguments, and then swaps the two integers that the pointers point to. This function must be pass by pointer, i.e. int *, not pass by reference, i.e. int &.
Invoke your swap function with the addresses of x and y, 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)
2. The League with DMA
Rewrite your League program from Assignment 5 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays.
This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following:
void initializeData(string names[], int wins[], int size)
void sort(string names[], int wins[], int size)
void display(string names[], int wins[], int size)
Your modified league program should start out by asking the user how many teams will be entered. It should then dynamically allocate two appropriate arrays, and proceed just like the original League assignment, calling the above three functions. When your program is done using the arrays, don't forget to use delete [] to return their memory to the system.
Note: you must use dynamic memory allocation for your arrays, i.e. the new and delete [] operators, to receive credit.
The output from your program should look approximately like this (user input in orange bold):
How many teams will you enter?: 4
Enter team #1: Padres
Enter the wins for team #1: 75
Enter team #2: Dodgers
Enter the wins for team #2: 91
Enter team #3: Giants
Enter the wins for team #3: 92
Enter team #4: Cubs
Enter the wins for team #4: 65
League Standings:
Giants: 92
Dodgers: 91
Padres: 75
Cubs: 65
Explanation / Answer
#include <iostream>
#include <string>
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);
const int SIZE = 5;
// This function calls the functions and initializes the size of the arrays.
int main(){
string names[SIZE];
int scores[SIZE];
initializeArrays(names, scores, SIZE);
sortData(names, scores, SIZE);
cout << endl;
displayData(names, scores, SIZE);
}
// This function takes in the names and scores and puts them in seperate arrays.
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];
}
}
// This funciton uses bubble sort to sort the scores and keeps track of the names corresponding to the score.
void sortData(string names[], int scores[], int size){
int tempScore, j;
string tempName;
for(size = 4; size > 0; size--){
for(int i = 0; i < size; i++) {
j = i+1;
if(scores[i] > scores[j]) {
tempScore = scores[i];
tempName = names[i];
scores[i] = scores[j];
names[i] = names[j];
scores[j] = tempScore;
names[j] = tempName;
}
}
}
}
// This function display the scores with the names in a list of top scorers.
void displayData(const string names[], const int scores[], int size){
cout << "Top scorers:" << endl;
for(int count = size - 1; count >= 0; count--)
cout << names[count] << " : " << scores[count] << endl;
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.