THESE ARE ERRORS PLS Help: { recursion.cpp:62:12: error: cannot initialize retur
ID: 670519 • Letter: T
Question
THESE ARE ERRORS PLS Help: {
recursion.cpp:62:12: error: cannot initialize return object of type 'int' with an rvalue of
type 'const node *'
return walker + (length(st->p));
^~~~~~~~~~~~~~~~~~~~~~~~
recursion.cpp:69:22: error: indirection requires pointer operand ('int' invalid)
total += *howMany;
^~~~~~~~
recursion.cpp:74:12: error: indirection requires pointer operand ('int' invalid)
if(*howMany =='') return 0;
^~~~~~~~
recursion.cpp:75:19: error: no matching function for call to 'sumR'
return 1+ sumR(howMany + 1);
^~~~
recursion.cpp:19:5: note: candidate function not viable: no known conversion from 'int' to
'const node *' for 1st argument
int sumR(const node *);
^
recursion.cpp:22:5: note: candidate function not viable: no known conversion from 'int' to
'const char *' for 1st argument
int sumR(const char *); // sum ASCII value
^
recursion.cpp:72:7: note: candidate function not viable: requires 2 arguments, but 1 was
provided
int sumR(const int * p, int howMany)
^
recursion.cpp:82:39: error: no member named 'sum' in 'node'
return listOfNumbers + sum(p->sum);
~ ^
recursion.cpp:88:28: error: no member named 'howMany' in 'node'
return 1 + sumR(p->howMany);
~ ^
recursion.cpp:94:25: error: comparison between pointer and integer
('int' and 'const char *')
for(int i =0; i < ptr; i++
~ ^ ~~~
recursion.cpp:95:4: error: expected ')'
{
^
recursion.cpp:94:12: note: to match this '('
for(int i =0; i < ptr; i++
^
recursion.cpp:136:38: error: reference to overloaded function could not be resolved; did
you mean to call it?
cout<<"deleting"<prev->data<<endl;
}
THE CODE:::
#include <iostream>
using namespace::std;
struct node
{
int data;
node * p;
};
int length( const char *);
int lengthR(const char *);
int length( const node *);
int lengthR(const node *);
int sum(const int *, int howMany);
int sumR(const int *, int howMany);
int sum(const node *);
int sumR(const node *);
int sum(const char *); // sum ASCII values
int sumR(const char *); // sum ASCII value
int max(const char *);
int maxR(const char *);
int max(const int * , int howMany);
int maxR(const int *, int howMany);
int max(const node *);
int maxR(const node *);
void makeLinkedList(node * & start);
void printLinkedList(ostream &, const node *);
void deleteLinkedList(node *);
int length(const char *p)
{
int ans = 0;
for (int i = 0; p[i]; i++){
ans++;
}
return ans;
}
int lengthR(const char *p)
{
if (*p == '') return 0;
return 1 + lengthR(p + 1);
}
int lengthR(const node * st)
{
if (st == NULL) return 0;
return 1 + lengthR(st->p);
}
int length(const node * st)
{
const node * walker = st;
if (st == NULL) return 0;
return walker + (length(st->p));
}
int sum(const int * p, int howMany)
{
int total;
for(int i =0; i < howMany; i++)
total += *howMany;
return total;
}
int sumR(const int * p, int howMany)
{
if(*howMany =='') return 0;
return 1+ sumR(howMany + 1);
}
int sum(const node *p)
{
const node * listOfNumbers =p;
if(p==NULL) return 0;
return listOfNumbers + sum(p->sum);
}
int sumR(const node *p)
{
if(p==NULL) return 0;
return 1 + sumR(p->howMany);
}
int max(const char *ptr)
{
int temp =0;
for(int i =0; i < ptr; i++
{
temp++;
}
return temp;
}
int maxR(const node *ptr)
{
if(ptr == NULL) return 0;
return maxR(ptr->p);
}
void printLinkedList(ostream &, const node *s)
{
const node * walker = s;
while(walker != NULL)
{
cout<< walker->data<<"";
}
cout << endl;
}
void deleteLinkedList(node *s)
{
if(s==NULL) return;
if(s->p==NULL)
{
delete s;
}
node * prev = s;
node * current = prev->p;
while(current !=NULL)
{
cout<<"deleting"<<prev->data<<endl;
delete prev;
prev = current;
current = current->p;
}
cout<<"deleting"<prev->data<<endl;
delete prev;
cout<<"destructor leavng"<<endl;
}
void makeLinkedList(node * & start)
{
// make the linked list 7 -> 12 -> 6 -> 15 -> 3
start = new node;
start->data = 7;
start->p = new node;
start->p->data = 12;
start->p->p = new node;
start->p->p->data= 6;
start->p->p->p = new node;
start->p->p->p->data= 15;
start->p->p->p->p= new node;
start->p->p->p->p->data= 3;
start->p->p->p->p->p= NULL;
}
int main()
{
cout << length("abcdefgh") << endl;
cout<< lengthR("abcd");
node * st;
makeLinkedList(st);
cout << st->p->p->data << endl;
cout << lengthR(st) << endl;
printLinkedList(cout,st);
deleteLinkedList(st);
return 0;
}
Explanation / Answer
Modified program: Highlighted the modified code in bold
#include <iostream>
using namespace::std;
struct node
{
int data;
node * p;
};
int length( const char *);
int lengthR(const char *);
int length( const node *);
int lengthR(const node *);
int sum(const int *, int howMany);
int sumR(const int *, int howMany);
int sum(const node *);
int sumR(const node *);
int sum(const char *); // sum ASCII values
int sumR(const char *); // sum ASCII value
int max(const char *);
int maxR(const char *);
int max(const int * , int howMany);
int maxR(const int *, int howMany);
int max(const node *);
int maxR(const node *);
void makeLinkedList(node * &start);
void printLinkedList(const node *);
void deleteLinkedList(node *);
int length(const char *p)
{
int ans = 0;
for (int i = 0; p[i]; i++){
ans++;
}
return ans;
}
int lengthR(const char *p)
{
if (*p == '') return 0;
return 1 + lengthR(p + 1);
}
int lengthR(const node * st)
{
if (st == NULL) return 0;
return 1 + lengthR(st->p);
}
int length(const node * st)
{
if (st == NULL) return 0;
return 1 + (length(st->p));
}
int sum(const int * p, int howMany)
{
int total;
for(int i =0; i < howMany; i++)
total += howMany;
return total;
}
int sumR(const int * p, int howMany)
{
if(howMany ==0) return 0;
return 1+ sumR(p, howMany + 1);
}
int sum(const node *ptr)
{
const node * listOfNumbers = ptr;
if(ptr==NULL) return 0;
return listOfNumbers->data+sum(ptr->p);
}
int sumR(const node *ptr)
{
if(ptr==NULL) return 0;
return 1 + sumR(ptr->p);
}
int max(const char *ptr)
{
int temp =0;
for(int i =0; i < *ptr; i++)
{
temp++;
}
return temp;
}
int maxR(const node *ptr)
{
if(ptr == NULL) return 0;
return maxR(ptr->p);
}
void printLinkedList(const node *s)
{
const node * walker = s;
while(walker != NULL)
{
cout<< walker->data<<" ";
walker = walker->p;
}
cout << endl;
}
void deleteLinkedList(node *s)
{
if(s==NULL) return;
if(s->p==NULL)
{
delete s;
}
node * prev = s;
node * current = prev->p;
while(current !=NULL)
{
cout<<"deleting"<<prev->data<<endl;
delete prev;
prev = current;
current = current->p;
}
cout<<"deleting"<<prev->data<<endl;
delete prev;
cout<<"destructor leaving"<<endl;
}
void makeLinkedList(node * &start)
{
// make the linked list 7 -> 12 -> 6 -> 15 -> 3
start = new node;
start->data = 7;
start->p = new node;
start->p->data = 12;
start->p->p = new node;
start->p->p->data= 6;
start->p->p->p = new node;
start->p->p->p->data= 15;
start->p->p->p->p= new node;
start->p->p->p->p->data= 3;
start->p->p->p->p->p= NULL;
}
int main()
{
cout << length("abcdefgh") << endl;
cout<< lengthR("abcd");
node * st;
makeLinkedList(st);
cout << st->p->p->data << endl;
cout << lengthR(st) << endl;
printLinkedList(st);
deleteLinkedList(st);
system("pause");
return 0;
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.