Question

C++, Will Upvote: A) Try to get the following programs to run in your environment. B)...

C++, Will Upvote:

A) Try to get the following programs to run in your environment.

B) If the programs will run, document each line of code with comments and describe any changes you had to make to the original code to get it to work. If the programs are unable to be modified to run with desirable results, explain why you think so.

C) Description your solution and a synopsis of how your code is intended to work and the tests that you created to prove that it works as intended.

//CODE A:

#include <iostream>

using namespace std;

int* reverse(const int* list, int size)
{
  int result[6];

  for (int i = 0, j = size - 1; i < size; i++, j--)
  {
    result[j] = list[i];
  }
  return result;
}

void printArray(const int* list, int size)
{
  for (int i = 0; i < size; i++)
    cout << list[i] << " ";
}

int main()
{
  int list[] = {1, 2, 3, 4, 5, 6};
  printArray(list, 6);
  cout << endl;
  int* p = reverse(list, 6);
  printArray(p, 6);
  cout << endl;

  return 0;
}

---------------------------------------------

//CODE B:

#include <iostream>
using namespace std;

int* reverse(const int* list, int size)
{
  int* result = new int[size];

  for (int i = 0, j = size - 1; i < size; i++, j--)
  {
    result[j] = list[i];
  }
  return result;
}

void printArray(const int* list, int size)
{
  for (int i = 0; i < size; i++)
    cout << list[i] << " ";
}

int main()
{
  int list[] = {1, 2, 3, 4, 5, 6};
  printArray(list, 6);
  cout << endl;
  int* p = reverse(list, 6);
  printArray(p, 6);
  cout << endl;

  return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

int* reverse(const int* list, int size)
{
static int result[6]; // issues is with this as we are returning the address of this variale we need to make it as
// static because we can not return the address of local variables because they
// will get destroyed once this function is completed

// looping through array to reverse the array
for (int i = 0, j = size - 1; i < size; i++, j--)
{
result[i] = list[j];
}
// returning the reverse of the array
return result;
}
// prints the array
void printArray(int* list, int size)
{
// loop thorugh the array and print
for (int i = 0; i < size; i++)
cout << list[i] << " ";
}


int main()
{
// creating int array with elements 6
int list[] = {1, 2, 3, 4, 5, 6};
// calling printArray to print elements
printArray(list, 6);
// newline printing
cout << endl;
// calling reverse() to reverse the array
int * p = reverse(list, 6);
// again printing the array
printArray(p, 6);
cout << endl;

return 0;
}

27 int main() 28 // creating int array with elements 6 30 int list[] 1, 2, 3, 4, 5, 61; 31 // calling printArray to print elements 32 printArray(list, 6); 33 / newline printing 34 cout <<endl; 35 // calling reverse) to reverse the array int * p reverse(list, 6 37 / again printing the array 38 printArray (p, 6); 39 40 41 return 0 cout << endl; 1 2 3 4 5 6 6 5 4 3 2 1 Program finished with exit code 0 s ENTER to exit console

#include <iostream>
using namespace std;

int* reverse(const int* list, int size)
{
// here we are creating the array dynamically so that we can return the address
// because if we create arrays dynamically they will not destroy until program ends
int* result = new int[size];

for (int i = 0, j = size - 1; i < size; i++, j--)
{
result[j] = list[i];
}
return result;
}
// prints the array
void printArray(int* list, int size)
{
// loop thorugh the array and print
for (int i = 0; i < size; i++)
cout << list[i] << " ";
}


int main()
{
// creating int array with elements 6
int list[] = {1, 2, 3, 4, 5, 6};
// calling printArray to print elements
printArray(list, 6);
// newline printing
cout << endl;
// calling reverse() to reverse the array
int * p = reverse(list, 6);
// again printing the array
printArray(p, 6);
cout << endl;

return 0;
}

This solution to reverse the given array elements into the new array and return from the function

Add a comment
Know the answer?
Add Answer to:
C++, Will Upvote: A) Try to get the following programs to run in your environment. B)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • 9. At your job, you are creating a library. A co-worker brought this test code to...

    9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final practice exam. //Please fill in the functions at the bottom of...

    //This program is your final practice exam. //Please fill in the functions at the bottom of the file. (sum and removeItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream>...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT