Can some 1 debug this for me and help me print the output

This is a discussion on Can some 1 debug this for me and help me print the output within the C Programming forums, part of the General Programming Boards category; Ok so heres the deal. I need the program to print in two columns, one with the number of quizzes ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    Can some 1 debug this for me and help me print the output

    Ok so heres the deal. I need the program to print in two columns, one with the number of quizzes two wit the grade from each quiz, and then at the end i need the average to print, here is the code so far

    Code:
    int main()
    {
    	//Define Varibles int and floats
    	int gone,gtwo,gthree,gfour,gfive,gsix,gseven,geight,gnine,gten,k,avg,sum;
    	
    	
    	
    	//User Input
    	printf("Please enter the 10 grades, make sure you space and use commas to differentiate each grade");
    	scanf("%i,%i,%i,%i,%i,%i,%i,%i,%i,%i",&gone,&gtwo,&gthree,&gfour,&gfive,&gsix,&gseven,&geight,&gnine,&gten);
    	
    	
    	
    	//Check Input
    	if (gone == -gone||gtwo == -1||gthree == -gthree||gfour == -gfour||gfive == -gfive||gsix == -gsix||gseven == -gseven||geight == -geight||gnine == -gnine||gten == -gten)
    	
    		printf("Please enter a positive grade. Grades can not be negative");
    	
    	
    	else 
    	{ 
    		sum = gone+gtwo+gthree+gfour+gfive+gsix+gseven+geight+gnine+gten;
    		
    		avg = sum/10;
    	{
    		
    	
    		while(k <=10)
    		{
    			k++;
    			printf("%i,%i",k,avg);
    			
    		}
    
    	return 0;
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. gone==-gone does not check to see if gone is negative, it checks (in a very roundabout way) to see if gone is zero.
    2. If you intend to use a for loop to print your quiz scores, your quiz scores must be in an array.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    i dont think my prof wants arrays but i think it would be best if i use it also im tryin 2 say if the number enter is negative then dont do the else...is there a general way i can say that??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If your prof didn't want arrays, he wouldn't have given the assignment.

    That's what if means -- do either or the other, but not both. So put everything you don't want done in the negative case in the else clause of your if-statement.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    makes total sense now thanks,im going 2 rewrite n get bck 2 u

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > # gone==-gone does not check to see if gone is negative, it checks (in a very roundabout way) to see if gone is zero.
    Unless you happen to be using a 1's complement machine, in which case it means something else
    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.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's not ~, it's -. I fail to see how using a one's complement machine will change the rules of mathematics.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by tabstop
    I fail to see how using a one's complement machine will change the rules of mathematics.
    Possibly because there are two representations for zero in one's complement arithmetic, hence +0 != -0. Then again, I have never had the chance to actually use an implementation that uses one's complement.
    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

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    +0 and -0 have separate representations, so I don't see how they would compare equal.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Oh, better yet, in section 6.2.6.2, C99 says that "it is unspecified whether these cases actually generate a negative zero or a normal zero, and whether a negative zero becomes a normal zero when stored in an object." Here, "these cases" refer to the use of certain operators like unary + and unary - which may generate a negative zero if the implementation supports negative zeros.

    Consequently, whether +0 != -0 in one's complement in a C program is implementation defined.
    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

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I apologize; I thought the standard required positive zero and negative zero to compare equal, but I see now that it only permits it.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    ok im having problems again this is wut the hw says: Design a program in C which will compute the total grade as well as the average grade received by a student for 10 quizzes. Each quiz grade is out of 10 points. When the program is executed, the user will be asked to input the 10 grades in succession. The output is in the form of a tabular display on the monitor which consists of two columns, one for Quiz # and the other for Quiz Grade. The total and average grade values are to be printed below the table.

    where the hell do i put the arrays if its coming as a user input

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    heres the new code

    Code:
    int main()
    {
    	//Define Varibles int and floats
    	int gone,gtwo,gthree,gfour,gfive,gsix,gseven,geight,gnine,gten,counter,avg,sum;
    	
    	
    	
    	//User Input
    	printf("Please enter the 10 grades, make sure you space and use commas to differentiate each grade  ");
    	scanf("&#37;i,%i,%i,%i,%i,%i,%i,%i,%i,%i",&gone,&gtwo,&gthree,&gfour,&gfive,&gsix,&gseven,&geight,&gnine,&gten);
    	
    	
    	
    	//Check Input
    	if (gone >= 0||gtwo >= 0||gthree >= 0||gfour >= 0||gfive >= 0||gsix >= 0||gseven >= 0||geight >= 0||gnine >= 0||gten >=0)
    	{
    		sum = gone+gtwo+gthree+gfour+gfive+gsix+gseven+geight+gnine+gten;
    		avg = sum/10;
    	
    		
    		for(counter=1; counter <=10; counter++)
    			{
    			counter = counter;
    			printf("%i\t%i\n",counter,gone,gtwo,gthree,gfour,gfive,gsix,gseven,geight,gnine,gten);
    			}
    	}
    	else 
    	{
    		printf("Please enter a positive grade. Grades can not be negative");
    		
    	}
    	
    	printf("%i\t%i\t%i",counter,avg,sum);
    	
    	getch();
    	
    
    	return 0;
    }

    but i need my program to loop and the output to look like this
    number of quizzes quiz grade
    1 80
    2 82
    3 50
    4 90
    5 78
    6 90
    7 100
    8 35
    9 11
    10 65
    Last edited by clezzie; 10-22-2008 at 11:02 PM.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    can any 1 help me wit my output??

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would REALLY help your case if you used arrays instead of individual variables. However, we can do both, I suppose:
    Code:
    int *ptrArr[] = { &gone,&gtwo,&gthree,&gfour,&gfive,&gsix,&gseven,&geight,&gnine,&gten };
    int i;
    
    ...
    for(i = 0; i < sizeof(ptrArr)/sizeof(ptrArr[0]); i++)
    {
       printf("%d\t\%d\n", i, *ptrArr[i]);
    }
    There, problem solved.

    [Of course, your professor will know immediately that you didn't come up with this yourself, at least if he asks you to explain what the code does, so you are probably better off working out how to solve it yourself].

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

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