Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. Red Dead Redemption 2

      1. PC
      2. Gameplay
      3. Missions
      4. Help & Support
    2. Red Dead Online

      1. Gameplay
      2. Find Lobbies & Outlaws
      3. Help & Support
      4. Frontier Pursuits
    1. Crews & Posses

      1. Recruitment
    2. Events

    1. GTA Online

      1. Diamond Casino & Resort
      2. DLC
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Grand Theft Auto Series

    3. GTA 6

    4. GTA V

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

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

    7. GTA Vice City Stories

    8. GTA Liberty City Stories

    9. GTA San Andreas

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

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

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

      1. GTA Advance
      2. GTA 2
      3. GTA
    13. 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. 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. News

    2. Forum Support

    3. 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

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