Quantcast

Jump to content

» «
Photo

For Loop in C

7 replies to this topic
Moestry
  • Moestry

    Player Hater

  • Members
  • Joined: 13 Jan 2017
  • United-Kingdom

#1

Posted 13 January 2017 - 12:18 PM Edited by Moestry, 13 January 2017 - 12:18 PM.

Hello every one , i want to do an For loop for this console output.

 

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

And i want have something like this.

0 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
6 7 8 9 10
7 8 9 10
8 9 10
9 10
10

 

It need to have 2 For Loops.

Can someone script me that in C?.

I'm new in programming and i dont know how that work

 

Thx.


Parik
  • Parik

    Transform

  • Members
  • Joined: 03 Dec 2012
  • Mars
  • Contribution Award [Chain Game]
    Best Total Overhaul 2017 Contribution [GTA: Underground]

#2

Posted 13 January 2017 - 12:44 PM Edited by Parik, 29 May 2017 - 07:48 PM.

#include <stdio.h>

int
main(void)
{
    int i;
    for(i = 1; i <= 10; i++)
    {
        int j;
        for(j = 1; j<=i; j++)
        {
            printf("%d ",j);
        }
        printf("\n");
    } 
    return 0;
}

For the second one

#include <stdio.h>

int
main(void)
{
    int i;
    for(i = 0; i <= 10; i++)
    {
        int j;
        for(j = i; j <= 10; j++)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
   return 0;
}

The first code works like this

It runs a loop for i from 1 to 10 , and prints the numbers between 1 to i.

 

The second code does the same except it prints numbers i to 10.


Moestry
  • Moestry

    Player Hater

  • Members
  • Joined: 13 Jan 2017
  • United-Kingdom

#3

Posted 13 January 2017 - 12:45 PM

Thank you :D


K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Best Poster in Technology / Programming 2017
    Best Poster [Technology / Programming] 2016
    Best Poster [Programming] 2015
    Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#4

Posted 13 January 2017 - 01:31 PM

Some important corrections to Parik's answer having to do with language, rather than algorithm. I'm honestly only adding these because question asks about C and not C/C++ in general. It might sound like nit-picking, but there are many little pedantic habits that can make you a better programmer.

1) Declaration of variable inside the for loop initializer is not always allowed. Specifically, this became a C feature only with C99, and not all C compilers have it enabled by default. This code might not compile. On the other hand, C99 and any C++ compiler should have for loops declared exactly like that.
2) C is not C++. "int main()" does not mean in C what it does in C++. Correct C definition is "int main(void)". This will compile, but can have undesired side-effects.
3) Since main is declared int, it must return a value. Any half-decent C/C++ compiler will throw a warning. Don't just ignore warnings. They are there to protect you from stupid things. Like your code terminating with an "error" because E/RAX register was not empty when main returned.

Finally, purely a style note which becomes important in C++. In modern use, the preferred increment in a loop is pre-indrecment (++i) instead of post-increment (i++). The reason has to do with operator overloading in C++ and what these two operators imply for an object with non-trivial constructor.
  • t3h PeNgU1N oF d00m, Mr.Arrow, Parik and 1 other like this

Gian_Yagami
  • Gian_Yagami

    Trick

  • Members
  • Joined: 08 Nov 2011
  • None

#5

Posted 09 January 2018 - 02:49 PM

I have shorter code:

int i;
int j = 1;

for(i=1; i<=10; i++){
   printf("%i", i);
   if(i==j){
      printf("\n");
      i = 0;
      j++;
   }
}

K^2
  • K^2

    Vidi Vici Veni

  • Moderator
  • Joined: 14 Apr 2004
  • United-States
  • Best Poster in Technology / Programming 2017
    Best Poster [Technology / Programming] 2016
    Best Poster [Programming] 2015
    Most Knowledgeable [Web Development/Programming] 2013
    Most Knowledgeable [GTA Series] 2011
    Best Debater 2010

#6

Posted 10 January 2018 - 08:24 AM

I have shorter code:

You have a bug. I recommend checking out IDE One for quick prototypes.

Also, if we're showing off, here's the second problem in 3 lines with one loop and no branches.
for (int n=24; n>2;) {
    printf("0 1 2 3 4 5 6 7 8 9 10%n\n"+24-n, &n);
}
  • Tchuck, Gian_Yagami and Parik like this

Gian_Yagami
  • Gian_Yagami

    Trick

  • Members
  • Joined: 08 Nov 2011
  • None

#7

Posted 10 January 2018 - 04:34 PM Edited by Gian_Yagami, 10 January 2018 - 04:35 PM.

 

I have shorter code:

You have a bug. I recommend checking out IDE One for quick prototypes.

Also, if we're showing off, here's the second problem in 3 lines with one loop and no branches.
for (int n=24; n>2;) {
    printf("0 1 2 3 4 5 6 7 8 9 10%n\n"+24-n, &n);
}

Oh you right, my mistake is when j var is reach 10 and i var will reset and j var turned into 11, making output count to ten shows twice.

 

I already fix this:
 

#include "stdio.h"

int main () {
	int i;
	int j = 1;

	for(i=1; i<=10; i++) {
		if(!(j > 10)) {
			printf("%i", i);
			if(i==j) {
				printf("\n");
				i = 0;
				j++;
			}
		}
	}
}

Gian_Yagami
  • Gian_Yagami

    Trick

  • Members
  • Joined: 08 Nov 2011
  • None

#8

Posted 10 January 2018 - 04:50 PM

For the second case:

#include "stdio.h"

int main () {
	int i;
	int j = 0;

	for(i=0; i<=10; i++) {
		if(i >= j) {
			printf("%i ", i);
			if(i==10) {
				printf("\n");
				i = 0;
				j++;
			}
		}
	}
}

Sorry not mean to spam with double post, but I like solving case like these.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users