Hello! I wrote a program to do what I thought was correct. However, I was informed that I actually didn't use pointers at all. I was told I accomplished the correct output screen using globals. I guess I don't understand how pointers work?? Any advice is greatly appreciated.
*****THIS IS WHAT IS SUPPOSED TO HAPPEN*****
Write a program to move and sort data with functions using pointers. You will have to define and assign array A with {2,4,6,8,10} and define and assign array B with {0,0,0,0,0}. The program should transfer the data from A to B. Then, sort the data in array A so that it is organized from largest to smallest (as opposed to smallest to largest like it is now). All array data transfer between functions must be via pointers.
The User Screen should display:
• The purpose of the program.
• The original content of each of the array locations for arrays A and B.
• The new contents of each of the array locations for array A.
• The new contents of each of the array locations for array B.
Hint: Break the program into small sections. Try to display the original array A first. If you are successful, try to display the original array B. (I would create a function to do this, that way it is only one set of code.) Then develop the function to transfer the data from A to B. Then develop a function to sort the data in array A. Finally display the finished product. *******************************************************************************
*********CODE*************
//**************************************
// This program will use pointers to
// transfer data between two arrays
// then sort the data from high to low.
//**************************************
#include <iostream>
using namespace std;
int a[5] = {2, 4, 6, 8, 10};
int b[5] = {0, 0, 0, 0, 0};
//initialize & declare arrays
int i;
int n;
void swap(int *p1, int *p2);
void BothArrays(void);
void sort(int n);
void swap2(int *p3, int *p4);
//Functions for sorting & transferring data
int main()
{
cout << "***************************************************************\n\n";
cout << "This program written by me." <<endl;
cout << "This program will use pointers to transfer data between\n";
cout << "two arrays. It will then sort the data from high to low.\n\n";
cout << "This program will self destruct.\n\n";
cout << "***************************************************************\n\n";
//Output text
BothArrays();
// Call Array A and B function
swap2(&a[5], &a[5]);
for (i = 0; i < 5; i++){
cout << a[i] << ", " ;}
// Call swap2 function, this transfers
// A content into B
sort(5);
for (i = 0; i < 5; i++)
cout << a[i] << ", ";
// Call sort function, this sorts high
// to low range.
return 0;
}
void swap2(int *p3, int *p4){
cout <<"\nSwapped Array 'A' content into Array 'B'.\n";
int temp = *p3;
*p3 = *p4;
*p4 = temp;
// I'm putting content from
// A into B then calling it
// up top.
}
void BothArrays(void){
int a[5] = {2, 4, 6, 8, 10};
cout << "Original Array 'A' content:\n";
for (i = 0; i < 5; i++){
cout << a[i] <<", ";}
// Displays A content
int b[5] = {0, 0, 0, 0, 0};
cout << "\nOriginal Array 'B' content:\n";
for (i = 0; i < 5; i++){
cout << b[i] <<", ";}
// Displays B content
}
void sort(int n)
{
cout <<"\nSort content from High to Low range:\n";
int i, x, high;
for (i = 0; i < n + 1; i++){
high = i;
for (x = i + 1; x < n; x++)
if (a[x] > a[high])
high = x;
if (i != high)
swap(&a[i], &a[high]);
// Not 100% sure what is going
// on here. To my understanding, this is
// comparing values in the array.
// Followed example in book
}
}
void swap(int *p1, int *p2){
int temp = *p1;
*p1 = *p2;
*p2 = temp;
// Bread and butter of
// program. Pointer function.
}
Thank you for the suggestion. I'm going to have to mess with it a little bit. This was my first post, but I think I might know what <> means. Did a screen capture of output file. won't let me paste it in this box.
You are using pointers successfully in your swap functions but not in the sort() and botharrays()
Since I don't exactly want to rewrite your program I'd suggest that you declare all variables in their respective functions instead of globally. this would prevent you from cheating yourself and teach you how to properly use pointers throughout the program.
this means
1 2 3 4 5
int a[5] = {2, 4, 6, 8, 10};
int b[5] = {0, 0, 0, 0, 0};
//initialize & declare arrays
int i;
int n;