Thank you in advance for anyone who is willing to help me. Can someone give me the answers and briefly explain what you did? I have been stuck and frustrated with these questions for about a week now, so I'm asking for help. There are two questions that I need help with and they are:

1.) Write a program to print the hollow diamond of stars (*), ask the user to enter an odd number as the height. For example, if user enters height as 7, output will look like:

        * 
      *   * 
    *      * 
   *        * 
    *      * 
     *   * 
       *

2.) Write a program that asks the user to type positive integers. When the user types a negative value the program writes ERROR and asks for another value. When the user types -1 that means that the last value has been typed and the program must write the average of the positive integers. If the number of typed values is zero the program writes 'NO AVERAGE'.

Dani AI

Generated

As noted by , the second problem's spec is ambiguous because -1 is both "negative" and the intended sentinel. A practical interpretation that matches common assignments: treat -1 as the sentinel that ends input (do not print ERROR for -1); any other negative value prints "ERROR" and is ignored. If no positive integers were entered before -1, print "NO AVERAGE". This makes the behavior deterministic and testable.

For the hollow diamond use the row's distance from the center instead of checking diagonal sums. That makes the logic easier to generalize and reason about (it produces the same pattern described by diagonals). Let center = n/2. For each row compute outer = abs(center - row). If inner = n - 2*outer - 2 is negative, print one star after outer spaces; otherwise print a star, inner spaces, then a star. Validate that n is odd and positive before drawing.

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter odd height: ";
    while (cin >> n) {
        if (n > 0 && n % 2 == 1) break;
        cout << "Height must be a positive odd integer. Try again: ";
    }
    int center = n / 2;
    for (int row = 0; row < n; ++row) {
        int outer = (row > center) ? row - center : center - row;
        int inner = n - 2 * outer - 2;
        for (int s = 0; s < outer; ++s) cout << ' ';
        if (inner < 0) cout << '*' << '\n';
        else {
            cout << '*';
            for (int s = 0; s < inner; ++s) cout << ' ';
            cout << '*' << '\n';
        }
    }
    return 0;
}

For the averaging program use -1 as the stop token, print ERROR for other negatives, accumulate positives, and handle the zero-count case. Example:

#include <iostream>
using namespace std;

int main() {
    long long sum = 0;
    int count = 0;
    int x;
    cout << "Enter integers (-1 to finish):\n";
    while (cin >> x) {
        if (x == -1) break;
        if (x < 0) { cout << "ERROR\n"; continue; }
        sum += x; ++count;
    }
    if (count == 0) cout << "NO AVERAGE\n";
    else cout << "Average: " << (static_cast<double>(sum) / count) << '\n';
    return 0;
}

Notes: use a 64-bit accumulator to reduce overflow risk, test with small odd heights (1, 3, 7) and mixed inputs, and avoid pasting solutions into graded assignments without understanding (a point raised by ).

Recommended Answers

All 5 Replies

You haven't shown us what you have done to solve this yourself, but for the first one, for problems of this type I suggest you work with a specific example. Take a number, like 7, then write out the number of each character that needs to be output per line

3 spaces plus 1 asterisk
2 spaces plus 1 asterisk plus 1 space  plus 1 asterisk
1 space  plus 1 asterisk plus 3 spaces plus 1 asterisk
etc

Then figure out what loop(s) you need to generate those numerical values. Once you have this for a specific number you can generalize it for any odd number.

The second question suffers from spec error (and was asked by another user). A negative number both generates an error and causes an average to be calculated. Rewrite the spec.

commented: Thank you, I got the question done yesterday. +0

The hollow diamond is about distance from center column and distance from center row; their sum s/b half the height to print. In this case, 3. So:

for ( int c = 0 ; c < 7 ; c++ ){
  for ( int r = 0 ; r < 7 ; r ++ ){
     If ( r + c == 3  || r - c == -3 || r + c ==  9 || r - c == 3 ){
        cout << ‘*’ ;
      }
    }

     cout << ‘\n’ ;
   }
// r + c goes from upper right to lower left
// r - c  goes from upper left to lower right
commented: On Daniweb we strive to help. Doing someone's homework for them is not helping. -3

How do you teach those who fall off the official wagon?

@DG, I tend to not worry about when the student gets the homework done for them. Why? Next week another assignment and they didn't learn from last week so the climb gets harder and harder. Eventually they fail. Or worse they learn "this is how you program today. You ask and let others write the code for you."

PS. Added with edit. Now that this code is on the web, a prof could use one of the many plagiarism checkers and blammo. They get a failing grade on this assignment.

commented: In my defense, I do not have a proffesor for my C++ for Engineers course. I only have my TA to teach me, and he is not great at explaining. +0

How do you teach those who fall off the official wagon?

To use your metaphor, what kind of lesson is giving a drink to an alcoholic? We don't teach people the value of work by doing it for them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.