Write a complete C++ program that defines and initializes a single 10-element in
ID: 3545478 • Letter: W
Question
Write a complete C++ program that defines and initializes a single 10-element integer array (use random values of your choice to fill the array). Use standard notation (array name with subscripts) to print the array in two columns, one in order from top to bottom, the other in reverse order. The columns must be next to each other in the display. Next, print the array in the same fashion using pointers with offset notation. Direct your output to a text file. Submit your code listing with your output text file. You do not need to use any functions for this program.Write a complete C++ program that defines and initializes a single 10-element integer array (use random values of your choice to fill the array). Use standard notation (array name with subscripts) to print the array in two columns, one in order from top to bottom, the other in reverse order. The columns must be next to each other in the display. Next, print the array in the same fashion using pointers with offset notation. Direct your output to a text file. Submit your code listing with your output text file. You do not need to use any functions for this program.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
int number[10] = {11, 8, 7, 5, 19, 24, 1, 2, 9, 10};
int i;
int* p;
ofstream file("output.txt");
// print in two columns
for (i = 0; i < 10; i++)
{
cout << number[i] << " " << number[9-i] << endl;
file << number[i] << " " << number[9-i] << endl;
}
cout << endl;
file << endl;
// Using pointers with offset notation
p = number;
for (i = 0; i < 10; i++)
{
cout << *(p+i);
cout << " ";
cout << *(p+9-i) << endl;
file << *(p+i);
file << " ";
file << *(p+9-i) << endl;
}
file.close();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.