Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HELP!
Write a program that reads a set of 20 ratings, calculates the maximum and minimum of that set of ratings.

What I have tried:

this is my code (in spanish)

C
#include <stdio.h>
#include <conio.h>

int x,c;
float n=0,baja,alta;

int main (void)
{
	for (c=1;c<=20;c++)
	{
		printf ("%i-. Ingresa calificacion:\n\n",c);
		scanf ("%f", &n);
	}

	if (x==1)
	{
		alta=n;
		baja=n;
	}
	else
	{
		if(n<baja)
		{
			baja=n;
		}
		else
		{
			if (n>alta)
			{
				alta=n;
			}
		}
	}
	
	printf ("La calificacion mas baja es: %.2f \n", baja);		
	printf ("La calificacion mas alta es: %.2f \n", alta);	
		
	getch ();
	return 0;
}
Posted
Updated 16-Oct-22 12:33pm
v2
Comments
Dave Kreskowiak 16-Oct-22 11:57am    
And? What's the question?

It would also help if your code was properly indented.

Oh, and ALWAYS use curly braces around your code where "optional". The elimination of curly braces for a single statement is what usually gets noobs in trouble when writing code.

I fixed the formatting of your code and inserted braces where appropriate. It seems to have made when you mistake is quite obvious.

Besides the bug that Richard has already pointed out and the bracket problems that Dave has already partially fixed, there are a few more things that don't work well. I have written the error locations as comments in the source code.
C
int main(void)
{
	// do not declare variables global, x not used
	int c;  //x
	float n = 0, baja, alta;

	for (c = 1; c <= 20; c++)
	{
		printf("%i-. Ingresa calificacion:\n\n", c);
		scanf("%f", &n);
	// }  

	  // The uninitialized local variable "x" was used, change to c
	  // if (x == 1)
	  if (c == 1) {
		 alta = n;
		 baja = n;
	  }
	  else {
	     if (n<baja) {
            baja = n;
	     }
	     // else {
	     if (n>alta) {
            alta = n;
	     }
	  }
	}

And then one more note:
C
// warning: 'getch': The POSIX name for this item is deprecated, use _getch()
_getch();

My suggestion would be to dispense with both conio.h and getch() and instead use getchar() to wait for user input.
 
Share this answer
 
v2
C++
if(n<baja)
{
    baja=n;
}
else
{
    if (n>alta)
    {
        alta=n;
    }
}

In the above code if the first test is true it will not do the second test. So move it out of the else block thus:
C++
if(n<baja)
{
    baja=n;
}
if (n>alta)
{
    alta=n;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900