Thread: Need help with the output plz

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    65

    Need help with the output plz

    I remember someone else had a homework problem that they wanted us to solve. In search to find something to do I decided to attempt it. I have worked about 90% of it and would like help finishing it. I would post it back into the original thread but I dont want to do his/her homework and figure that he does not read the forums and just simply wanted an answer.
    The person's homework was to have an output read
    1
    22
    333
    4444
    55555
    4444
    333
    22
    1

    well check out my code then I will ask my question.
    Code:
    #include <stdlib.h>
    
    main()
    {
    int i,x,x1,d;
    for(i=0;i<=5;++i)
    {
    for(x=0;x<i;++x)
    {
    printf("%i",i);
    }
    }
    if(i=4)
    {
    for(;i>0;i--)
    {
    for(x=0;x<i;++x)
    {
    printf("%i",i);
    }
    }
    }
    return(0);
    }
    I have it down to output 1223334444555554444333221 and would like help on what conditional to have the newline come into play, I have tried different clauses but no luck. There has to be one person to steer me into the right place. Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mmm, you could try indenting it.

    Also maybe compile it with more warnings enabled to spot the rather dubious if() statement.
    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.

  3. #3
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    Code:
    #include <stdio.h>
    
    #define HEHE 5
    
    int main()
    {
    	int i;
    	int j;
    	for (i = 0; i < HEHE; i++)
    	{
    		for (j = 0; j <= i; j++)
    		{
    			printf("%d", i + 1);
    		}
    		printf("\n");
    	}
    	for (i = HEHE - 1; i >= 0; i--)
    	{
    		for (j = 0; j < i; j++)
    		{
    			printf("%d", i);
    		}
    		printf("\n");
    	}
    	return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The whole point is to try and explain, not blurt out an answer at the first sign of trouble....
    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.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    Code:
    #include <stdlib.h>
    
    main()
    {
     int i,x,x1,d;
       for(i=0;i<=5;++i)
       {
                        for(x=0;x<i;++x)
                        {
                        printf("%i",i);
                        }
       }
     if(i=4)
     {
      for(;i>0;i--)
      {
                        for(x=0;x<i;++x)
                        {                   
                        printf("%i",i);
                        }
      }
     }
     return(0);
    }
    sorry is that better for you, and sorry I don't have the highest grammer but dubious I have no idea what that means......thanks for your help?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(i=4)
    Does your compiler complain about this?
    What about == ?
    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.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    well update I have fixed my problem. I even stoped it from \n the empty space. I kinda agree with Salem with blurting the answer, all you had to say if something along the lines of in which /n dictates the numbers value not amount. Well just to finish the thread I will post my code with my own updated tweak to it.
    Code:
    #include <stdlib.h>
    
    main()
    {
     int i,x,x1,d;
       for(i=0;i<=5;++i)
       {
                        for(x=0;x<i;++x)
                        {
                        printf("%i",i);
                        }
                                        if(i>0)
                                        {
                                        printf("\n");
                                        }
       }
     if(i=4)
     {
      for(;i>0;i--)
      {
                        for(x=0;x<i;++x)
                        {                   
                        printf("%i",i);
                        }
      printf("\n");
     
     }
    }
    scanf("%s",x1);
     return(0);
    }

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    wait salem should it? I have never had that error, I use the bloodshed Dev-C++ complier.

  9. #9
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    i'm not good at english, you may not understand the words i written,so i thinked the code is the best way to express my thought.

    sorry, i will do my best to explain , and not just post the code

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    It should at least give a warning, as it's rather clear that you want to compare, not assign.

    Maybe your compilation settings are too loose ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    I wrote out the if in my code. I would just like to know A. What is the difference between = and ==. B logicaly why that is. Please dont post use google its just rude.

  12. #12
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(10) : warning C4013: “printf”未定义;假设外部返回 int
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(29) : warning C4013: “scanf”未定义;假设外部返回 int
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(5) : warning C4101: “d” : 未引用的局部变量
    d:\my works\vc++.net 2003 works\workplace\test\test.c(29) : warning C4700: 使用的局部变量“x1”尚未初始化
    正在链接...

    in my compilation, it gives four warnings

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >A. What is the difference between = and ==.

    This is the assignment operator: =
    This is the comparison operator: ==

    Quote Originally Posted by zouyu1983
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(10) : warning C4013: “printf”未定义;假设外部返回 int
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(29) : warning C4013: “scanf”未定义;假设外部返回 int
    d:\My works\VC++.NET 2003 WORKS\Workplace\test\test.c(5) : warning C4101: “d” : 未引用的局部变量
    d:\my works\vc++.net 2003 works\workplace\test\test.c(29) : warning C4700: 使用的局部变量“x1”尚未初始化
    正在链接...

    in my compilation, it gives four warnings
    printf and scanf are in <stdio.h>, not <stdlib.h>.
    d is not used anywhere.
    x1 is an int, your scanf call uses %s with it -- %d is for ints -- and the corresponding argument should then be &x1.
    Last edited by Dave_Sinkula; 11-25-2006 at 01:48 PM. Reason: Back to back.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35

    Wink

    Quote Originally Posted by Dave_Sinkula
    >A. What is the difference between = and ==.

    This is the assignment operator: =
    This is the comparison operator: ==

    printf and scanf are in <stdio.h>, not <stdlib.h>.
    d is not used anywhere.
    x1 is an int, your scanf call uses %s with it -- %d is for ints -- and the corresponding argument should then be &x1.
    do you know chinese?

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by zouyu1983
    do you know chinese?
    Not even a word.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. oops output plz...
    By centaurs in forum C Programming
    Replies: 6
    Last Post: 07-01-2009, 06:11 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM