Hello all,

I'm suppose to write a program that asks the user to input five numbers. Out of those five numbers, the program is suppose to select the maximum value. I attempted to do it with "else-if" statements as can be seen in the following code. I was hoping there was a function included in the math.h or stdlib.h header files that could do this for me. However, there doesn't seem to be one. Here is what I have:

int main()
{
	float first_number;
	float second_number;
	float third_number;
	float fourth_number;
	float fifth_number;
	

	printf("Please input five numbers: ");
	scanf("%f%f%f%f%f", &first_number, &second_number, &third_number, &fourth_number, &fifth_number);
	
	if (first_number > second_number && first_number > third_number && 
		first_number > fourth_number && first_number > fifth_number)
		{
			printf("The largest value is %f.", first_number);
		}
	else if (second_number > first_number && second_number > third_number &&
			second_number > fourth_number && second_number > fifth_number)
		{
			printf("The largest value is %f.", second_number);
		}
	else if (third_number > first_number && third_number > second_number &&
			third_number > fourth_number && third_number > fifth_number)
		{
			printf("The largetst value is %f.", third_number);
		}
	else if (fourth_number > first_number && fourth_number > second_number &&
			fourth_number > third_number && fourth_number > first_number)
		{
			printf"The largest value is %f.", fourth_number);
		}
	else (fifth_number > first_number && fifth_number > second_number &&
		 fifth_number > third_number && fifth_number > fourth_number)
		{
			printf("The largest value is %f.", fifth_number);
		}

	
	return 0;

Am I on the right track or totally off the mark? I am to stick to using "if-else", switch, or "else-if" statements.

Noble

I wanted to add that when I compile it, it'll give me the max value from the set of numbers but it will also say the max value is whatever number stored in the "fifth_number" variable. For example, the numbers that were entered are: 9, 10, 8, 1, 2. It will select the largest value as 10 and output "The largest value is 10." like it's suppose to. However, it will also output "The largest value is 2," The program seems to always output whatever is in that last "else" statement.

Thanks,

Noble

else (fifth_number > first_number && fifth_number > second_number &&
		 fifth_number > third_number && fifth_number > fourth_number)
		{
			printf("The largest value is %f.", fifth_number);
		}

You should not have a condition after the "else" statement. Only "if" and "else if" statements should have conditions. Try changing it to this:

else
		{
			printf("The largest value is %f.", fifth_number);
		}

Thanks! However, I can't use loops because it hasn't been covered in class. We will soon and I can't wait.... And arrays are even further down the line. Even though I have a very limited knowledge of programming, it seems that those two threads involve loops and arrays. The code that I currently have using "else-if" statements seems to accomplish what I want but with that minor glitch.

Thank you Vernon!!! Your second post helped helped and solved my problem!

Noble

void main()
{
    float* a;
    int n,i,max=0;
    printf("Enter number of elements to find the maximum\n");
    scanf("%d",&n);
    a=malloc(sizeof(float)*(n+1));
    for(i=0;i<n;i++)
    {
        printf("Enter number %d\n",i+1);
        scanf("%f",&a[i]);      
    }
    max=a[0];
    for (i=1;i<n;i++)
    {
        if (max<a[i])
        {
            max=a[i];
        }
    }
    printf("The maximum number is \n%d\n\n",max);    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.