Thread: Help to solve My easy C programing questions!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Red face 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.

    (Answer Question 1)

    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.
    for linked lists.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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
    [edit]Which is actually very close (but not quite) to what you already have now that I look at it.[/edit]
    Last edited by hk_mp5kpdw; 04-27-2005 at 01:10 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    Thanks hk_mp5kpdw, will try to do it,
    So anyone know, question 2, question3,and question4?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    quit doing other peoples homework piano
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    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. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    My question2

    Hello! Please help me!

    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. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote 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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    6
    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. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    MosunPinkie, I have moved your posts here.

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Easy Questions
    By CougarElite in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2005, 09:58 PM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  3. Double buffering in GDI -- easy?
    By fusikon in forum Game Programming
    Replies: 17
    Last Post: 02-15-2003, 10:03 PM
  4. Asking Questions About Homeworks
    By ammar in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-04-2002, 11:17 AM
  5. FAQ: Examples of good questions (General)
    By Prelude in forum FAQ Board
    Replies: 1
    Last Post: 10-11-2002, 08:57 PM