Help!!!

This is a discussion on Help!!! within the C Programming forums, part of the General Programming Boards category; Hi However's out there! First of all I need to point out that I am not a lazy bum. I ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Help!!!

    Hi However's out there!

    First of all I need to point out that I am not a lazy bum. I work full-time working for Nokia and am doing a MSc in Computer Science Conversion Course part-time (this means thta my Honours Degree was in Business and I have decided to do a completed different masters course to broaden my horizons). I have attended all my lectures (3 so far in C Programming) (and have spent considerable time out of lectures) but I already have an assignment that I am having problems with (it is very hard for me but probably really easy for you guys and I haven't been tought the subject area - they seem to do this on masters courses - maybe I should have choosen a subject that came more naturally to me such as business, but I really want to get into computing).

    My problem is;

    Part a)

    Write a program that outputs a triangle to the screen. the user should be able to input the height of a triangle which should be some number between 1 and 20 inclusive. For example, if the user inputs a height of 5, the output should be;


    Code:
          *
         ***
        *****
       *******
      *********
    Hint: Use a nested loop structure which minimises the number of printf() statements. Count the number of non-printing as well as the asterisk characters.

    Part b)

    Calculate the value of 'pi' from the infinite series;

    Code:
             4   4   4   4   4
    pi = 4 - - + - - - + - - - + ......
             3   5   7   9   11
    The program should then print to screen two tables, each line being a calculated pi value followed by the number of terms used to calculate the value. The first table should be 10 lines which list the values of pi approsimated by 1 term of this series, by two two terms, by three terms and so on for the first 10 terms. the second table should print just two lines, depicting the first occurnace of 3.14 and 3.141 and their corresponding number of terms to get that degree of precision.

    To some of you this may seem really, really easy. I am sure others of you have been in this same position as me. I do not expect you to complete the whole code, I want to learn at the same time, but some pointers of what to go for for both parts of the questions would be brilliant.

    I am sorry I have to clog up your message board with useless information and questions and please do not think of me as the usual lay-about student.

    Thank you for you time, in not only reading this long message, but for hopefully helping me become one of you; an expert in C Programming.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Clue 1
    Code:
    for ( i = 0 ; i < 20 ; i += 2 ) {
        printf( "Number of stars in this row = %d\n", i );
    }
    Now use the value of i to
    - decide how many spaces to print
    - how many stars to print


    Clue 2
    Code:
    int sign = 1;
    for ( i = 0 ; i < 10 ; i++ ) {
        printf( "sign=%d\n", sign );
        sign = sign * -1;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Cheers for your tips, but I still really am having trouble, I have now spent over 20 hours on the first question and I am still not having any luck. By the way the triangle shouldn't look like it does up the top, I did put spaces either side of it but they were deleted when I posted the question. Si it should have equal amount of spaces either side.

    Can anyone help me? The whole nested loop structure and C-Programming in general is really hard for me.

    Cheers

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well post what you have then.

    A nested loop is simply
    Code:
    for ( a ; b ; c ) {
        for ( d ; e ; f ) {
        }
    }
    Replace a to f with whatever
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Part b)

    Calculate the value of 'pi' from the infinite series;

    4 4 4 4 4
    pi = 4 - - + - - - + - - - + ......
    3 5 7 9 11
    Are we talking about a power series?


    Here is an example to calculate, rather *estimate* the distance between two points.

    Code:
    #define MIN(a,b) (if (a<b)?a:b)
    
    
    void FastDist(int x,int y)
    {
      x=abs(x);
      y=abs(y);
      int mn=MIN(x,y)
      return (x+y-(mn>>1)-(mn>>2)+(mn>>4));
    }
    Perhaps that example will help.
    Last edited by VirtualAce; 10-18-2003 at 05:35 AM.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    Well, for the triangle one, I would use recursion (a function calling itself). Create a traingle() function, probably one that takes the height as an argument, and calculate and output the number of *s needed, then decrement the height and call the triangle function again. Remember to use a conditional (an if() statement) to check whether the function needs to be called again, otherwise the function will continue forever.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    @buba:
    What are you calculating there?
    The distance between x and y would be abs(x-y).
    Also I don't see what that has to do with calculating pi.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Code:
    int i,j;
    int height=5;
    
    for(i=0; i<height; i++)
    {
       for(j=0; j<...; j++)
          printf(" ");
      
       for(j=0; j<...; j++)
          printf("*");
    
       printf("\n");
    }
    Now you just have to replace the first ... with the number of spaces to be printed and the second ... with the number of *.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    How would I change to put the number of stars needed and the number of spaces (bearing in mind their should be an even amount of spaces either side of the stars)?

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Re: Help!!!

    You wrote in your first post:
    Originally posted by Marc Sharp
    I do not expect you to complete the whole code, I want to learn at the same time, but some pointers of what to go for for both parts of the questions would be brilliant.
    But now you want a complete code for the first question.

    You don't need to output the spaces on the right side of the tiangle since they are not visible anyway.

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    I know I said that, I keep on trying and trying, but I haven't even got a clue where to start, let alone finish. I had a couple of other questions that involves working out the marks for students, etc. I was fine with that but I can't even get going on this. I have tried and spent hour upon hour trying but to no luck. I haven't even got any good code to start with to show you. I am sorry, I really am, but could somebode give me any more help. If not do not worry.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    This is my code so far;
    Code:
    /*
    Program: triangle.c.
    Using nested for loops to draw a triangle.
    */
    #include<stdio.h>
    
    main ()
    {
    	int i, j ;
    
    	for (i = 1 ; 1 <= 20 ; i++ )
    	{
    		for (j = 1 ; j <= i ; j++ )
    		{
    			if ( i > 39 - j )
    							{
    				printf("*") ;
    			} 
    			else
    			{
    				printf(" ") ;
    			}
    		}
    		printf("\n") ;
    	}
    }


    If you compile this then you see that it is an infinite loop also I think the number of stars will be wrong and the number of spaces is incorrect. For example if the height is 1 then their should be 19 spaces to the left of 1 star, if the height was 10, then their would be 10 spaces to the left and 19 stars. And if the height was 20, then their would be no spaces and 39 stars. I can't seem to get this part of it. I am also unclear where to put the text; 'Please input the height of a triangle between 1 and 20'. I know it should be one of the print statements or do you think I should declare it somewhere else.

    Your help would be much appreciated.
    Code:
    /*

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Like so, but only because you seem to be making a big deal out of it
    Code:
    #include <stdio.h>
    
    void pyramid ( void ) {
        int height = 10;
        int stars_per_row = 1;
        int i, j;
        for ( i = 0 ; i < height ; i++ ) {
            int spaces = height - stars_per_row / 2;
            for ( j = 0 ; j < spaces ; j++ ) printf(" ");
            for ( j = 0 ; j < stars_per_row; j++ ) printf("*");
            printf("\n");
            stars_per_row += 2;
        }
    }
    
    void pi ( void ) {
        double  denominator = 1;
        double  sign = 1;
        double  result = 0;
        int     i;
        int     num_iterations = 20;
        for ( i = 0 ; i < num_iterations ; i++ ) {
            result = result + ( 4.0 / denominator ) * sign;
            printf( "Result=%f, denominator=%f, sign=%f\n",
                result, denominator, sign );
            denominator += 2;
            sign *= -1;
        }
    }
    
    int main ( ) {
        pyramid();
        pi();
        return 0;
    }
    You start by giving meaningful names to things, like stars_per_row and height - and not in something anonymous like i,j etc. Just use those for actually counting things.
    Then you also use additional variables like spaces to hold the intermediate answer instead of some 'magic' like i-39.


    Oh, and the &#91;code][/code] tags go around the code itself, it's not good enough just to put a &#91;code] into your post just to make the alert go away.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Salem,

    You are like a God looking down on me from programming heaven. I really do appreciate your help and guidance.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21