1. ## 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.*/

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*/```  3. 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. >white space does not matter in C/C++
Except where it does. 5. Okay I take that back. White space between lines and words does not matter. 6. >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. 7. 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. 8. 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. 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).

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 10. Thank you so much for your help!!!  Popular pages Recent additions 