Help for our project

This is a discussion on Help for our project within the C Programming forums, part of the General Programming Boards category; Originally Posted by ervinako Code: #include<stdio.h> int main () { int row1, x; printf("Row1 :"); for(row1 = 2; row1 > ...

  1. #16
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by ervinako View Post
    Code:
     
    #include<stdio.h>
    
    int main ()
    {
        int row1, x;
    
        printf("Row1 :");
    
        for(row1 = 2; row1 > 0; --row1)
            {
                for(x = 0; x < row1; ++x)
                printf("\tI");
            }
    
        return 0;
    }
    I got it is this what you trying to say?..
    Code:
            printf("Row 1: ");
            for(row1 = 2; row1 > 0; --row1)
                {
                    for(x = 0; x < row1; ++x)
                    printf("I");
                }
            printf("Row 2: ");
            for(row2 = 3; row2 > 0; --row2)
                {
                    for(y = 0; y < row2; ++y)
                    printf("I");
                }
            printf("Row 3: ");
            for(row3 = 4; row3 > 0; --row3)
                {
                    for(z = 0; z < row3; ++z)
                    printf("I");
                }
    THIS PRINT:

    Row1: III Row2: IIIIII Row3: IIIIIIIIIIII

    why?..

  2. #17
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Because you don't use any newlines.
    printf("\nRow 2: ");
    ...
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #18
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by oogabooga View Post
    Because you don't use any newlines.
    printf("\nRow 2: ");
    ...
    Code:
     printf("\nRow 1: ");
            for(row1 = 2; row1 > 0; --row1)
                {
                    for(x = 0; x < row1; x++)
                    printf("I");
                }
            printf("\nRow 2: ");
            for(row2 = 4; row2 > 0; row2--)
                {
                    for(y = 0; y < row2; y++)
                    printf("I");
                }
            printf("\nRow 3: ");
            for(row3 = 5; row3 > 0; --row3)
                {
                    for(z = 0; z < row3; ++z)
                    printf("I");
                }
    This print:

    Row1: I I I
    Row2: I I I I I I I I I I I //this must be 5 only.
    Row3: I I I I I I I I I I I I I I I //this must be 7 only.

    HELP!.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    For starters, you need to indent your code properly, e.g.,
    Code:
    printf("\nRow 1: ");
    for(row1 = 2; row1 > 0; --row1)
    {
        for(x = 0; x < row1; x++)
            printf("I");
    }
    printf("\nRow 2: ");
    for(row2 = 4; row2 > 0; row2--)
    {
        for(y = 0; y < row2; y++)
            printf("I");
    }
    printf("\nRow 3: ");
    for(row3 = 5; row3 > 0; --row3)
    {
        for(z = 0; z < row3; ++z)
            printf("I");
    }
    Next, why do you have those nested loops? Your problem is that your loop logic is wrong: you need to be clear as to why those loops are there.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by laserlight View Post
    For starters, you need to indent your code properly, e.g.,
    Code:
    printf("\nRow 1: ");
    for(row1 = 2; row1 > 0; --row1)
    {
        for(x = 0; x < row1; x++)
            printf("I");
    }
    printf("\nRow 2: ");
    for(row2 = 4; row2 > 0; row2--)
    {
        for(y = 0; y < row2; y++)
            printf("I");
    }
    printf("\nRow 3: ");
    for(row3 = 5; row3 > 0; --row3)
    {
        for(z = 0; z < row3; ++z)
            printf("I");
    }
    Next, why do you have those nested loops? Your problem is that your loop logic is wrong: you need to be clear as to why those loops are there.
    Cause i want to print

    Row1: I I I
    Row2: I I I I I
    Row3: I I I I I I I

    other user suggested to use loop than using arrays.
    mmm for the game intruction it is in the 1st page..

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by ervinako
    other user suggested to use loop than using arrays.
    Yes, and that is a correct approach. However, your nesting of loops and general loop logic is incorrect. I would expect something along the lines of:
    Code:
    for i = 0 until i == number of lines to print
        print "I I I "
        for j = 0 until j == 2 * i
             print "I "
             increment j
        print newline
        increment i
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by laserlight View Post
    Yes, and that is a correct approach. However, your nesting of loops and general loop logic is incorrect. I would expect something along the lines of:
    Code:
    for i = 0 until i == number of lines to print
        print "I I I "
        for j = 0 until j == 2 * i
             print "I "
             increment j
        print newline
        increment i
    Meaning like this?..

    Code:
    for(row1 == 0; row1 == 3 ; row1++)
                {
                    printf("\tI I I");
    
                        for(x = 0; x == 2 * row1)
                        {
                            printf("\tI");
                            x++;
                            printf("\n");
                            row1++;
                        }
                }

  8. #23
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Anyone what will i do next?..

    Code:
    #include <stdio.h>
    main()
    {
        int row1;
        int row2;
        int row3;
        int j; int i=1;
        int item; int l;
        int row; int player=1;
        int column; int sum;
    
    
    
    
    
    
        while (i<sum)
        {
            printf("\nPlayer %d's turn! \n\n",player);
            player++;
    
    
            if(player == 3)
                player = player - 2;
    
        printf("\nRow 1: ");
            for(row1 = 0; row1 < 3; row1++)
                {
                    printf("\t|");
                }
        printf("\nRow 2: ");
            for(row2 = 0; row2 <5; row2++)
                {
                    printf("\t|");
                }
        printf("\nRow 3: ");
            for(row3 = 0; row3 <7; row3++)
                {
                    printf("\t|");
                }
    
            printf("\nEnter row number: ");
            scanf("%d", &row);
    
    
            if(row > 3 || row < 1)
            {
                printf("Error! Enter number from 1-3!\n");
    
    
                while (row > 3|| row < 1)
                {
                    printf("Enter row number: ");
                    scanf("%d", &row);
                }
            }
    
    
            printf("Enter number of items to remove: ");
            scanf("%d", &item);
    
    
            switch(row)
            {
                case 1:
                {
                    if(item>3||item<1)
                        printf("Error! Enter number from 1-3!\n");
    
    
                    while(item>3||item<1)
                    {
                        printf("Enter number of items to remove:");
                        scanf("%d", &item);
                    }
                }
    
    
                break;
    
    
                case 2:
                {
                    if(item > 5|| item < 1)
                        printf("Error! Enter number from 1-5!\n");
    
    
                    while(item > 5 || item < 1)
                    {
                        printf("Enter number of items to remove:");
                        scanf("%d", &item);
                    }
                }
    
    
                break;
    
    
                case 3:
                {
                    if(item > 7 || item < 1)
                        printf("Error! Enter number from 1-7!\n");
    
    
                    while(item > 7 || item < 1)
                    {
                        printf("Enter number of items to remove: ");
                        scanf("%d", &item);
                    }
                }
    
    
                break;
    
            };
    
            sum= i* 15;
    
        }
    
        return 0;
    }

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have never heard of the Game of Nim, but please specify what your problem is, without referring to the Nim Game, OK?

  10. #25
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    for(row1 == 0; row1 == 3 ; row1++)
                {
                    printf("\tI I I");
    
                        for(x = 0; x == 2 * row1)
    I think you should read about how for-loops work in C.

    Bye, Andreas

  11. #26
    Registered User
    Join Date
    Oct 2012
    Posts
    59
    Quote Originally Posted by Adak View Post
    I have never heard of the Game of Nim, but please specify what your problem is, without referring to the Nim Game, OK?
    Can you visit my latest thread?


    PS: LOCK THIS THREAD.

  12. #27
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Here -> Game of Nim (PROJECT HELP!)
    And closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes Project (Not class project)
    By adam.morin in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2011, 12:48 AM
  2. Paid project - The Free Marketing Project
    By sharefree in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 10-27-2010, 02:15 PM
  3. Project Help please!
    By black_hole??? in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2009, 12:55 AM
  4. C project
    By 666cos in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 08:32 AM
  5. your 11th & 12th grade c++ project or similar project
    By sleepyheadtony in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 01-13-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21