Hello all, brand new to the forum. I was thrown into a Computer Science class out of no where and already have an assignment with little knowledge of what's going on. I've scheduled appointments with the teacher but those don't start for a week and I still have the assignment. Go figure... I know the class is focused on "functions" right now. I would very much appreciate it if someone could help me with this assignment. Explanation would be wonderful, we are using Microsoft Visual Studios 2013 Ultimate. There's two problems so one at a time would be helpful.
Assignment follows:
"Problem 1
Write a program that prints a banner with the message "HELLOWORLD" rotated to be read from top to bottom. Specifically, each letter in the message should be printed using multiple lines composed of * symbols. The code to print each letter should go into a separate function for that letter. That is, you must write seven different functions for the seven letters in the message: D, E, H, L, O, R, and W. Then, your main function will call each function in the correct order to print out the message. To help get you started, here is the D() function that I used:
Problem 2
Write a program that calculates the average of a stream of positive numbers. The
user can enter as many positive numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, treat zero as a positive number, i.e., zero counts as a number that goes into the average. Of course, the negative number should not be part of the average. You must use a function to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main() function. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time. The main() function must print the average (which was returned from your function), and then ask the user if they want to repeat the entire process. The user should be allowed to repeat the process as many times as they wish. This means you will have a loop in your main() function that instructs the user what to do, calls your average function, reports the result, and finally asks the user if they want to calculate another average for a different set of numbers.
Note that your function that reads in the numbers and computes the average must have no cout statements. All output must happen in main(). Also, be sure you take the time to think through the pieces and, more importantly, implement in small steps. I suggest you proceed like so:
• Get the structure of defining and calling your function down first, with no loops involved.
• Fill in the function with a loop that runs while the user enters positive (or zero) numbers.
• Modify the loop to keep track of the sum and count, then print the average in the function.
• Modify the function to actually return the average.
• Modify main() to use another loop to ask the user if they want to repeat the entire calculation.
At each step, build your program, fix any errors, test it out, and convince yourself that that piece is correct befor[/b]e moving on."
First of all, every program must have an entry point and usually this is the function called "main". This function acts as a gateway to other functions and just basically carries out all of the code in your program. You can call functions by simply typing their name and if it has parameters then they will go inside of the brackets. So, if you wanted the code inside the function "void D()" to run, you would very simply type "D();" in your main function. The "void" part in front of the function name determines what type of value it returns; since it is "void", a value isn't usually returned.
I explained some basics of functions just in case you didn't quite understand them. Anyway, all you have to do for problem 1 is call all of the functions that output the letters one at a time. As given in the example, a letter 'D' is output into the console. So, all you need to do is create new functions just like that one and then output the other letters in ascii art form. In case you didn't know, "cout" means "console output" and as it suggests it writes something into the console with the left binary shift operator ("<<"); and "endl" simply goes onto the next line. So basically, you just need to work out the ascii art forms of the letters either by doing it yourself or you can find an ascii art generator on the internet to save time.
Once you have done that, you have to call the functions as stated earlier. After that, you will need to pause the console program otherwise it would output the text and then close down and since a computer is extremely fast, it would appear as if it didn't do anything to the untrained eye. This isn't very complicated and can be done with "cin.get()". If you want to know what exactly this does then it does this: it gets a character from the input buffer. But if there isn't one then it waits for input, so in other words it waits until the user presses the enter key.
See how you go and then I'll be willing to give you some more help if you so wish.
Tom
P.S - sorry if it's a bit too long!
P.P.S - this assignment (in my opinion) is a bit complicated for someone just starting out!
I think your assignments are very hard for a beginner and what type of code can you use? Probably only that which you have been taught. So I am quessing vectors are out?
If you play around with problem 2 you might come up with something like this...
For problem 1, that example is poor. Hopefully this one makes more sense. Compile and run this code and see if you can trace through why the program outputs what it does:
#include <iostream>
usingnamespace std;
void H()
{
cout << "Now inside a function used to print an 'H'!" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
cout << "*******" << endl;
cout << "* *" << endl;
cout << "* *" << endl;
}
int main()
{
cout << "I'm about to call a function that will print an 'H' made out of asterisks!" << endl << endl;
H();
cout << "Back in main!" << endl;
}
If this makes sense, then you can write the functions for the other letters similarly, and call them from main in the appropriate order.
Tom thank you for that explanation, it makes more sense now. Booradley, thank you for the example as well. I am currently trying to make sense of all this information I have now. I will report back with my progress. Thank you all again
So after a lot of struggle and wanting to rip my hair out, and finally the information you've gentlemen have given me as well as my text book I came up with this and it surprisingly worked.
#include <iostream>
using namespace std;
Not sure why when I paste the code in here the letters are messed up, they appear perfect in Visual studios however. I am moving onto the next problem as best I can.
And after a long weekend, I found a solution to problem 2. Thank you all for you help.
#include <iostream>
using namespace std;
double average();
int main()
{
cout << "Your avergae is: " << average() << endl;
}
double average()
{
double input=0;
double total = 0;
int count=0;
double average;
do{
cout << "Enter positives numbers to claculate average. Enter a negative number to calculate." << endl;
cin >> input;
if (input >= 0)
{
total = input + total;
count++;
}
}
while (input >= 0);
average = total / count;
return average;