Thread: Displaying a table using nested loops

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Displaying a table using nested loops

    OBS! no need for nested loops as it seems!

    Hi, we got this assignment from the teacher and still after trying and trying and reading I cant solve it. The mission is to display this(ASCII table):




    the problem is in the looping itself.

    As you see it is 6 rows and 8 columns and as Iunderstand it I need to make the program read from left to right at each line.


    this is the code I am playing with right now .. any help if truly very HELPFUL!

    Code:
    #include <stdio.h>
     
    int main()
    {
    
    char ch='h';
    
    int j;
    int i;
    
    for(i=1; i<=6;  i++)
    {
       
    	for(j=32; j<=78; j++)
    	{	
    	
    		printf(" %#o ", j );
    	}
    
    					
    printf("\n");
    }	return 0;
    
    }
    I have tried to put the 32-78 in the outher loop but it doesnt work


    /Kalle
    Last edited by leviterande; 09-22-2009 at 10:04 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Try to come up with an algorithm. The inner loop goes column-wise, the outer loop goes row-wise while incrementing "ch" thro' every pass of the inner loop and displaying the "ch" value in octal and as a character.

    Hint: octal 40 stands for the ASCII space character ' ' not 'h'.
    Last edited by itCbitC; 09-22-2009 at 09:43 AM.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by itCbitC View Post
    Try to come up with an algorithm. The inner loop goes column-wise, the outer loop goes row-wise while incrementing "ch" thro' every pass of the inner loop and displaying the "ch" value in octal and as a character.

    Hint: octal 40 stands for the ASCII space character ' ' not 'h'.


    thank you for the help, I am aware about the relation between octal,decimal and character.. that is not hte hard part. I have already displayed the whole table of the ASCII but simply it is just not in a "table form"

    for instance I can do this:

    Code:
    #include <stdio.h>
     
    
    
    int main()
    {
    
     char ch='h';
     
    int j;
    int i;
    
    
    
    for(i=32; i<=78;  i++)
    {
       
    	printf("%#o %c\t",i,i);
    
    
    }	return 0;
    }
    Last edited by leviterande; 09-22-2009 at 09:55 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So at some point you're going to have to think. How do you want your table to print? If you are going to insist on nested loops (I don't know why you would, but that's your title) then you need to think what is the purpose of the outer loop (which will tell you (a) what the start-stop numbers are going to be and (2) what goes inside it) and what is the purpose of the inner loop (which will tell you ditto).

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    So at some point you're going to have to think. How do you want your table to print? If you are going to insist on nested loops (I don't know why you would, but that's your title) then you need to think what is the purpose of the outer loop (which will tell you (a) what the start-stop numbers are going to be and (2) what goes inside it) and what is the purpose of the inner loop (which will tell you ditto).

    sorry, I could be very wrong.. I shoudnt use any nested loops .. the teacher confused us. I checked now and there are nothing about using nested loop for the solution. it says only printf

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Did you even read my previous post? You can do it with just one loop if you break onto a new line at column number 8.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by itCbitC View Post
    Did you even read my previous post? You can do it with just one loop if you break onto a new line at column number 8.


    How does one break onto a new line?
    Last edited by leviterande; 09-22-2009 at 10:32 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by leviterande View Post
    How do one break onto a new line?
    Print a new line character.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Print a new line character.
    ah, you just mean simple newline, hmmm I think we missunderstand eachother quite a bit actually

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    I still do not undestand what to change. should it be a nested loop in the first place? I am a newbie into programming

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by leviterande View Post
    I still do not undestand what to change. should it be a nested loop in the first place? I am a newbie into programming
    When you want to move to a new line, you need to print a new-line character. The decision on whether to do so is a decision (i.e., an if) and so a nested loop is not involved.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    I am still trying figure it out:



    /*Lab3 Assgnment3. using advanced nested loops to show a ASCII table */


    Code:
    #include <stdio.h>
     
    
    
    int main()
    {
     
     
    
    int number;
    
    
    for(number=32; number<=78; number++)
    
    
    {
    
    if((number) == (number+8))
    {
    printf("\n");
    }
    printf(" %#o %c",number, number );
    }
    
    
    
        return 0;
    
    }

    it still doesnt work, I dont get any compiling erorrs but the if statement doesnt have effects on the code at all?!

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would the number ever equal itself + 8. That's impossible math right there.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by leviterande View Post
    I am still trying figure it out:
    it still doesnt work, I dont get any compiling erorrs but the if statement doesnt have effects on the code at all?!
    Instead of repeating what I just said 5 minutes ago, I'll just post a link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 10:16 AM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  5. Displaying a table
    By hkguy in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2004, 07:49 AM

Tags for this Thread