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

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);
}

Related

Read Multi integer from user and do some iteration and calculation

instructions are:
Write a program which reads 10 different integers from the user and finds and prints
a) Minimum element
b) Sum of all elements and
c) Average of elements.
Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a
there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
you min variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}

How do I keep the users inputted values after he leaves the "enter" option and make him start from were he finished of?

I have a certain exercise I must do. Hopefully someone understands what I have failed to do.
whenever a user presses enter, types in less than 10 integers, presses q to exit the option and then comes back, all of his input values will be gone. I want to input values to be left there until the total elements stored are 10. How do I do this?
Note! The user is not supposed to exit the application (which in this case is CMD), rather they should only be able to leave the "enter" option and still have their input values "stored".
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define L 10
void view(int a[])
{
int i;
printf("\n Here are the currently stored values\n");
printf("[");
for(i=0; i<L; i++)
{
printf(" %d ",a[i]);
}
printf("]");
}
int enter(int a[], int n)
{
printf("\n Note! You can press any letter if you want to return to the main menu. \n");
for(int i=0; i<L; i++)
{
int mesa;
printf(" Enter measurement #%d (or q to quit): ", n+1);
int check = scanf("%d",&mesa);
if(check)
{
a[i] = mesa;
n++;
}
else
{
char temp;
scanf(" %c",&temp);
return n;
}
}
return n;
}
void compute(int a[], int n)
{
printf(" Computing...\n");
//Min value
{
int i;
int min = 10000;
for (i = 0; i < L; i++)
if(a[i] < min)
min = a[i];
printf("\n The min value is: %d \n", min);
}
//Max value
{
int i;
int max = a[0];
for (i = 0; i < L; i++)
if (a[i] > max)
max = a[i];
printf(" The max value is: %d \n", max);
}
// Average & Normalization
{
float average;
int i;
int norm;
int sum;
for(i = 0; i < L; ++i)
{
sum = sum + a[i];
average = (float)sum / 10; //typecast - Ge en interger en tillfällig roll. På så sätt kan du säga åt programmet att du faktiskt vill ha float som svar och inte ett heltal som svar.
}
printf(" Average value: %.2f \n", average);
printf(" Normalized array: [");
for(i = 0; i < L; i++)
{
norm = a[i] - average; //average - every a[]
printf(" %d", (int)round(norm));
}
printf(" ]");
}
}
void reset(int a[])
{
printf(" You have chosen to reset the array. All the elements in the array will now be deleted. \n");
//memset( a, 0, 10*sizeof(a)); <--- Kan ej användas då sizeof() funktionen läser av en variabel och inte hela arrayens storlek.
memset( a, 0, 10*sizeof(int*));
}
int main()
{
char command;
int a[L];
printf(" Hello and welcome to this measurement tool. In this program you will be able to type in and analyze data.\n");
printf(" In the section below, you can choose a letter v,e,c,r or q to do certain things. More information will be provided down below.\n");
printf("\n v(View) - Displays currently stored values.");
printf("\n e(Enter) - Allows you to store values. ");
printf("\n c(Compute) - Displays the maxiumum, minimum, normalized and average value of those which are stored.");
printf("\n r(Reset) - Deletes all stored values.");
printf("\n q(Quit) - Exits the program. \n");
printf("\n Please choose one of the commands above: ");
do
{
int n = 0;
scanf(" %c",&command);
switch(command)
{
case 'v' : view(a);break;
case 'e' : enter(a,n);break;
case 'c' : compute(a,n);break;
case 'r' : reset(a);break;
default : printf("\n Please choose one of the options given.\n");break;
case 'q' :
{
printf(" Want to exit? Confirmation needed. Press q again to exit.");
scanf(" %c",&command);
}
}
printf("\n VECRQ?: ");
} while(command != 'q');
return 0;
}
In main(), move the declaration of n outside the loop:
int n = 0;
do
{
...
Update n when you call enter():
n = enter(a,n);
In the enter function, start your loop from n:
for(int i=n; i<L; i++)

I need help figuring out why is this not working. (C)

This is what i get This code needs to print out 2 maximum numbers, when -999 is entered the code needs to stop.
I tried every thing but most of the times i get the maximum number but not the second maximum number.
#include <stdio.h>
int main ()
{
int x = 0;
int max = 0;
int max2 = 0;
int y = 0;
int flag = 0;
do
{
printf("Enter the number -999 to stop: ");
scanf("%d", &x);
if (x != -999)
{
if (x > max)
{
max = x;
max2 = y;
}
printf("Enter the number -999 to stop: ");
scanf("%d", &y);
if (y != -999)
{
if (y > max)
{
max = y;
max2 = x;
}
}
else
{
flag = 1;
}
}
else
{
flag = 1;
}
}
while (flag == 0);
printf("The max number is: %d\n", max);
printf("The second max number is: %d\n", max2);
return 0;
}
#include <stdio.h>
int main ()
{
int x = 0;
int max = 0;
int max2 = 0;
int flag = 0;
do
{
printf("Enter the number -999 to stop: ");
scanf("%d", &x);
if (x != -999)
{ // bigger than max?
if (x > max)
{
// since the new max is x and the old max is bigger than max2
max2 = max;
max = x;
}
// bigger than max2?
else if (x > max2)
{
max2 = x;
}
}
else // exit loop
{
flag = 1;
}
}
while (flag == 0);
printf("The max number is: %d\n", max);
printf("The second max number is: %d\n", max2);
return 0;
}
There are more problems in your code.
You have unnecessary two inputs (x and y), then you wrongly assume, when x > max, then y contains second max, and when y > max then x contains second max.
Correct code should look like this:
int main()
{
int x = 0;
int max = 0;
int max2 = 0;
while (true)
{
printf("Enter the number (-999 to stop): ");
scanf("%d", &x);
if (x == -999)
{
break;
}
if (x > max)
{
max2 = max;
max = x;
}
else if (x > max2)
{
max2 = x;
}
}
printf("\n\nThe max number is: %d\n", max);
printf("The second max number is: %d\n", max2);
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();
}

Finding min and max value in C (while loop)

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;
}

Resources