Thread: My program needs less code.

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    5

    My program needs less code.

    Sorry I copied the earlier version, I should have double chk...
    this is the correct version it will compile.

    Hello I have an assignment; this is an example of what the output should be:

    Enter Grade 1: 97
    Enter Grade 2: 45
    Enter Grade 3: 76
    Enter Grade 4: 345

    Sorry, that’s an incorrect value try again…

    Enter Grade 4: 99
    Enter Grade 5: 999

    (an ‘x’ is marked for each grade range.)

    Result…

    Grade 95-100: xx
    Grade 90-94:
    etc…

    My code works it’s just that my professor wants it to be done in 11 functional lines, is this possible? If anyone has any idea I would appreciate.
    Code:
    #include <stdio.h>
    
    main()
    {
    
    int count[101], y, grade, x;
    int A=0, B=0, C=0, D=0, F=0;
    int AB=0, BC=0, CD=0, DF=0;
    
    printf(“\n\n          | Grade System |       \n”);
    printf(“-----------------------------------------------\n\n”);
    
    while (grade!=  999)
    {
    for (y=0; y<=100; ++y)
    		count[y]=0;
    
    printf(“\n   Please input Grades ‘0 – 100’, to see results or exit program type ‘999’\n\n”);
    
    for(y=0; y<=99; ++y)
    {
    printf(“Grade:   “);
    scanf(“%i”, &grade); 
    
    if (grade == 999)
    	y=100; 
    else if (grade <0 || grade >100)
    { 
    printf(“\n Sorry, that is incorrect value, to see result or exit program type ‘999’ \n\n”);
    	--y;
    }
    else 
    	++ count[grade];
    }
    
    for(y=100; y>=95; --y)
    A=A+count[y];
     for(; y>90;  --y)
    AB=AB+count[y];
     for(; y>85;  --y)
    B=B+count[y];
     for(; y>80;  --y)
    BC=BC+count[y];
     for(; y>75;  --y)
    C=C+count[y];
     for(; y>70;  --y)
    CD=CD+count[y];
     for(; y>65;  --y)
    D=D+count[y];
     for(; y>60;  --y)
    DF=DF+count[y];
     for(; y>0;  --y)
    F=F+count[y];
    
    printf(“\n 95-100:  “);
    	for(x=A; x>0; --x)
    printf(“x”); 
    
    printf(“\n 90-94:  “);
    	for(x=AB; x>0; --x)
    printf(“x”); 
    
    printf(“\n 85-89:  “);
    	for(x=B; x>0; --x)
    printf(“x”); 
    
    printf(“\n 80-84:  “);
    	for(x=BC; x>0; --x)
    printf(“x”); 
    
    printf(“\n 75-79:  “);
    	for(x=C; x>0; --x)
    printf(“x”); 
    
    printf(“\n 70-74:  “);
    	for(x=CD; x>0; --x)
    printf(“x”); 
    
    printf(“\n 65-69:  “);
    	for(x=D; x>0; --x)
    printf(“x”); 
    
    printf(“\n 60-64:  “);
    	for(x=DF; x>0; --x)
    printf(“x”); 
    
    printf(“\n 0-59:  “);
    	for(x=F; x>0; --x)
    printf(“x”); 
    printf(“\n --------------  | End Program/Results |  -------------- \n”);
    
    	}
    }
    Last edited by shish789; 05-13-2004 at 03:56 PM.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    You can compile this ?

  3. #3
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    Code:
    if 
    {
    (grade == 999)
    	y=100;
    } 
    else {if (grade <0 || grade >100)
    { 
    printf(“\n Sorry, that is incorrect value, to see result or exit program type ‘999’ \n”);
    	--y;
    }
    else 
    	{++ count[grade];}
    }
    else 
    {	++count[grade];
    }

    try this code because you have an illegal else
    in the old code

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    5
    anyone can help me bring this code to 11 functional lines

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by shish789
    anyone can help me bring this code to 11 functional lines
    Post something that compiles.

  6. #6
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Quote Originally Posted by enjoy
    Code:
    if 
    {
    (grade == 999)
    	y=100;
    } 
    else {if (grade <0 || grade >100)
    { 
    printf(“\n Sorry, that is incorrect value, to see result or exit program type ‘999’ \n”);
    	--y;
    }
    else 
    	{++ count[grade];}
    }
    else 
    {	++count[grade];
    }

    try this code because you have an illegal else
    in the old code
    No, that second else statement is an 'else if' statement. It's legal. It doesn't look pretty, but the way he had it was right (as far as the else if statement goes... I don't have a compiler here so I don't know what is causing the error...

  7. #7
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    this syntax is still bad:
    Code:
    for(y=100; y>=95; --y);
    	A=A+count[y];
    there mustn't be a semicolon after the for(..)

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    5
    I actually successfully compiled it, just forgot to change it on the forum, thanks IceBall for pointing that out. Is there anyway I can reduce the code to 11 functional lines?

    Code:
    for(y=100; y>=95; --y);
    	A=A+count[y];
    
    /*changed*/
    for(y=100; y>=95; --y)
    	A=A+count[y];

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why does it have to be 11 lines? Also, define the word line. Is this one line or two?
    Code:
    for( x = 0; x < y; x++ ) printf("%d ", x );
    It could be either. It's two statements, so does that constitute two lines, or do you mean "physical" lines? If so...
    Code:
    #include <stdio.h>
    int main( void ) { int x = 0, y = 10; for( x = 0; x < y; x++ ) printf("%d ", x ); return 0; }
    Two "lines".

    While you're at it, you may want to post the code you're now trying to shrink.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Depending on what you define as a "line", I have an eleven line answer. I skipped the "AB" "BC" crap though. It would be trivial to add it. I'll post mine when this thread isn't what I consider to be in the valid time frame of your home work.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    5
    The code I am trying to shrink is the first code up top. This is an intro to C program class I am taking, and the 'AB' 'BC' grade system is what my school goes by, so can not really skip it. As far as what a 'line' is I am not sure my professor said make your program with 11 functional lines he gave it to us as a challenge not to bring down our grade. I am not a programmer nor do I go to school to be one, so I am not sure what he means when he say functional lines. But kinda late to ask him now, that all home work is due tomm.
    Quote Originally Posted by quzah
    Depending on what you define as a "line", I have an eleven line answer. I skipped the "AB" "BC" crap though. It would be trivial to add it. I'll post mine when this thread isn't what I consider to be in the valid time frame of your home work.

    Quzah.

  12. #12
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    He wants you to post the progress you have made so far. What is the current version of the code you are working on?

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    why am i getting funny looking ' ” ' characters when i copy + paste thius code into my compiler? is ” legal? or does it have to be "
    ” " see the difference?

    when i did fix this problem and compiled it, a lot of characters were jumbled and typing 'exit' created an infinite loop.
    Last edited by sand_man; 05-13-2004 at 11:16 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe a "functional line" is something that's actually doing some work to solve the problem. For example, functional lines are colored green in the following:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int x;
        for( x = 0; x < 10; x++ )
            if( x % 3 == 0 )
                printf("x is now %d\n", x );
        return 0;
    }
    Here we have three "functional lines". The for loop would be one, because it's doing something benificial and functional. It increments and what not. The if check would be, because it also does something for us. Finally, the printf statement also is functional.

    The variable declaration, include line, the declaration of main, the { line, return and what not aren't functional. They don't do anything, but are required for it to actually be a C program.

    If you don't count variable declarations, and if you omit my buffer clearing (for people who like to input letters when they should put in numbers), then I have your program in six functional lines. If you count my error checking, I have it in seven. This includes your "AB", "DF" scenarios.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    5
    SandMan, what compiler are you using ANSI C Unix? I get that same problem, I just can not connect to the UNIX server/I get dropped all the time, so I just type it up in VS .net. Then I have to copy and edit it so my prof. does not get ........ed off at me for using MS apps. lol
    okay, thanks quzah I think I know what you mean, in MS VS .net its blue (int, for, while, etc) but printf scanf are just in black. So if printf is a ' functional line ' I do not see how I can shrink this code to 11 lines, or less. I am going to turn it in like this. Quzah, still like to see how you pulled it off, if you have time to post it. Got to go to class, thanks for taking the time to look at my code.

    Code:
    #include <stdio.h>
    
    main()
    {
    
    int count[101], y, grade, x;
    int A=0, B=0, C=0, D=0, F=0;
    int AB=0, BC=0, CD=0, DF=0;
    
    printf(“\n\n          | Grade System |       \n”);
    printf(“-----------------------------------------------\n\n”);
    
    while (grade!=  999)
    {
    for (y=0; y<=100; ++y)
    		count[y]=0;
    
    printf(“\n   Please input Grades ‘0 – 100’, to see results or exit program type ‘999’\n\n”);
    
    for(y=0; y<=99; ++y)
    {
    printf(“Grade:   “);
    scanf(“%i”, &grade); 
    
    if (grade == 999)
    	y=100; 
    else if (grade <0 || grade >100)
    { 
    printf(“\n Sorry, that is incorrect value, to see result or exit program type ‘999’ \n\n”);
    	--y;
    }
    else 
    	++ count[grade];
    }
    
    for(y=100; y>=95; --y)
    A=A+count[y];
    for(; y>90;  --y)
    AB=AB+count[y];
    for(; y>85;  --y)
    B=B+count[y];
    for(; y>80;  --y)
    BC=BC+count[y];
    for(; y>75;  --y)
    C=C+count[y];
    for(; y>70;  --y)
    CD=CD+count[y];
    for(; y>65;  --y)
    D=D+count[y];
    for(; y>60;  --y)
    DF=DF+count[y];
    for(; y>0;  --y)
    F=F+count[y];
    
    printf(“\n 95-100:  “);
    	for(x=A; x>0; --x)
    printf(“x”); 
    
    printf(“\n 90-94:  “);
    	for(x=AB; x>0; --x)
    printf(“x”); 
    
    printf(“\n 85-89:  “);
    	for(x=B; x>0; --x)
    printf(“x”); 
    
    printf(“\n 80-84:  “);
    	for(x=BC; x>0; --x)
    printf(“x”); 
    
    printf(“\n 75-79:  “);
    	for(x=C; x>0; --x)
    printf(“x”); 
    
    printf(“\n 70-74:  “);
    	for(x=CD; x>0; --x)
    printf(“x”); 
    
    printf(“\n 65-69:  “);
    	for(x=D; x>0; --x)
    printf(“x”); 
    
    printf(“\n 60-64:  “);
    	for(x=DF; x>0; --x)
    printf(“x”); 
    
    printf(“\n 0-59:  “);
    	for(x=F; x>0; --x)
    printf(“x”); 
    printf(“\n --------------  | End Program/Results |  -------------- \n”);
    
    	}
    }
    [QUOTE=quzah]I believe a "functional line" is something that's actually doing some work to solve the problem. For example, functional lines are colored green in the following:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Run A Program from within a cpp code
    By Hexxx in forum C++ Programming
    Replies: 6
    Last Post: 01-02-2006, 08:05 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. very difficult code - program
    By gaurav_behl in forum C Programming
    Replies: 2
    Last Post: 06-15-2004, 11:33 PM