Home > Software engineering >  Implementation of a tree structure and calculation of mean value in the same subprogram
Implementation of a tree structure and calculation of mean value in the same subprogram

Time:12-04

I have worked quite a bit on my code but it doesn't seem to work. The program should consist of a subprogram that prints out all the trees and the return the average value of the trees as a double. I want my subprogram print_tree do both be able to print out the trees and return the mean value.

I managed to work some things out:

  • Calculation of the mean value
  • Implementation of the '|'
  • It will print out the right numbers of trees depending on the input
  • Print the right side of the tree

What I have problems with:

  • How do I print out the left side of the tree without ruining the tree structure. When I tried to do this my tree messed up. I believe it has something to do with my setw(k). How do I modify my code to work? I believe it only needs a few fixes.
  • How am I supposed to both print out the trees and return the mean value in the same program? I have a void now because when I try to do a double so that I can do a return it won't work.

This is how it should look like in the terminal:

Type integers: 2 1 4

  |
 -|-
--|--
  |
 |
-|-
 |
    |
   -|-
  --|--
 ---|---
----|----
    |
The average value was: 2.3

This is how my terminal looks like:

Type integers: 2 1 4

|
-|-
--|--
|
|
-|-
|
|
-|-
--|--
--- |---
--- -  |----
|
2.3
The average value was: 

As soon as I added my third loop (the k loop) my code got completely messed up.

This is my code as of now:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void calculate_median (int const sum, int const counter) {
    cout << setw(1) << setprecision(1) << fixed << static_cast<double>(sum) / static_cast<double>(counter) << endl;
}

void print_tree() {
    int sum {};
    int tree {};
    int counter {};
    
    cout << "Type integers: ";
    while (cin >> tree) {
        counter  ;
        sum  = tree;

        for (int i {}; i <= tree;   i) {    
            for (int k {}; k < i;   k) {
                cout << left << setfill(' ') << setw(k) << '-';     
            }

            cout << '|';
            for (int j {}; j < i;   j) {
                cout << "-"; 
            }

            cout << '\n';
        }

        cout << '|' << endl;
    }

    return calculate_median(sum, counter);  
}
  
int main() {
    print_tree();
    cout << "The average value was: ";

    return 0;
}

CodePudding user response:

Your loop over k is meant to print the left side of a branch, right?
Not the entire branch.
Your code there is

for (int i {}; i <= tree;   i) {    
    for (int k {}; k < i;   k) {
        cout << left << setfill(' ') << setw(k) << '-';     
    }

Why do you set the field width (/offset) within that loop, and not before printing the entire branch? Easiest way to see that it can't work is to see that your i starts as zero, and in that case, the loop over k doesn't iterate. With that alone, your central stem isn't displaced in the first iteration at all, right?

Next, let's say tree is 2. We want the result

  |
 -|-
--|--
  |

So in our first iteration of the loop over k has to set the offset to what?
Right now, you set it to zero. For the next iteration to one.
This is the opposite of what we want to do - initially, the offset has to be the highest, and then become smaller and smaller, except for the trunk. In the case of tree = 2, the first offset needs to be 3 (=2 1) or higher (last offset should be one), and the offset in each following iteration the previous value minus one.

Look at this code:

for (int i = 0; i <= tree; i  )
{ 
    cout << setw(tree 1-i);

    for (int k = 0; k < i; k  ) cout << "-";   
    cout << '|';
    for (int k = 0; k < i; k  ) cout << "-"; 
    cout << endl; 
}

cout << setw(tree 1);
cout << '|' << endl;

In the initial iteration, for tree = 2, we set the offset to 2 1-0 = 3, that is the tip of the tree has an offset of tree. Then the second branch gets an offset of 2 1-1 = 2, and the third 2 1-2 = 1.
Last, we print the trunk, which has to get the same offset as the tip, 2 1 = 3.

By the way, why do you initialize your iteration variables with {}? Technically, that works, but I find that to be quite unusual.

CodePudding user response:

This all should work with some minor indentation and comments to explain what's happening and where it is.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void calculate_mean (int const sum, int const counter) {
    double mean = sum/counter;
    cout << "The average value was: ";
    cout << mean;
}

void print_tree () {
    int sum {};
    int tree {};
    int counter {};
    
    cout << "Type integers: ";
    
    while (cin >> tree) {
        counter  ;
        sum  = tree;

        for (int i {}; i <= tree;   i) { 
            for (int l {}; l < tree-i; l  ) { //adding the required spacing for each branch '-' based on tree-row(i)
                cout << ' ';
            }

            for (int k {}; k < i;   k) {
                cout << '-';     
            }

            cout << '|';
    
            for (int j {}; j < i;   j) {
                cout << "-"; 
            } 

            cout << '\n'; 
        }

        for (int l {}; l < tree; l  ) { // just the same loop for adding the spacing for the stem
            cout << ' ';
        }

        cout << '|' << endl;
    }
    
    calculate_mean(sum, counter);  //because the function is of type 'void' we can't return a value so instead we'll just call it outright
    //and tell the calculate_mean function to just print its result to the console.
    //also as an aside median means the middle value of a set while mean means the average which is what I assume you wanted since
    //the final message says average value
}
  
int main() {
    print_tree();
  
    return 0;
}
  •  Tags:  
  • c
  • Related