Finding min and max value in C (while loop) - c

I am new at C. I am having problems with finding min and max value with while loop.
Can somebody tell me how can I find MIN value without initializing min value with random number..
#include<stdio.h>
#define STOP 0
main()
{
int n, min, max;
printf("unesite niz cijelih brojeva [0 za kraj]: \n");
scanf("%d", &n);
max=0;
min=999999;
while(n!=STOP)
{
if(n<min)
min=n;
if (n>max)
max=n;
scanf("%d", &n);
}
printf("max broj je: %d, a min broj je: %d.\n", max, min);
system("pause");
}

Problem was at line where i was initializing min value. The right code is:
#include<stdio.h>
#define STOP 0
main()
{
int n, min, max;
printf("unesite niz cijelih brojeva [0 za kraj]: \n");
scanf("%d", &n);
max=0;
min=n; //here was the problem
while(n!=STOP)
{
if(n<min)
min=n;
if (n>max)
max=n;
scanf("%d", &n);
}
printf("max broj je: %d, a min broj je: %d.\n", max, min);
system("pause");
}

As #Марко Лучић says min=n;
Code can also max=n;
Suggested modifications:
1- Initialize min, max
#include <limits.h>
min = INT_MAX;
max = INT_MIN;
2- Test scanf() results. Only 1 scanf() needed.
while (scanf("%d", &n) == 1 && n != STOP) {
if(n < min)
min = n;
if (n > max)
max = n;
}

Related

adding a second function to the array that is working with indexes of array

I have a code that works perfectly, its finding average of all numbers that were put in the array and its finding average of a section of array. I just need to make second function that is working with the array with its indexes.
Code:
#include <stdio.h>
#include <stdlib.h>
double priemer(double *pole, int dlzka);
int main()
{
int i, dlzka;
printf("Zadaj velkost pola: ");
scanf("%d", &dlzka);
while (dlzka < 1)
{
printf("Chyba! Cislo musi byt viac, ako 0.\n");
printf("Zadaj pocet prvkov znova: ");
scanf("%d", &dlzka);
}
double *pole = malloc(dlzka * sizeof(double));
for(i = 0; i < dlzka; i++)
{
printf("cislo %d: ", i + 1);
scanf("%lf", &pole[i]);
}
printf("Priemer vsetkych prvkov v poli je: %.3lf\n", priemer(pole, dlzka));
int zaciatok, koniec;
printf("Zadaj zaciatok useku: \n");
scanf("%d", &zaciatok);
printf("Zadaj koniec useku: \n");
scanf("%d", &koniec);
if(koniec > dlzka || zaciatok > koniec || zaciatok < 1)
{
printf("Zly vstup");
return 1;
}
printf("Priemer od %d. do %d. prvku je: %.3lf\n", zaciatok, koniec, priemer(pole + (zaciatok - 1), koniec - (zaciatok - 1)));
return 0;
}
double priemer(double *pole, int dlzka)
{
double sum = 0;
int i;
for(i = 0; i < dlzka; i++)
{
sum += pole[i];
}
return sum/dlzka;
}

C programming find min,max and sum without using recursion and arrays

I am trying to write a program that get bunch of numbers, n >= 5
The program asks from the user to enter n non-negative numbers and calculate min,max and the sum of the numbers.
In case the user enter negative numbers, the function asks to enter a positive number.
I have a problem with the first negative number, any clue what's wrong with the following code?
void main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1;
printf("Enter numbers: \n");
scanf("%d", &num1); //here was the error
min = num1;
int sum = num1;
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
while (num1 < 0)
{
{
printf("Enter again number: /n");
scanf("%d", &num1);
}
}
if (num1 > max)
max = num1;
else if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
}
output:
Enter number:
8
Enter numbers:
-8
7
6
9
10
6
7
6
The max number is 10, and the min is -8, and the sum is 43
check if min<0 assign new entered value of num1 to it.
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
if (min < 0)
min = num1;
while (num1 < 0){
//rest of the code}
}
I have a problem with the first negative number, any clue what's wrong with the following code?
Do not read first number with special code, which lacks a negative test. Simplify and read just like other numbers.
Simply start min,max with extremes.
#include <limits.h>
...
int max = INT_MIN;
int min = INT_MAX;
int sum = 0;
printf("Enter numbers: \n");
for (int i = 0; i < x; i++) { // start at 0
int num1;
scanf("%d", &num1);
while (num1 < 0) {
printf("Enter again number: /n");
scanf("%d", &num1);
}
if (num1 > max)
max = num1;
// else if (num1 < min)
if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d",
max, min, sum);
you should put while after taking the input from the user to prevent any negative number also inside while there isn't need to put double cure prates
#include <stdio.h>
int main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1,sum;
printf("Enter numbers:\n");
sum = 0;
min = num1;
for (int i = 1; i<=x; i++)
{
scanf("%d",&num1);
while(num1<0)
{
printf("Enter again number: \n ");
scanf("%d", &num1);
}
if (num1 > max)
{ max = num1;}
if (num1<min)
{ min = num1;}
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
return 0;
}
````````````

Minimum Maximum Mark

I need to find the maximum and minimum mark from 5 integers which are inputted by the user. The maximum is being printed out, but the minimum is not. Any ideas?
#include<stdio.h>
int main()
{
int marks = 0, avg = 0, min = 0, max = 0;
for (int i = 0; i < 5; i++)
{
printf("Enter a mark: ");
scanf_s("%d", &marks);
if (marks > max)
{
max = marks;
}
if (marks < min)
{
min = marks;
}
}
printf("The maximum mark is: %d\n", max);
printf("The minimum mark is: %d\n", min);
//printf("The minimum mark is: %d\n", avg);
getch();
getch();
}
Set initial value of min to something higher, like 10000. Because none of your input values probably less than 0.
Or even better, use maximum available value for your data type
#include <stdio.h>
#include <limits.h>
int main()
{
int marks = 0, avg = 0, min = INT_MAX, max = 0;
for (int i = 0; i < 5; i++)
{
printf("Enter a mark: ");
scanf_s("%d", &marks);
if (marks > max)
{
max = marks;
}
if (marks < min)
{
min = marks;
}
}
printf("The maximum mark is: %d\n", max);
printf("The minimum mark is: %d\n", min);
//printf("The minimum mark is: %d\n", avg);
getch();
getch();
}

Retaining and naming user-inputted variables in C

I am trying to calculate a gradient using several X and Y values. My problem is that after my code takes the mean of the X and Y values I cannot re-use the user-inputted X and Y values to further calculate a gradient as I don't know how to name them. I've included my code below, and I think I have to use either Arrays or Global variables for this task.
Thank you in advance, my coding knowledge is limited.
#include <stdio.h>
int main () {
int n, i;
float num[1000], total=0, mean;
printf("Enter the amount of x-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input x-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean=total/n;
printf("The mean of all the x-values entered is %.2f to 2 decimal places", mean);
{
float num[1000], total=0, mean;
printf("Enter the amount of y-values:");
scanf("%d", &n);
while (n <= 0 || n > 1000) {
printf("Print error. The number should in range of 0 to 1000.\n");
printf("Please try to enter the amount again: ");
scanf("%d",&n);
}
for (i = 0; i < n; ++i) {
printf("%d. Input y-value:", i+1);
scanf("%f", &num[i]);
total += num[i];
}
mean = total / n;
printf("The mean of all the y-values entered is %.2f to 2 decimal places", mean);
return 0;
}
}

C program using while- weird counter error if counter>8

This program finds out the max and min of 10 numbers using the while loop.
If i set the number of inputs from the users to 8 or less it works, if i set it to >8 (say 10) it produces an error (program stops responding) when the user tries to enter the 9th number.
Any ideas ? Thanks.
#include <stdio.h>
#include <stdlib.h>
main()
{
int x, max, min;
int i = 0;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
max = 0;
min = x; // Set min to number
do
{
i++;
if (i > 10) break;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
if (x > max)
max = x;
if (x < min)
min = x;
}
while (max > min);
printf("max este %d\n", max);
printf("min este %d\n", min);
}
I do not see why using a do-while loop when you know how many iterations you want to perform. You should use a for loop instead (do-while works too, but for loop is intended to be used in situations like these).
Moreover, you had an else when checking for min, which was not needed, since you want to check if every number given from the user is min or max.
Notice that the for loop starts from 1, since we read one number outside the loop.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, x, max, min;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
max = x;
min = x; // Set min to number
for(i = 1; i < 9; ++i) {
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
if(x > max) {
max = x;
}
if(x < min) { // here you had an else, but it was wrong
min = x;
}
}
printf("max este %d\n", max);
printf("min este %d\n", min);
return 0;
}
About the return value of scanf() mentioned in one comment, you can always check the ref.
If you need to that with a while loop, then you should do this:
#include <stdio.h>
#include <stdlib.h>
int main() {
int i = 0, x, max, min;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
max = x;
min = x; // Set min to number
while(i < 9) {
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
if(x > max) {
max = x;
}
if(x < min) { // here you had an else, but it was wrong
min = x;
}
i++;
}
printf("max este %d\n", max);
printf("min este %d\n", min);
return 0;
}
Here the counter i is increased at the end of every loop and the condition to stop is i < 9, since we have already read a number outside the loop.
Do not write else if for min, because you need to check both max and min at the same time.Also for 10 numbers it should be i>=10.And you can make while to 1, because it will break at i=10 anyhow.Here is your working code.Also initially max should be set to x.
#include <stdio.h>
#include <stdlib.h>
main()
{
int x, max, min;
int i = 0;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
max = x;
min = x; // Set min to number
do
{
i++;
if (i >= 10) break;
printf("Introduceti un intreg\n"); // Enter a number
scanf("%d", &x);
if (x > max)
max = x;
if (x < min)
min = x;
} while (1);
printf("max este %d\n", max);
printf("min este %d\n", min);
}

Resources