Jump to content

nested loop

- - - - -

  • Please log in to reply
10 replies to this topic

#1
za7ef

za7ef

    Newbie

  • Members
  • Pip
  • 7 posts
Hi guys

if any one can help me with this
how can i write a program by nested loop and make it look like this :::
enter a number >
5
A
Ab
AbC
AbCd
AbCdE

Edited by za7ef, 07 April 2010 - 12:57 PM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Are you seriously using font that big? This topic should be locked.

#4
za7ef

za7ef

    Newbie

  • Members
  • Pip
  • 7 posts
I am a beginner on c++ .
I spent more than 5 hours just to write the program below .
If any one can help with the program above please .


#include <stdio.h>
#include <stdlib.h>
int main() {
    int i,m,j;
    
    scanf("%d",&m);
            for (i=1;i<=m;i++)
            {
            printf("\n");
            for (j=1; j<=i; j++)
            {
            printf("%d",j);
            }
             }
printf("\n");
system ("pause");
return 0;}

Edited by ZekeDragon, 08 April 2010 - 12:36 AM.
Please use [code] tags (the # button) when posting code.


#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
That should output:

1
12
123
1234
12345

Now, do you know how to work with characters?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
za7ef

za7ef

    Newbie

  • Members
  • Pip
  • 7 posts
i tried but it did't work with me
even if know how it work
how can make it like that
A
Ab
AbC

As you can it is capital letter and after that small
i think that is impossible

#7
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
It is not impossible...
In your for loop you can just have a if statement to check whether it should be capital or small letter by using a int variable and armed with that information add or subtract the ascii difference of small and capital letters...

like this:


#include <stdio.h>

#include <stdlib.h>

int main()

{

	int i,m,j;

	int boolean = 1;

	scanf("%d",&m);

	for (i=1;i<=m;i++)

	{

		printf("\n");

		for (j=1; j<=i; j++)

		{

			if(boolean == 0)

			{

				printf("%c",j+96);

				boolean = 1;

			}

			else

			{

				printf("%c",j+64);

				boolean = 0;

			}

		}

		boolean = 1;

	}

	printf("\n");

	return 0;

}


Hope this clarifies everything... and if it doesn't, please ask more questions. ^_^

#8
za7ef

za7ef

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you, Thank you, Thank you, Thank you .......

lintwurm
Man, you are the best of the best in c++ programs.
you are Genius.
you see without you I will take mabye around 5 to 10 years to solve this problem.

After knowing how to solve this kind of problems.
I think know I understand chapter 6 and 7 in ICS 103.

Again, thank you . . .


#9
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
uhm thanks I guess, but most of the users here know way more than me.

anyway, did you figure out why adding 96 and 64 gives you capital and small letters?

#10
za7ef

za7ef

    Newbie

  • Members
  • Pip
  • 7 posts
Yes.
Because this is a constant thing in c++
From 65 to 90 it will give us a capital letters (A - Z) and
From 97 to 122 it will give us a small letters (a - z)
Actually,I did not know these things before, I understande it from you know thanks to you.
First, j=1 so by adding 1 to 64 will gives us (A), then, j++ so by adding 2 to 96 that will give us (b) and so on ....

I also understand how did it start with the caital letter followed by a small letter
its all about ((boolean)) it organized everything.I think that is very smart thing from you


#11
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

za7ef said:

Because this is a constant thing in c++
This actually has nothing to do with C++, instead this involves ASCII, which does have A-Z between 65 and 90, and a-z between 97 and 122. Depending on ASCII is not platform independent, which in your case may be just fine, but just keep that in mind. I would have done the following:
void print_letters_to(char max)
{
    const int difference = 'a' - 'A';
    if (max >= 'a' && max <= 'z') max -= difference;

    if (max >= 'A' && max <= 'Z')
    {
        for (char i = 'A'; i <= max; ++i)
        {
            for (char j = 'A'; j <= i; ++j)
            {
                putc(j + (!(j % 2) ? difference : 0), stdout);
            }
            putc('\n', stdout);
        }
    }
}

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users