-
Program giving errors
/* There are errors in this program,, I do not want the program altered in any way as I won't learn that way
All I need is to know is what line the errors are on, and explain why they are errors please so I can correct the program myself
THANKS */
/* Hi_Low&Average.C */
#include <stdio.h>
void main()
{
int hi, low, number , num_count, value,
average, total;
/* set hi to owest integer value and low to highest
integer so that they will change */
hi = -32768;
low = 32767;
number = 0 = num_count = 0 = total = 0;
/* test to ensure number of inputs is positive */
while ( number < 1 ) do
{
printf("\nEnter number of repititions > ");
scanf("%d" ,&number);
if( number < 1 ) then
printf("\nNumber must be > 1\n");
}
/* loop for input of numbers */
for( num_count = 0, num_count < number; num_count++ )
{
prinf("Enter number #%d > ", num_count+1);
scanf ("%d" , &value );
/* test for highest and lowest values */
switch ( value )
{
case value > hi : hi = value;
break;
case value < low: low = value;
break;
case default: break;
}
total:= total + value;
}
/* Calculate and display average */
average = total / number;
printf("\nAverage of %d numbers is %d", number, average);
/* display highest and lowest values */
printf("\nHighest Value is %d, Lowest is %d" , hi, low);
-
>>number = 0 = num_count = 0 = total = 0;
for one thing, this line would be better off as:
number = num_count = total = 0;
makes sense to you why, I hope.
Code:
while ( number < 1 ) do
{
printf("\nEnter number of repititions > ");
scanf("%d" ,&number);
if( number < 1 ) then
printf("\nNumber must be > 1\n");
}
You obviously haven't read up on how to loop correctly, try:
Code:
while ( number < 1 )
{
printf("\nEnter number of repititions > ");
scanf("%d" ,&number);
if( number < 1 )
{
printf("\nNumber must be > 1\n");
}
for a while loop, and
Code:
do
{
printf("\nEnter number of repititions > ");
scanf("%d" ,&number);
if( number < 1 )
{
printf("\nNumber must be > 1\n");
}
while ( number < 1 );
for a do-while loop.
also, there's no "then" in C, so get rid of that, and replace it with a bracket {
Code:
for( num_count = 0, num_count < number; num_count++ )
change that to
for( num_count = 0; num_count < number; num_count++ )
because you had a comma there instead of a semicolon.
Code:
switch ( value )
{
case value > hi : hi = value;
break;
case value < low: low = value;
break;
case default: break;
}
change that to two if statements, because you can't evaluate expressions in a switch-case.
Code:
total:= total + value;
this isn't Pascal! :p you meant total= total + value; right? :)
also, on your very last line add a } to close off the main function
Warning: don't look at this if you don't want to see the fixed version of your program - I took the liberty of doing so as I was fixing it
Code:
#include <stdio.h>
int hi, low, number , num_count, value, average, total;
void main()
{
/* set hi to owest integer value and low to highest
integer so that they will change */
hi = -32768;
low = 32767;
number = num_count = total = 0;
/* test to ensure number of inputs is positive */
while ( number < 1 )
{
printf("\nEnter number of repititions > ");
scanf("%d" ,&number);
if( number < 1 )
{
printf("\nNumber must be > 1\n");
}
}
/* loop for input of numbers */
for( num_count = 0;num_count <number; num_count++ )
{
printf("Enter number #%d > ", num_count+1);
scanf ("%d" , &value );
/* test for highest and lowest values */
if(value > hi)
hi = value;
else if(value < low)
low = value;
total= total + value;
}
/* Calculate and display average */
average = total / number;
printf("\nAverage of %d numbers is %d", number, average);
/* display highest and lowest values */
printf("\nHighest Value is %d, Lowest is %d\n" , hi, low);
}
-
I'm senseing a homework question here
-
Thanks guys.
Must still be trying to do the pascal thang .
Thanks for your help.
And NO not homeworking trying to get the C thang hard to shake pascal.
-
oh yeah, and before I forget - use int main() and don't forget to add return 0; before your last }
-
The code tags keep things neat too, Ken. I've heard great things from the people who urge members to read the posting guidelines and faq, Andy. By they way, that is a great example for stretching the legs with a language.
Best Regards,
New Ink -- Henry