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();
}
Related
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;
}
I want to print the maximum, minimum, and total sum of input integers
but i don't understand why use this code(max,min,sum=arr[0];)
#include<stdio.h>
int main(void)
{
int arr[5];
int max, min, sum, i;
for (i = 0; i < 5; i++)
{
printf("input: ");
scanf("%d", &arr[i]);
}
max = min = sum = arr[0];
for (i = 1; i < 5; i++)
{
sum += arr[i];
if (max < arr[i])
max = arr[i];
if (min > arr[i])
min = arr[i];
}
printf("Maximum: %d \n", max);
printf("Minimum: %d \n", min);
printf("Total: %d \n", sum);
return 0;
}
The code is setting all the variables equal to the first element of the array. Then compares with the rest of it to replace in case they are greater, lesser or to add the value (sum)
So I am trying to make a simple program that reads integers of values and ID number so the output will be minimum maximum of the value and its maxID minID number. The first integer of the input file will indicate how many more input will be read in loops. My program compiles without any problem and minimum maximum output are correct however,the output of ID numbers are wrong. Could anyone help me with diagnose this issue? Sorry for my silly question, I am new in programming. Thanks.
#include <stdio.h>
int main(){
int val[100],id[100];
int i, max, min, size, idmax, idmin,minindex,maxindex;
printf("Enter how many IDs: ");
scanf("%d", &size);
printf("Enter ID numbers and values:\n");
for(i=0; i<size; i++)
{
scanf("%d %d", &id[i], &val[i]);
}
max = min = val[0];
for(i=1; i<size; i++)
{
if(val[i] > max)
{
max = val[i];
maxindex = i;
for(i=0;i<size;i++){
if(id[i]==maxindex){
idmax=id[i];
}
}
}
if(val[i] < min)
{
min = val[i];
minindex = i;
for(i=0;i<size;i++){
if(id[i]==minindex){
idmin=id[i];
}
}
}
}
printf("Max number = %d with ID number = %d\n", max, idmax);
printf("Min number = %d with ID number = %d\n", min, idmin);
return 0;
}
You are making the problem more complicated than it actually is. When running through the ;list, you can simply update the idmax oridmin value whenever you update the corresponding max or min:
#include <stdio.h>
int main() {
int val[100], id[100];
int i, max, min, size, idmax, idmin;/// No longer need these: minindex, maxindex;
printf("Enter how many IDs: ");
scanf("%d", &size);
printf("Enter ID numbers and values:\n");
for (i = 0; i < size; i++) {
scanf("%d %d", &id[i], &val[i]);
}
max = min = val[0];
idmin = idmax = id[0];/// Initialize IDs similarly to min and max
for (i = 1; i < size; i++) {
if (val[i] > max) {
max = val[i];
idmax = id[i];/// Only need to change this when max changes
}
if (val[i] < min) {
min = val[i];
idmin = id[i];/// Only need to change this when min changes
}
}
printf("Max number = %d with ID number = %d\n", max, idmax);
printf("Min number = %d with ID number = %d\n", min, idmin);
return 0;
}
Sum and get the average of the values in the array, and stop to ask a number when the sum exceeds 10,000.I need to clear it with an example of these conditions.
The language fit that need the example is in C
my code
```
#include <stdio.h>
int main()
{
int array[10];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
```
Answer of your Question
#include <stdio.h>
int main()
{
int array[10];
int num, negative_sum = 0, positive_sum = 0,j;
static int i=0;
float total = 0.0, average;
label:
j=i;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i; i < (j+num); i++)
{
scanf("%d", &array[i]);
}
i=j;
printf("Input array elements \n");
for (i; i < (j+num); i++)
{
printf("%d\n", array[i]);
}
i=j;
for (i; i < (j+num); i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + (float)array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
if(total<10000)
{
printf("Total is less than 10000 and Now total is : %.2f\n",total);
goto label;
}}
it is the answer of your question.program will run when your total is exceed upto 10000.
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);
}