I need some help with my program please.

This is a discussion on I need some help with my program please. within the C Programming forums, part of the General Programming Boards category; Hi! I turned in my program to my professor, but he said that there was improper line spacing & that ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Unhappy I need some help with my program please.

    Hi! I turned in my program to my professor, but he said that there was improper line spacing & that my program gave the wrong answer for square root of the average.

    If anyone could kindly look at what I have so far & tell me what's wrong with my program, I would greatly appreciate it.

    Thank You Very Much.


    ASSIGNMENT 2

    Write a program which asks the user to enter three numbers from the keyboard, and determines the smallest number, the largest number and (if the sum of all the numbers is not negative) the square root of the sum of all the numbers. If the sum is negative, the program informs the user of that fact.



    The program must work for cases in which all data are negative, all data are positive, all data are the same, or the data are mixed negative and positive values.



    Here's My Program


    Code:
    #include <stdio.h>
    #include <math.h>
    
    /*This program is designed to find the smallest of 3 numbers, the largest,
    
    the average, & the square root of the average.*/
    
    int main()
    
    {
    
    	 float a;  /*a represents 1st number input.*/
    
    	 float b;  /*b represents 2nd number input.*/
    
    	 float c;  /*c represents 3rd numer input.*/
    
    	 float avg; /*avg represents the average of those inputs.*/
    
    	 printf("Please Enter a number. ");
    
    	 scanf("%f",&a);
    
    	 printf("Enter another number. ");
    
    	 scanf("%f",&b);
    
    	 printf("Enter another number. ");
    
    	 scanf("%f",&c);
    
    	 /*This section of the program determines the smallest of the three inputs.*/
    
    	 if((a<b) && (a<c))
    	 {
    
    		printf("%f is the smallest number.\n", a);
    
    	 }
    
    	 if((b<a) && (b<c))
    	 {
    		printf("%f is the smallest number.\n", b);
    	 }
    
    	 else if((c<a) && (c<b))
    
    	 {
    
    		printf("%f is the smallest number.\n", c);
    
    	 }
    
    	/*This section of the program determines the largest of the three inputs.*/
    
    	if((a>b) && (a>c))
    
    	 {
    
    		printf("%f is the largest number.\n", a);
    
    	 }
    
    	 else if((b>a) && (b>c))
    
    	 {
    
    		printf("%f is the largest number.\n", b);
    
    	 }
    
    	 else if((c>a) && (c>b))
    
    	 {
    
    		printf("%f is the largest number.\n", c);
    
    	 }
    
    	 /*This section is in the event the sum of all three numbers are negative.*/
    
    	 if((a+b+c)<0)
    
    	 {
    
    		printf("The sum is negative.\n");
    
    		printf("You can't find the square root of a negative number!\n");
    
    	 }
    
    	 /*This section of the program determines the average of the three inputs.*/
    
    	 avg= (a+b+c)/3;
    
    	 printf("The average of the three numbers is %f\n", avg);
    
    	 printf("The square root of the average is %f\n", sqrt(a+b+c));
    
    
    	 return(0);
    }
    
    /*End of Program*/
    Last edited by agentxx04; 09-25-2004 at 06:57 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    << !! Posting Code? Read this First !! >>
    Now read, then press the edit button to fix your post
    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.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Go look at some other C/C++ source code somewhere and have a look at how they format it. In my opinion, they way you've done it now makes it hard to read. If someone gave me source code in this format I would ignore it because the spacing is very unprofessional. It would make me wonder how professional the actual code is (though I'm not saying you're unprofessional - you're still a student). Techincally, white space does not matter in C/C++, so if you feel like it, you can tell your professor that and refuse to do anything about it. Probably easier in the long run to just go ahead and learn go style, however.

    As for the square root, I can't find a problem. Though you may want to end the program by returning 0 early in a conditional if the numbers sum to a negative. I'd also like to point out that since you have not yet done that, this bug may be the problem your teacher is referring to.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >white space does not matter in C/C++
    Except where it does.
    My best code is written with the delete key.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Okay I take that back. White space between lines and words does not matter.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >White space between lines and words does not matter.
    You might want to take that one back too.

    Whitespace doesn't matter, except where it does. Leave it at that and you're safe.
    My best code is written with the delete key.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
     float a; /*a represents 1st number input.*/
    
    float b; /*b represents 2nd number input.*/
    
    float c; /*c represents 3rd numer input.*/
    To me, naming your variables input_num1 through input_num3 would solve the need for the comments and also make more sense throughout the rest of the program.

    Or maybe not even those names, but the lesson is that the name of the variable should explain what it does. If you have to explain what a variable does with a comment then you need to rename it. Of course there are exceptions where you can use something like i as a generic counter variable.
    Last edited by itsme86; 09-25-2004 at 02:03 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by sean_mackrory
    Okay I take that back. White space between lines and words does not matter.
    still not quite right

    Code:
    intmain(intargc,char*argv[]) {
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by agentxx04
    Hi! I turned in my program to my professor, but he said that there was improper line spacing & that my program gave the wrong answer for square root of the average.

    Code:
    	 /*This section of the program determines the average of the three inputs.*/
    
    	 avg= (a+b+c)/3;
    
    	 printf("The average of the three numbers is %f\n", avg);
    
    	 printf("The square root of the average is %f\n", sqrt(a+b+c));
    
    
    	 return(0);
    }
    Have you checked what you are actually calculating? The square root of the average is sqrt(avg).

    On the other hand, he asked for the square root of the sum, not the square root of the average. (Right answer, wrong label?)

    As for as line spacing: If the instructor said not to double space, then white space between lines does count (not to C or C++, but to the guy who gives your grade).

    It is not uncommon for companies to publish and enforce style guidelines for code created by employees (that is, for employees who want to remain employees).

    [edit]
    Forget the program for a minute. Go back and read the assignment. Work out the answers with pencil and paper. Use 1, 2, 3 as your numbers. Now run your program and enter 1, 2, 3. Do you get the answers that the assignment requires?
    [/edit]

    Dave
    Last edited by Dave Evans; 09-25-2004 at 11:11 PM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    16
    Thank you so much for your help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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