Click to See Complete Forum and Search --> : HELP!!! With C++


shaggy112
01-24-2001, 09:52 PM
Hi,
I am working on a project for school.
I need to write a simple program, and am having a hard time with it. Can someone help? Here are the parameters...Thanks!

"Write a program which utilizes a function named drawit. This function accepts an integer parameter between 3 to 15 (odd numbers only) . You must perform an error check. The output of function drawit(7) is:

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


Utilize for, do..while loop to complete these programs. You must perform at least one function call.
"

TheLinuxDuck
01-24-2001, 09:58 PM
Why don't you post either what you've got so far or your ideas, and we can offer suggestions or directions for you to study. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
I have a belly button.
:wq

Energon
01-24-2001, 10:04 PM
yup... post what ya got... people here more than likely won't do your homework for ya... http://www.linuxnewbie.org/ubb/wink.gif

anyway, here's a bit of pseudo code for the main algorithm that I made up real fast:


integer number
input number
if number is less than 3 or greater than 15
error
else if number%2 equals 0
error

temp_number equals number*2
for 1 to number
for 1 to temp_number
print "*"
temp_number equals number - 2
loop
loop


I'll leave it up to you to add the do...while loop (and yeah, the first part where it error checks is coded really bad and could be improved upon), but the for loop is the basic algorithm...

EDIT - needed the code tags

[This message has been edited by Energon (edited 24 January 2001).]

shaggy112
01-24-2001, 10:12 PM
Thanks for the alogrithm....
That is what I am needing...I will work with it and post again.
FYI...Not my class...I am helping someone else http://www.linuxnewbie.org/ubb/smile.gif
blind leading blind!
not my expertise....yet

[This message has been edited by shaggy112 (edited 24 January 2001).]

patas
01-24-2001, 10:22 PM
I actually had to do something similar for my C++ class only upside down. The logic is simple, use one for loop to print the number of lines and another for loop to print the "*". Create an integer variable for the row, i.e. int row;, and create another variable for the "*", i.e. int mark;. The first for loop will look something like this: for(int row = 10; row > 1; row--). This will loop ten times, giving you ten lines. The second for loop will look something like this: for(int mark = 0; mark < row - mark; mark++). This will print the "*". Of course you have to figure out the rest, but this should do what you need. It might be a bit off since I don't have my original code, but at least it should get you started.

------------------
If you smell what the Tux is serving!!!

shaggy112
01-24-2001, 10:24 PM
Here is what I have so far.
Any ideas of what to do next?
Thanks!
#include <iostream.h>

int main ()
{
int num, anum, var2;
cout << "How many rows of stars? ";
cin >> num;
if (( num < 3) | | ( num > 15 ) | | ( num = 0))
return 1;
anum = num * 2;
for ( var2=1; var2<30; var2++)
{
cout << "*\n";
anum = num - 2;
}
return 0;
}

Gweedo
01-24-2001, 10:59 PM
Just an idea.


Instead of how you got the error checking setup right now actually ask the person to input a number of rows between 3 and 15 and the number must be odd. If they go out of bounds then Tell them there number was not between 3 and 15 or odd and to enter a new number that is between 3 and 15 and make it an odd number.


[This message has been edited by Gweedo (edited 24 January 2001).]

Energon
01-24-2001, 11:07 PM
what he needs to do is encapsulate the code in a do while that loops if the number is out of bounds and breaks if it's within (and then it prints the stuff)... it's part of the requirements... if you think about it for a little bit, it'll come to your really easy...

and another thing, instead of this:

#include <iostream.h>

you should do it like this:

#include <iostream>
using namespace std;

that's, afaik, the proper way to include standard C++ headers... you can also fix things up so you aren't declaring 3 variables at the start (C++ doesn't require declarations at the beginning like that)...

and you've got the for loop wrong... look again at the psuedo code and you'll see what I mean... try to match your code as close to mine as possible and it'll work out...

Gweedo
01-24-2001, 11:21 PM
here is you code a little edited. This should get ya more in the direction of what they asked for. did not fix the wrong loop.





#include <iostream.h>

//remember the project ask for a function


int drawit (int);


int drawit (int num)
{
int anum, var2;
anum = num * 2;
for ( var2=1; var2<30; var2++)
{
cout << "*\n";
anum = num - 2;
}
}


int main ()
{
int num;
cout << "The number of stars in a row must be between 3-15 and odd";
cout << "How many rows of stars? ";
cin >> num;


// edited the below line so it will see if the number is odd or even
// according to the project it must be odd and between 3 and 15


if (( num < 3) | | ( num > 15 ) | | (num % 2 == 0))
return 1;


drawit (num);


return 0;
}


[This message has been edited by Gweedo (edited 24 January 2001).]

Energon
01-24-2001, 11:50 PM
mmm... that's right... I put the number%2 in my pseudo code and it got left out in translation... and it's important for testing odd/even numbers...

shaggy112
01-25-2001, 12:07 AM
Okay...I have tried some goto statements to no avail. I do not understand the for, and do...while loops. Can somebody help out with those.
I appreciate all the help thus far.
Thanks!

Energon
01-25-2001, 12:18 AM
hmm... you shouldn't use goto statements...

this is the general form of a do...while:

do
{
// body
} while(condition)

an example:


int number = 0;

do
{
number++
}while(number < 10);


so the code runs through once, setting number to one... and then it tests the condition (is number less than 10? if so, loop)... and then when the number is 10, it exits the loop...

shaggy112
01-25-2001, 01:02 AM
Thanks!
I will try to work with it.

TetsuoII
01-25-2001, 04:59 AM
I would recommend writing some for and while loops which outputs the information in the loop, for example:

for( int i=0; i<10 ; i++ )
cout << i << endl;

and so on. The output will give you an idea of what is happening in the loop and you can work with that.
The important thing to know about a for loop (hint,hint) is that you can increase the loop counter with any value you want.

T.