Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!   (85,502 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption 2

    2. Red Dead Redemption

    3. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

Sign in to follow this  
Moestry

For Loop in C

Recommended Posts

Moestry

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.

Edited by Moestry

Share this post


Link to post
Share on other sites
Parik
#include <stdio.h>intmain(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>intmain(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.

Edited by Parik

Share this post


Link to post
Share on other sites
Moestry

Thank you :D

Share this post


Link to post
Share on other sites
K^2

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.

Share this post


Link to post
Share on other sites
Gian_Yagami

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++;   }}

Share this post


Link to post
Share on other sites
K^2

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);}

Share this post


Link to post
Share on other sites
Gian_Yagami

 

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++;			}		}	}}
Edited by Gian_Yagami

Share this post


Link to post
Share on other sites
Gian_Yagami

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.