( Posting the same question for third time. Can I please get answer in C++.( and
ID: 3792829 • Letter: #
Question
( Posting the same question for third time. Can I please get answer in C++.( and not in C or Java) ..... Please Try and Provide me a complete programm Answer. not just one or two function)
* Declare a single dimensional array of 65 characters. Convert each character into an integer and store it in a linked list. Manipulate the linked list by completing the following task:
Create an additional linked list call greater_List, using the original linked list copy all number greater than 100 into the greater_list and give a total count of the numbers greater than 100. Create an additional array called less_array, copy all the numbers less than or equal to 100 in the original list into less_array give a total count of the numbers that are less than or equal to 100.
Explanation / Answer
#include <iostream>
using namespace std;
#define MAX 65
#define BREAK_VAL 100
/*linked list node*/
struct node
{
node(int const& val) : value (val), next(nullptr)
{
}
int value;
node* next;
};
/*print integer array*/
void PrintArr(int arr[], int const& size)
{
cout << "Printing Array" << endl;
for(int i = 0; i< size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
/*print linked list*/
void PrintList(node* head)
{
cout << "Printing list" << endl;
while(head)
{
cout << head->value << " ";
head = head->next;
}
cout << endl;
}
/*make linked list from array*/
node* MakeLL(char arr[], int const &size)
{
if(size <= 0)
{
return nullptr;
}
node *head = new node(arr[0]);
node* curr = head;
int i = 1;
for(; i<size; ++i)
{
curr->next = new node(arr[i]);
curr = curr->next;
}
return head;
}
/*fill greater values than comp_value in linked_list*/
void MakeGreater(node *head, node *&greater_list_head, int &count_greater, int const& comp_value)
{
node* curr = greater_list_head;
count_greater = 0;
while(head)
{
if(head->value > comp_value)
{
++count_greater;
if(!greater_list_head)
{
greater_list_head = new node(head->value);
curr = greater_list_head;
}
else
{
curr->next = new node(head->value);
curr = curr->next;
}
}
head = head->next;
}
}
/*fill lesser values then comp_value in array*/
void FillLesser(node* head, int arr[], int &count_lesser, int const& comp_value)
{
count_lesser = 0;
while(head)
{
if(head->value <= comp_value)
{
arr[count_lesser++] = head->value;
}
head = head->next;
}
}
int main()
{
/*user input for MAX characters*/
char arr[MAX];
int i = 0;
while(i < MAX)
{
cin >> arr[i];
++i;
}
/*Making Linked list from user character array*/
node *head = MakeLL(arr, MAX);
/*finding all elements greater than BREAK_VAL and making new linked list*/
node *greater_list = nullptr;
int count_greater = 0;
MakeGreater(head, greater_list, count_greater, BREAK_VAL);
/*finding all elements lesser than BREAK_VAL and making new array*/
int arr_lesser[MAX-count_greater];
int count_lesser;
FillLesser(head, arr_lesser, count_lesser, BREAK_VAL);
/*Printing greater_list*/
PrintList(greater_list);
/*Printing arr_lesser*/
PrintArr(arr_lesser, count_lesser);
cout << "Total numbers of chars whose values are less than 100 : " << count_lesser << endl;
cout << "Total numbers of chars whose values are greater than 100 : " << count_greater << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.