# Thread: Help to solve My easy C programing questions!

1. ## Help to solve My easy C programing questions!

Below is my lecturer give me the question, i just can handle the first questio, but i think my logic thinking is not so good! , so hope some one can help me to solve, thanks!

1.)write a recursive function that takes a parameter a non negative integer and generatesthe following pattern of stars. If the non-negative integer is 4, then the pattern generated is:

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

Also write a program that prompts the user ti enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as number of lines generates the above pattern.

*Question1, i solved already, but i think the logic is not so good, so post at here to discuss.

Code:
```#include <stdio.h>
void asterisks1(int x)
{
int i;
if (x ==0 ) return;
for (i=0;i<x;i++)
printf ("*");
printf("\n");
asterisks1(x-1);
}

void asterisks2(int a,int b)
{
int i;
if (a ==b+1 ) return;
else
for (i=0;i<a;i++)
printf ("*");
printf("\n");
asterisks2(a+1,b);
}

int main()
{
int value;
scanf("%d",&value);
asterisks1(value);
asterisks2(1,value);

}```

2.) Write a program that uses a stack to print the prime factors of a positive integer in descending order using the stack.

3.) Write a programing that reads a line of text, changes each uppercase letter to lowercase and places each letter both in a Queue and onto a stack. The program should then verify whether the line of text is a palindrome.

4.)write a program to test:
a.) The selection sort algorithm.
b.) The insertion sort algorithm.

2. For the first problem...

You were asked to write 1 recursive function, not 2 of them. A better algorithm might be:

Code:
```1.  If N = 0 then exit function
2.  Else
2a.    Print N asterisks
2b.    Call function recursively with N-1 as argument/parameter
2c.    Print N asterisks```
Which is actually very close (but not quite) to what you already have now that I look at it.[/edit]

3. Thanks hk_mp5kpdw, will try to do it,
So anyone know, question 2, question3,and question4?

4. 2) sounds hard but isn't. simply try dividing the number by each integer (starting at 2) up to it's square root. ie. say 25 was the number, try 25/2, 25/3 25/4. there are no prime factors larger than the square root, in this case, 5.

if the number -think for youself-, then put it on the stack. ...think about how the numbers will come back off the stack.

5. That first problem is fun. I solved it, but then noticed the requirement to use recursion. Here's my solutions.

For the third problem, note that a queue is first-in/first-out and a stack is first-in/last-out. Also, note that a palindrome is read the same forwards and backwards.

6. quit doing other peoples homework piano

7. This is not homework lar! Just my leturer give questions only! Seldom people can solve! :P
Because all just new to start studying C programing! anyway thanks! all! , I get a lot, if cannot then will post the coidng over here! Thanks for help!

Question 4, i still don't know what is talking about?

8. ## MY question2 coding!

I did the prime number already!

Question2
find the Prime number from 1-10 coding.

Code:
```#include <stdio.h>

int main()
{
int i;
int j;
int prime;
for (i=0;i<=10;i++)
{
if(i<=1)
{
prime =0;
}
else
{
prime =1;
for (j=2;j<i;j++)

if (i%j==0)
prime =0;
}
if (prime!=0)
printf ("%d\n", i);
}
}```
After that how to store the value to the stack?
Anyone can teach me??

9. ## My question2

My second test, this is my coidng! still cannot run! T_T

Code:
```#include <stack>
#include <iostream>
using namespace std;
int i;
int j;
int prime;

int main()
{
stack<int> s;

for (i=0;i<10;i++)
{
if(i<=1)
{
prime =0;
}
else
{
prime =1;
for (j=2;j<i;j++)

if (i%j==0)
prime =0;
}
if (prime!=0)
s.push(i);
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
return 0;
}
}```

10. Originally Posted by misplaced
quit doing other peoples homework piano
Ha...but it's just too much fun.

hotwebs, it looks like this should be on the C++ forum since you're using STL now.

11. Oh:O sorry! i don't know C and C++ much! so don't know which can i post? :P sorry! please apologize! Any advanced here.?

12. 1. Don't dump multiple homework questions (especially without answers) in the same thread.
2. Don't BUMP your thread with useless "I'm still stuck" messages - that just annoys everyone.
3. Lay off the excessive use of the ! key, and try and spell better.

> i don't know C and C++ much!
I suggest you talk to your lecturer to clarify which you're supposed to be learning. At the moment, it's all a confused mess.

> while (!s.empty())
Hint - move this OUTSIDE your for loop.

13. pls u guyz should help me solve it on tym.i rily nid ur help here.dis is a very important test.10ks so much,u guyz.

14. MosunPinkie, I have moved your posts here.