****PLEASE DOCUMENT THIS COMPLETED CODE**** ... MAKE SURE ITS DOUCUMENTED WITHIN
ID: 3548496 • Letter: #
Question
****PLEASE DOCUMENT THIS COMPLETED CODE**** ... MAKE SURE ITS DOUCUMENTED WITHIN THE CODE!!
#include<iostream>
using namespace std;
const int size = 10;
struct node
{
int data;
node *next;
char ch;
};
int main()
{
node *ptr;
int *p;
int i, j, *first;
cout << " enter size of new array: "; // enter number for array size
cin >> j;
ptr = new node[j];
p = new int[j];
first = p;
for ( i=0; i<j;i++)
{
cout << " enter data: ";
cin >> *p;
cout << " enter character: ";
cin >> ptr[i].ch;
ptr[i].data = *p;
p++;
}
p = first;
for (i=0; i<j;i++)
{
cout << " " << *p << " " << ptr[i].data << ptr[i].ch;
p++;
}
p--;
for (i=0; i<j;i++)
{
cout << " " << *p;
p--;
}
char c;
cin >> c;
return 0;
}
Explanation / Answer
/* ****Header Files declarations *** */
#include<iostream>
using namespace std;
/* Global constant variable decalaration */
const int size = 10;
/* **** Structure Declaration ****** */
struct node
{
int data;
node *next;
char ch;
};
/* ***** Driver Program ******** */
int main()
{
//Variable Declarations
node *ptr; //Struct Type variable declared
int *p;
int i, j, *first;
cout << " enter size of new array: "; // enter number for array size
cin >> j; //Reading value from the user
ptr = new node[j]; //Dynamically creating node struct array
p = new int[j]; //Creating an integer array
first = p;
//For loop to read Data from the user.
for ( i=0; i<j;i++)
{
cout << " enter data: ";
cin >> *p; //Reading Integer value
cout << " enter character: ";
cin >> ptr[i].ch; //Reading character variable of struct
ptr[i].data = *p; //Assigning the integer value read to data variable of struct
p++; //Incrementing to the next read
}
p = first;
//For Loop to print all the values read for integer array and node struct
for (i=0; i<j;i++)
{
cout << " " << *p << " " << ptr[i].data << ptr[i].ch;
p++;
}
p--;
//Printing the Integer Array Backwards
for (i=0; i<j;i++)
{
cout << " " << *p;
p--;
}
char c;//Declaring Character type variable
cin >> c;//Reading the character type variable
return 0;//Exiting main code with 0
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.