Question

(C++) compute and return the sum of the ints contained in the Array table using RECURSION....

(C++) compute and return the sum of the ints contained in the Array table using RECURSION. Start with the following function.

int sum(node * head[]);   

//

//

//This is the provided header file.

//arr.h
#include
#include
#include

const int SIZE = 5;   //size of the array of head pointers

struct node
{
int data;
node * next;
};


/* These functions are already written and can be called to test out your code */
void build(node * head[]); //supplied
void display(node * head[]); //supplied
void destroy(node * head[]); //supplied

/* *****************YOUR TURN! ******************************** */
//Write your function prototype here:

int sum(node * head[]);   

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int sumNode(node *head){
    if(head == NULL){
        return 0;
    }
    else{
        return head->data + sumNode(head->next);
    }
}

int sum(node * head[]){
    int s = 0;
    for(int i = 0;i<5;i++){
        s += sumNode(head[i]);
    }
    return s;
}
Add a comment
Know the answer?
Add Answer to:
(C++) compute and return the sum of the ints contained in the Array table using RECURSION....
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
  • For this lab, you will need to write the following functions in table.cpp, and add function...

    For this lab, you will need to write the following functions in table.cpp, and add function prototypes for them to table.h and invoke the functions in main.cpp. * int countNodes(node * root) Recursively count the number of the nodes in the tree. * int sumLeaves(node * root) Recursively sum all of the leaf nodes in the tree. You must use the functions with these exact function prototypes. ****************************************************************************** main.cpp #include "table.h" #include <iostream> using namespace std; int main() { node...

  • c++, need help thank you Given an array of ints, return the sum of all elements...

    c++, need help thank you Given an array of ints, return the sum of all elements from the array that come before the first element that equals number 4 in the array. The array will contain at least one 4. Function prototype: int pre4int array ( ], int size); Hint: to find the array size, use: int size = sizeof(array) / sizeof( array[0]); Sample runs: int array1[ ] = {1, 2, 4, 1); pre4(array1, 4); // returns 3 int array2...

  • In C An array ints of integers has already been declared and initialized. A function printint...

    In C An array ints of integers has already been declared and initialized. A function printint has been defined with the following prototype: void printint(int *); Write code that will call printint with a pointer to the second value in the array ints. Note: you will not need to declare an int variable.

  • how can I find the sum of data that are in the list using an ARRAY...

    how can I find the sum of data that are in the list using an ARRAY of linked list . I just need a small example please. I'm not sure how to move from head[0] to head[1]....??? // This is the struct that I am working on: struct node { int data; node * next; } class List { public: list(); ~list(); private: node ** head; int size; };

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

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
ADVERTISEMENT