Thread: Need help for my assignment .

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Need help for my assignment .

    Code:
    // FOP assignment.
    //
    
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <conio.h>
    
    
    int main(void)
    {
        int a,i,j;
        int choices;
        int num;
        int language;
    
    
        printf("For English press 1\n");
        printf("Untuk Bahasa Malaysia tekan 2\n");
        printf("Answer/Jawapan: ");
        scanf("%d", &language);
    
    
        //English
        if(language == 1)
        {
            printf("\nChoose the shape of your choice");
            printf("\n1.Triangle\n2.Rectangle\n3.Diamond\n");
            printf("\nAnswer: ");
            scanf("%d", &choices);
            printf("\nEnter the size of the shape you want\n");
            printf("\nAnswer: ");
            scanf("%d", &num);
        }
    
    
        //Malay
        if(language == 2)
        {
            printf("\nSila pilih bentuk pilihan anda");
            printf("\n1.Segi Tiga\n2.Segi Empat Tepat\n3.Berlian\n");
            printf("\nJawapan: ");
            scanf("%d", &choices);
            printf("\nSila masukkan saiz bentuk yang anda mahu\n");
            printf("\nJawapan: ");
            scanf("%d", &num);
        }
    
    
        // This is for Triangle
        if(choices == 1)
            for (i = 1; i <= num; i++)
        {
            for (j = 1; j <= i; j++)
            {
                printf("*");
                printf(" ");
            }
            printf("\n");
        }
        printf("\n");
    
    
        // This is for Rectangle
        if(choices == 2)
            for (i = 1; i <= num; i++)
        {
            for (j = 1; j <= num; j++)
            {
                printf("*");
                printf(" ");
            }
            printf("\n");
        }
        printf("\n");
    
    
        // This is for Diamond
        if (choices == 3)
         for (i = 1; i <= num; i++)
         {
     {
         for ( j = num; j >= i; j--)
         {
             printf(" ");
         }
         for ( j = 1; j <= i; j++)
         {
             printf(" *");
         }
         {
             printf("\n");
         }
     }
         for ( i = num; i >= 1; i--)
     {
         for ( j = num; j >= i; j--)
         {
             printf(" ");
         }
         for ( j = 1; j <= i; j++)
         {
             printf(" *");
         }
         {
             printf("\n");
         }
             
     }
         getch();
         }
         getch();
    }
    Is there something wrong ? I couldn't get my diamond shape right..

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It looks very much to me like you have inserted closing braces willy-nilly in order to balance out an inappropriate use of opening braces, and so your "flow control" is all flow, no control. Eg, not only is your diamond shape wrong, but this program will never end unless you kill it:

    Code:
         for ( i = num; i >= 1; i--)
    Is inside this block:

    Code:
         for (i = 1; i <= num; i++)
    So first iteration of the outside loop, i == 1. For infinite subsequent iterations, i == 2.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    I didn't try to compile it, but it just looks like there's a misplaced set of curly braces. If you move them them, indent, and delete other superfluous braces, you get something like this:
    Code:
        // This is for Diamond
        if (choices == 3)
        {
           for (i = 1; i <= num; i++)
           {
               for ( j = num; j >= i; j--)
                   printf(" ");
               for ( j = 1; j <= i; j++)
                   printf(" *");
               printf("\n");
           }
           for ( i = num; i >= 1; i--)
           {
               for ( j = num; j >= i; j--)
                   printf(" ");
               for ( j = 1; j <= i; j++)
                   printf(" *");
               printf("\n");
           }
       }
    What's it look like?

    BTW, it would be helpful to use variables like row instead of i and column instead of j to make it more understandable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment 2
    By jpjpjpjp in forum C Programming
    Replies: 4
    Last Post: 02-10-2010, 07:04 AM
  2. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  3. need help for assignment,Please!!!
    By jojo13 in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2008, 05:11 AM
  4. help with assignment
    By robasc in forum C# Programming
    Replies: 7
    Last Post: 04-18-2005, 12:02 AM