# Maximum PC

 It is currently Sun Apr 26, 2015 10:18 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 18 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Beginner C++ programmer, needs assignment helpPosted: Sat Nov 27, 2004 7:21 pm
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
I normally wouldn't ask for help like this, especially concerning a college assignment. The assignment has already been handed in, however, so I want to do this for my own benefit.

Basically, I'm going to copy, word for word, what the problem is:

"Write a functional C++ program that inputs an interger and a character. The output should be a diamond composed of the character and extending the width specified by the interger. For example, if the interger is 11 and the character is an asterisk (*), the diamond would look like this:"

Code:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*

The problem is meant to be solved using loops (while statements).

I've been trying to figure this out for the past 5 days. The instructor has tried to point it out, even spelling out the solution in a flow chart on a piece of paper how the program should function, and even fellow classmates have tried to help me but it just won't seem to click inside my head. I'm almost to the point of tearing my hair out because I have a strong feeling this very question will be on the final test for the class on Monday.

Any help would be most appreciative.

Top

 Post subject: Posted: Sat Nov 27, 2004 9:27 pm
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
Notice how the output is 11 lines?
Coincides with supplying "11" - the number equals the number of lines to produce. It's the number of repititions the loop performs.

All a loop is doing is repeating the command(s) in it. So you have logic inside to do what you want...

Next - what's the relationship of the first 3 lines?
They're prime numbers - started at 1, they increase by adding 2 to the previous line. But...

You hit the middle, and instead now you subtract 2 from the previous line.

I have no idea if I'm helping, but I can once I know where exactly you're lost.

Top

 Post subject: Posted: Sun Nov 28, 2004 9:35 am
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Basically this is all I have been able to come up with:

Code:
#include <iostream>
using namespace std;

int main()
{
char letter;
int width;

cout << "This program creates a diamond-shaped object based on a number you input," << endl << "for the width, and the character the diamond will be comprised of." << endl << endl;
cin >> width;
cout << "Please input the character that will make up your diamond: ";
cin >> letter;
cout << endl << endl;

while (width != 0)
{
cout << letter;
width--;
}

cout << endl << endl;
return 0;
}

I know this only prints out a single line comprised of the width and the character entered. My problem is my brain can't think of how to construct the program. I've been at this on and off for 6 days now and I cannot get further than this.

Top

 Post subject: Posted: Sun Nov 28, 2004 10:15 am
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
Baby steps - deconstruct what's happening, and accomplish that. Then move on to the next step/level.

So you've got your loop setup to iterate the 11 times - good.

Now let's simplify the diamond - only go for the top half.
So what do you have to put in:

Code:
while (width != 0)
{
cout << letter;
width--;
}

...to get this:

Code:
*
***
*****

Don't worry about centering them - one thing at a time.

You can put decision logic in loops - if, switch, while, for.

Top

 Post subject: Posted: Sun Nov 28, 2004 10:45 am
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
But that's what I can't figure out. Everytime I try I either get an endless loop or an error.

Ugh. I know the answer must be simple enough, just my brain can't see it. And once it does, I'll be like "I'm such an idiot!"...

Top

 Post subject: Posted: Sun Nov 28, 2004 11:49 am
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
X_ArchAngel wrote:
But that's what I can't figure out. Everytime I try I either get an endless loop or an error.

You shouldn't be getting an endless loop - you shouldn't be working on the loop at all. It's set to run for the number of times a user specifies - nothing to fix there.

I just noticed something that might help - use this code instead:

Code:
int counter = 0

while (counter < width)
{
cout << letter;
counter++;
}

With your code, you'd be working from the bottom up. This will make it easier to think about how to get the output I listed - sorry about not noticing this earlier.

Top

 Post subject: Posted: Sun Nov 28, 2004 1:14 pm
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Well that's all fine and dandy, but it still doesn't help me find out how to make it print on the additional lines with the proper number of characters.

This fucking programming is really starting to piss me off.

Top

 Post subject: Posted: Sun Nov 28, 2004 1:56 pm
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
X_ArchAngel wrote:
Well that's all fine and dandy, but it still doesn't help me find out how to make it print on the additional lines with the proper number of characters.

I'm still getting an idea of where you're having trouble, sorry.

Any time you have a repetitive pattern, you should be thinking about loop logic: The first loop was that the program would show lines based on the number provided by the user - that's one loop. Now there's a pattern to how the "*" is printed - you need another loop, nested inside the first one. They are interrelated - as the first one increments, the next loop needs that number to use to show the proper output.

Printing the format properly, you need to be using a print command that won't create a new line unless you specify it to. For example - If I do:

cout << "a"
cout << "b"

I want to get:
ab

...as output. To make a new line, I'd have to add "\n" to tell the system to put in a line return. I've only done C and Java - I don't know C++ syntax so I can only help on logic.

PS: Don't beat yourself up - the exercise is not the easiest, nothing what I'd give to someone new to programming.

Top

 Post subject: Posted: Sun Nov 28, 2004 3:09 pm
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
That's easy for you to say...

... I can't translate what you just said into code. I'm not that smart enough yet. I know how to make the triangle, so to speak, but when I try thinking about it in code I get stack overflows and blue screen errors in my head. o.0

Basically put I need someone to walk me through the process and not just give hints. I've been given hints for over 5 days and it's done fuck all for me except get me even more confused.

Ugh, I fucking hate programming so much. If I honestly knew there was going to be this much programming in my college course I wouldn't have taken it.

And I'm not upset with you, Canuck, it's just... looking at the same fucking problem for five days, having atleast 5 people try to explain it to me and me STILL not knowing how to approach it is really aggrivating the shit out of me. I want to break something everytime I think of the question now.

Top

 Post subject: Posted: Sun Nov 28, 2004 4:05 pm
 Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Very first thing: build a shell of your program. Try to visualize how you're going to approach it. What headers do you need? You're going to need iostream to output the stars. So, you have:
Code:
#include <iostream>
using namespace std;//I never do this because it defeats the purpose of namespaces, but whatever

int main()
{

return 0;
}

Now, you'll want to get the number of stars maximum and make sure the user entered valid input. Valid input is probably going to be > 3, and it has to be an off number. This is simple enough, just use std::cin from <iostream>:
Code:

#include <iostream>
using namespace std;//I never do this because it defeats the purpose of namespaces, but whatever

int main()
{
int number_of_stars;//maximum number of stars

cout << "Maximum stars / rows? Enter an odd integer greater than or equal to 3: " << flush;//flush clears the output buffer
cin >> number_of_stars;//get the input
cin.ignore(1000, '\n');//ignore the newline left in the input buffer by cin

if( (number_of_stars<3)||(number_of_stars % 2 ==0))
{
cout << "Invalid input." << flush;
return 0;
}
return 0;
}

You always want to use std::flush or std::endl after you std::cout. It flushes the output buffer so the text appears when you want it to.
std::cin leaves the '\n' from when you pressed enter (i.e you enter 13 then press return), so you want to use std::cin.ignore() and ignore the newline and everything up to it after valid input.

Now, the if statement. The first part checks if number_of_stars is less than 3, which we know is invalid. Now, the || operator. Read this just like, "or". Now, get to know the modulo function. It returns the remainder of the division if we were dividing. So, 5%2 would be like saying remainder of 5 / 2. 5 / 2 comes out to be 2 R 1. If you think for a second, you'll see that for any even number, that number modulo 2 will be 0. This is a bit tricky. So the if statement would read, "if number_of_stars is less than 3 or if number_of_stars is even...".

Now, we want to create a loop that will start with one star, increase that number by two stars, until we reach the maximum (number_of_stars), and then come back down to one. A for loop is best suited for this. See if you can follow the logic:

Code:

#include <iostream>
using namespace std;//I never do this because it defeats the purpose of namespaces, but whatever

int main()
{
int number_of_stars;//maximum number of stars

cout << "Maximum stars / rows? Enter an odd integer greater than or equal to 3: " << flush;//flush clears the output buffer
cin >> number_of_stars;//get the input
cin.ignore(1000, '\n');//ignore the newline left in the input buffer by cin

if( (number_of_stars<3)||(number_of_stars % 2 ==0))
{
cout << "Invalid input." << flush;
return 0;
}

for(int current_stars = 1; current_stars <= number_of_stars; current_stars += 2)
{
for(int i = 1; i <= ((number_of_stars - current_stars) / 2); ++i)
{
cout << " " << flush;//output one space
}//this outputs the required number of space padding on the left side

for(int i = 1; i <= current_stars; ++i)
{
cout << "*" << flush;//output one star
}//output the stars

for(int i = 1; i <= ((number_of_stars - current_stars) / 2); ++i)
{
cout << " " << flush;//output one space
}//this outputs the required number of space padding on the right side
cout << endl;//new line, for the next number of stars
}

//now we have just outputted number_of_stars stars, and current_stars is at number_of_stars + 2
current_stars = number_of_stars - 2;//we are now going to the line under the maximum

for(; current_stars >= 1; current_stars -= 2)
{
for(int i = 1; i <= ((number_of_stars - current_stars) / 2); ++i)
{
cout << " " << flush;//output one space
}//this outputs the required number of space padding on the left side

for(int i = 1; i <= current_stars; ++i)
{
cout << "*" << flush;//output one star
}//output the stars

for(int i = 1; i <= ((number_of_stars - current_stars) / 2); ++i)
{
cout << " " << flush;//output one space
}//this outputs the required number of space padding on the right side

cout << endl;//new line, for the next number of stars
}

cin.get();//pause for them so they can see the diamond
return 0;
}

Here is how it works. It enters the first for loop for when we are going up the maximum and when at the maximum. In that foor loop, we enter a for loop for the padding (which we can figure out by (number_of_stars-current_stars)/2) on the left side, then it outputs the stars (current_stars # of stars), then the padding on the right side (same formula as the left side). Now we go to the next line and keep going till the maximum.

Now we do the same, but subtracting (i.e. tapering down the diamond) instead of adding. Then we pause and let the user see the masterpiece.

Does that help?

Top

 Post subject: Posted: Sun Nov 28, 2004 4:22 pm
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Unfortunately it doesn't because I haven't learned 'for' loops yet, and your coding looks too difficult for me to comprehend.

Ugh, just forget. I'm too stressed from trying for 5 days, I don't even care if this is on the test anymore.

Thanks anyways, folks. The effort is appreciated.

Top

 Post subject: Posted: Sun Nov 28, 2004 8:35 pm
 Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
X_ArchAngel wrote:
Unfortunately it doesn't because I haven't learned 'for' loops yet, and your coding looks too difficult for me to comprehend.

Ugh, just forget. I'm too stressed from trying for 5 days, I don't even care if this is on the test anymore.

Thanks anyways, folks. The effort is appreciated.

Don't give up! If you give up then you don't learn

All my code is explained in the comments, and if there's something you don't understand (in particular), ask me and I'll be glad to help.

A for loop is just like a while loop. Instead of just having a condition inside it, though (i.e. while( condition), like while( i < 5)), it allows you to do three things: Set the initial value or declare a variable (or more) in the first part, then the condition, then what to do when the condition isn't yet met.

So,
Code:
int i = 0;
while(i < 5)
{
cout << i << endl;
i++;
}

Can be put in a for loop like
Code:
for(int i =0; i < 5; i++)
{
cout << i << endl;
}

Top

 Post subject: Posted: Mon Nov 29, 2004 2:51 am
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
X_ArchAngel wrote:
Unfortunately it doesn't because I haven't learned 'for' loops yet, and your coding looks too difficult for me to comprehend.

Ugh, just forget. I'm too stressed from trying for 5 days, I don't even care if this is on the test anymore.

Thanks anyways, folks. The effort is appreciated.

You're about to have your final and you haven't covered a for loop? IMO, you should transfer schools, immediately. A for loop is basically just a while loop (the middle condition) that also contains a section to initialize variables and a part that is run at the end of each iterations (including after continues). The while condition is the only required part.

for (init vars; while condition; iterate)

You need to learn to break complex problems (even not so complex ones like this) down into smaller easier problems. First, instead of creating a diamond, let's create a triangle like CanuckieDuckie suggested...

input: 5 *
output:

*
**
***
**
*

Here we can see that two loops are needed. An outer loop for each line of output and an inner loop to print the *'s on each line. Start with the outer loop. What do we know? We know that there are going to be x number of lines depending on the input value, in this case 5. Easy enough.

Now what do we do during each line? We start with a single *. Every line after that adds one more * until we get to the line after the middle line (hint: 5/2). After that we start removing a single * from each line.

After getting the triangle, the diamond shape is just a matter of adding some whitespaces in front of the *. Notice how the number of spaces is sort of the inverse of the number of asterisks.

Top

 Post subject: Posted: Mon Nov 29, 2004 8:09 pm
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Oy. Thanks for all your help, folks, I'll try to apply it later on when I need to finish/correct the assignment last this week(end).

I was way to stressed about it to concentrate, so asking didn't do me much help anyways. The test came and went and I didn't even bother with that question. I just moved on and went about my business.

Thanks again, anyways. The effort is well appreciated.

Top

 Post subject: Posted: Fri Dec 10, 2004 7:21 am
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Okay, I have yet another problem...

I have my final assignment due in a couple of hours, and it involves making functions. I've been working on them and completed the first two some days ago with the teachers assistance.

My train of thought is to make a working program and -then- place it into functions, but as I am starting to learn, it makes it even more difficult to break a whole program down into functions. That and I suck at making functions, I need all the help I can get.

The program is to take any year and break it down into a calendar-style print out, taking into account leap years. As it is now I have the program working about 95% correctly, which is good enough for me right now. All that needs to be done is to break it into seperate functions, where main calls on a function to do all the calculations and return a result. The coding is as follows:

Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
int year, intMonth, day, dayCount, counter, dayOfWeek, intRes1, intRes2, intRes3, julianDay;
string month, spaces, spaces2;

counter = 1;   // Default month set to January for Julian calculation.

cout << "What year do you want a calendar for? ";
cin >> year;

cout << endl << year << endl << endl;

while (counter <= 12)
{
day = 1;   // Default day set to 1 for Julian calculation.

if (counter <= 2)
{
year = year - 1;
intMonth = counter + 12;
}
else
intMonth = counter;

intRes1 = 2 - year / 100 + year / 400;
intRes2 = 365.25 * year;
intRes3 = 30.6001 * (intMonth + 1);
julianDay = intRes1 + intRes2 + intRes3 + day + 1720995;

dayOfWeek = (julianDay + 1) % 7;

if (dayOfWeek == 0)
spaces = " ";
else if (dayOfWeek == 1)
spaces = "    ";
else if (dayOfWeek == 2)
spaces = "       ";
else if (dayOfWeek == 3)
spaces = "          ";
else if (dayOfWeek == 4)
spaces = "             ";
else if (dayOfWeek == 5)
spaces = "                ";
else if (dayOfWeek == 6)
spaces = "                   ";

if (counter == 1)
{
month = "January";
dayCount = 31;
}
else if (counter == 2)
{
month = "February";
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
dayCount = 29;
else
dayCount = 28;
}
else if (counter == 3)
{
month = "March";
dayCount = 31;
}
else if (counter == 4)
{
month = "April";
dayCount = 30;
}
else if (counter == 5)
{
month = "May";
dayCount = 31;
}
else if (counter == 6)
{
month = "June";
dayCount = 30;
}
else if (counter == 7)
{
month = "July";
dayCount = 31;
}
else if (counter == 8)
{
month = "August";
dayCount = 31;
}
else if (counter == 9)
{
month = "September";
dayCount = 30;
}
else if (counter == 10)
{
month = "October";
dayCount = 31;
}
else if (counter == 11)
{
month = "November";
dayCount = 30;
}
else if (counter == 12)
{
month = "December";
dayCount = 31;
}

cout << "       " << month << endl;
cout << "====================" << endl;
cout << " S  M  T  W  T  F  S" << endl;
cout << "--------------------" << endl;
cout << spaces;

while (dayCount > 0)
{
if (dayOfWeek > 6)
{
dayOfWeek = 0;
cout << endl;
}
if (day <= 9)
spaces2 = "  ";
else if (day > 9)
spaces2 = " ";
cout << day << spaces2;
day++;
dayOfWeek++;
dayCount--;
}
cout << endl << endl;
counter++;
}

return 0;
}

I tried making it into a void function earlier, but I'm still not knowledged enough to do it on my own. I'm at home right now but the assignment has to be handed in to the teacher by 4.

I basically have no idea what I'm doing when it comes to functions. I'm still trying to figure out that damned start one now and then.

Top

 Post subject: Posted: Fri Dec 10, 2004 7:53 am
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
X_ArchAngel wrote:
My train of thought is to make a working program and -then- place it into functions

Good way to approach.

X_ArchAngel wrote:
I tried making it into a void function earlier, but I'm still not knowledged enough to do it on my own. I'm at home right now but the assignment has to be handed in to the teacher by 4.

Rather than returning nothing from the function, I see one easy function that could return a string. Another one I see means you have to break into two parts - one returns a string and the other an int.

Top

 Post subject: Posted: Fri Dec 10, 2004 9:14 am
 Million Club

Joined: Tue Aug 17, 2004 9:58 pm
Posts: 2007
Okay, here's what I have so far:

Code:
#include <iostream>
#include <string>
using namespace std;

int GetDate(int, int, string&, string&, int&, int&);

int main()
{
int year, day, dayCount, counter, dayOfWeek;
string month, spaces, spaces2;

counter = 1;   // Default month set to January for Julian calculation.

cout << "What year do you want a calendar for? ";
cin >> year;

cout << endl << year << endl << endl;

while (counter <= 12)
{
GetDate(year, counter, month, spaces, day, year);

cout << "       " << month << endl;
cout << "====================" << endl;
cout << " S  M  T  W  T  F  S" << endl;
cout << "--------------------" << endl;
cout << spaces;

cout << day << spaces2;
day++;
dayOfWeek++;
dayCount--;
}
cout << endl << endl;
counter++;

return 0;
}

int GetDate(int counter, string& month, string& spaces, int& day, int& year)
{
int intMonth, dayCount, dayOfWeek, intRes1, intRes2, intRes3, julianDay;
string spaces2;

day = 1;   // Default day set to 1 for Julian calculation.

if (counter <= 2)
{
year = year - 1;
intMonth = counter + 12;
}
else
intMonth = counter;

intRes1 = 2 - year / 100 + year / 400;
intRes2 = 365.25 * year;
intRes3 = 30.6001 * (intMonth + 1);
julianDay = intRes1 + intRes2 + intRes3 + day + 1720995;

dayOfWeek = (julianDay + 1) % 7;

if (dayOfWeek == 0)
spaces = " ";
else if (dayOfWeek == 1)
spaces = "    ";
else if (dayOfWeek == 2)
spaces = "       ";
else if (dayOfWeek == 3)
spaces = "          ";
else if (dayOfWeek == 4)
spaces = "             ";
else if (dayOfWeek == 5)
spaces = "                ";
else if (dayOfWeek == 6)
spaces = "                   ";

if (counter == 1)
{
month = "January";
dayCount = 31;
}
else if (counter == 2)
{
month = "February";
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
dayCount = 29;
else
dayCount = 28;
}
else if (counter == 3)
{
month = "March";
dayCount = 31;
}
else if (counter == 4)
{
month = "April";
dayCount = 30;
}
else if (counter == 5)
{
month = "May";
dayCount = 31;
}
else if (counter == 6)
{
month = "June";
dayCount = 30;
}
else if (counter == 7)
{
month = "July";
dayCount = 31;
}
else if (counter == 8)
{
month = "August";
dayCount = 31;
}
else if (counter == 9)
{
month = "September";
dayCount = 30;
}
else if (counter == 10)
{
month = "October";
dayCount = 31;
}
else if (counter == 11)
{
month = "November";
dayCount = 30;
}
else if (counter == 12)
{
month = "December";
dayCount = 31;
}

while (dayCount > 0)
{
if (dayOfWeek > 6)
{
dayOfWeek = 0;
cout << endl;
}
if (day <= 9)
spaces2 = "  ";
else if (day > 9)
spaces2 = " ";
}

return 0;
}

I keep getting a bunch of errors and I know it's because I'm still unclear on how to define the variables I'm passing to the functions and back again.

Top

 Post subject: Posted: Fri Dec 10, 2004 1:52 pm
 There is no cause for alarm... yet

Joined: Tue Nov 09, 2004 10:50 am
Posts: 626
Location: Vancouver Island
** Warning: Never done C++, some C though. Give me Java though... **

I was thinking more like:

Code:
String getSpaces(int dayOfWeek)
{
if (dayOfWeek == 0)
spaces = " ";
else if (dayOfWeek == 1)
spaces = "    ";
else if (dayOfWeek == 2)
spaces = "       ";
else if (dayOfWeek == 3)
spaces = "          ";
else if (dayOfWeek == 4)
spaces = "             ";
else if (dayOfWeek == 5)
spaces = "                ";
else if (dayOfWeek == 6)
spaces = "                   ";

return spaces;
}

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 18 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets