|
This outputs
* * * * * * * * * *
*
* *
* * *
* * * *
Which is more than what I could get.
|
|
|
|
|
The problem with the code given is that he only uses one cout<<endl at the end of the function, so all the first stars are coming in the same line, try adding a cout<<endl before the if at the end of the first for. (Or maybe in other position, but you need it)
EDIT: You might also need to change the order of the fors
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
You are correct. I added another <<endl and changed some numbers a little, as you can see below, and got the correct result. Thank you all very much.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int func(int x);
int main(int argc, char *argv[])
{
func(0);
system("PAUSE");
return 0;
}
int func(int x)
{ int i;
for (i=0; i<=x; i++)
{
cout<<"* ";
}
{
cout << endl;
}
if (x<=2)
func(x+1);
for (i=1; i<=x; i++)
{
cout<<"* ";
}
cout << endl;
return 1;
}
Now I need to add a program that prompts the user to enter the number of lines in the pattern and then use the recursive code above to generate the pattern. Any thoughts?
I have tried to add the code below but get errors.
cout<<"Enter a number between 4 and 10: ";
cin>> int x;
cout<< endl;
|
|
|
|
|
Yes, sorry about that. Apart from the missing cout << endl; line already mentioned it gives the following output
* * * *
* * *
* *
*
*
* *
* * *
* * * *
I can provide a recursive function if you relax the requirement for it to take only one parameter:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int func(int x, int y);
int main(int argc, char *argv[])
{
func(1, 4);
return 0;
}
int func(int x, int y)
{
int i;
for (i=1; i<=x; i++)
{
cout<<"* ";
}
cout << endl;
if(x != y)
{
func(x + 1, y);
for (i=1; i<=x; i++)
{
cout<<"* ";
}
cout << endl;
}
return 0;
}
Graham
Librarians rule, Ook!
|
|
|
|
|
You are right. This works as well. Even better actually. You have helped out greatly. Thank you.
Do you know how to promt the user to enter the number of lines in a pattern and will use the recursive code to make the pattern?
I tried the below code and got errors.
cout<<"Enter a number between 4 and 10: ";
cin>> int x;
cout<< endl;
|
|
|
|
|
You should declare the variable before, and then assign the value.
int x = 0;
cout<<"Enter a number between 4 and 10: ";
cin>>x;
cout<<endl;
and then call the function
func (x)
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
That works. I just need to change some stuff around.
If I enter 0 I get
*
* *
* * *
* * * *
* * *
* *
*
If I enter 4 I get
* * * * *
* * * *
If I enter 10 I get
* * * * * * * * * * *
* * * * * * * * * *
I think that needs to be if 10 is entered.
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
|
|
|
|
|
Don't understand. I've changed the main function to be
int main(int argc, char *argv[])
{
int x = 0;
cout<<"Enter a positive number: ";
cin>>x;
cout<<endl;
if(x > 0)
func(1, x);
return 0;
}
and everything works fine (the check for a positive, non zero number is to avoid extremely large loops)
Graham
Librarians rule, Ook!
|
|
|
|
|
Hi,
The following works with a static and is fully recursive
void ShowTree(int iSize)
{
if(iSize > 0)
{
static int ilevel = 0;
ShowTree(-(++ilevel)); std::cout << std::endl;
ShowTree(--iSize);
ShowTree(-(--ilevel)); std::cout << std::endl;
}
else if(iSize < 0)
{
ShowTree(++iSize);
std::cout << "*";
}
}
Learn from the mistakes of others, you may not live long enough to make them all yourself.
|
|
|
|
|
hello for all
how can i store numberic result (int, float any number) in string varibales
|
|
|
|
|
There are a number of ways, depending which type of string you are using.
For std::string
#include <sstream>
std::ostringstream temp;
temp << value;
std::string result(temp.str());
</sstream>
For CString
CString str;
str.Format("%.1f", floatValue);
str.Format("%d", intValue);
For c null terminated strings use:
char str[50];
_snprintf(str, 50, "%.1f", floatValue);
_snprintf(str, 50, "%d", intValue);
sprintf can also be used in the last example but it is less safe than _snprintf (it can lead to buffer overruns with the supplied buffer isn't large enough)
Graham
Librarians rule, Ook!
|
|
|
|
|
int s=0;
int result[50];
string ggg[50];
i want stror result[s] in ggg[s]
|
|
|
|
|
std::ostringstream temp;
temp << result[s];
ggg[s] = temp.str();
Graham
Librarians rule, Ook!
|
|
|
|
|
thank you very much
modified on Sunday, June 8, 2008 3:04 AM
|
|
|
|
|
sofia_111 wrote: string ggg[50];
Are you sure this is what you want?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i want compute result then store it in string but ok it excuted
thank you for all
|
|
|
|
|
but you're creating 50 strings ! not a string of 50 chars !! are you aware of this ?
|
|
|
|
|
Is it possible to convert a full c++ program into code?? And no, i am not rtrying to steal a program, its for my computer science course. I have a program, but i want to covert it to code, so i can 'fix' their code without rewriting the whole thing, cause my comp randomly sucks
"The beauty of Grace is that it makes life not fair" Relient K, Be my Escape
|
|
|
|
|
Interesting.
I went through your previous posts and it's quite clear that you have no clue about programming beyond a "Hello World" application. Now you want to decompile/disassemble an existing program (which is illegal by the way) and make us believe that it is an assignment from your computer course?
Even if you had permission to disassemble that program, it takes a very skilled person to go through 100000 lines of assembly code to figure out what each statement does.
Also, what does this mean:
Pathetic wrote: cause my comp randomly sucks
??
|
|
|
|
|
It means that my c++ program has a major error, and it randomly refuses to compile anything at all. And No, I am not a major illegal cheater. I am trying to recover the SPLATT!!! programs code throught the program
"The beauty of Grace is that it makes life not fair" Relient K, Be my Escape
|
|
|
|
|
Usually yes. However it is a daunting task, see http://en.wikipedia.org/wiki/Reverse_engineering[^] for some general info.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Try to complete you cource with hard work,not in this way.
-@SuDhIrKuMaR@-
|
|
|
|
|
Yea, but i am supposed to be 100% finished tommorrow
"The beauty of Grace is that it makes life not fair" Relient K, Be my Escape
|
|
|
|
|
how can i add two integers(numbers) without using + operator,rather by doing bit manupolation???
|
|
|
|
|
May I ask why you want to do that?
|
|
|
|