LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2009, 03:28 PM   #1
guitarstorm
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
Help needed with a while loop.


Pretty much i'm trying to write a while loop (for a class assignment) to print a number pattern like this...

1 2 3 4 5 6 7 8

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

This is the code i have... For the example above, i would type in 5 for m and 8 for n...

i=1;
while (i<=m)
{
printf("\n");
j=1;
k=n;
while (j<=k)
{
printf("%d", k);
k--;
}
printf("\n");
i++;
}

But when i use this code, i get this result:

87654321

87654321

87654321

87654321

87654321

My outer loop is fine, since 5 lines print, but i can't figure out what i'm doing wrong with my inner loop... I want to get it so that the numbers in each line go up from 1, and also the numbers in each successive row decrease, like the pattern i showed at the top. Any help will be appreciated. Thanks!
 
Old 03-01-2009, 04:16 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Your sample code is incomplete ... and maybe you want to consider
a for-loop instead?
Code:
{ for( i=8; i>=4; i--){for(j=1;j<=i;j++){printf "%s ",j};printf "\n\n" }}
 
Old 03-01-2009, 05:20 PM   #3
guitarstorm
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Tinkster View Post
Your sample code is incomplete ... and maybe you want to consider
a for-loop instead?
Code:
{ for( i=8; i>=4; i--){for(j=1;j<=i;j++){printf "%s ",j};printf "\n\n" }}
I would but i'm supposed to use a while loop for that part of the assignment...
 
Old 03-01-2009, 05:37 PM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
In pseudo code, why don't you do something like:

Code:
i = 8
while i >= 1
    j=1
    while j <= i
        print j
        j = j+1
    end inner while loop
    print newline
    i = i-1
end outer while loop
You could use a for loop for either the inner or outer loop if you wanted

Last edited by billymayday; 03-01-2009 at 05:41 PM.
 
Old 03-01-2009, 05:57 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by guitarstorm View Post
I would but i'm supposed to use a while loop for that part of the assignment...
Code:
{ i=8; while( i>=4 ){ j=1; while( j<=i ){ printf "%s ",j; j++ };printf "\n\n";i-- }}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 11:27 PM
nested loop in Perl help needed Grafbak Programming 9 12-19-2006 11:28 AM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 06:17 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration