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;
}
Related
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;
}
I want to generate the arithmetic average from an array, but only with values from a certain range (here from -5 to 5)
Is this code ok?
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 10 || n <= 0)
{
printf("Error! number should in range of (1 to 10).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
edit : I am sorry I must have missed copying the whole code in th eheat of the moment.
It is a simple question I know bu I cannot seem to get it to work. Maybe the lack of sleep is making me go insane
Your code is wrong.
And it's poorly formatted and that's the reason why you don't see why it's wrong:
Your poorly formatted code:
...
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
} // it looks as if the for loop end here, but it doesn't
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // the for loop actually ends here
The same code formatted correctly:
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // <<< the for loop ends here
Correct code (look at the comments for an explanation):
...
int nbofnumbers = 0; // number of numbers in the interval [-5,5]
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
nbofnumbers++;
sum += num[i];
}
} // for loop must end here
average = sum / nbofnumbers; // we divide by the number of numbers
// in the interval [-5,5], not by n
printf("Average = %.2f", average);
return 0;
...
Basically, the max size of the array is 10 and the user is allowed to enter up to 10 values. If the user enters -1 or 0, before entering the ten values, then the loop stops and goes to the next loop. My problem is is that it works perfectly UNTIL I enter 10 values. The result will divide by 9 instead of 10 and print that there are 9 values in the array.
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
count++;
}
n = count-1;
float sum = 0;
float average;
for(i = 0; i<=n; i++)
{
sum = sum + numbers[i];
}
average = sum/count;
printf("The average price of the %d products is %.2f.\n", count, average);
return 0;
} //this is the fixed solution.
You should not write n=count-1; write n=count; instead. And move count++ to the end of loop.
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
count++;
}
n = count;
float sum = 0;
float average;
for(i = 0; i<n; i++)
{
sum = sum + numbers[i];
}
average = sum/a;
printf("The average price of the %d products is %.2f.\n", n, average);
return 0;
}
I'm pretty sure it is because of this line
n = count-1
Can you explain why you are subtracting 1? In the case where you input 10 numbers, count will equal 10. After subtracting 1 you will then only iterate thru the first 9 indexes in the array.
If you need to subtract 1 (to account for the user inputting a 0 or -1), then change the condition in the last for loop to be <= instead.
for(i = 0; i<=n; i++)
There are two things has to be considered in this program.
1 . n = count-1;
for(i = 0; i<n; i++)
The count variable contains the number of element in the array, while n has been assigned to count - 1 to access from the 0th position , but while iterating the loop condition `i<n` make the loop to run n-1 time (i.e 9 times in this case).
So that the sum calculation failed to calculate the last array element.
average = sum/a;
The variable average and sum are float , whereas n is an int, so the type conversion has to be made while calculating the average.
average = sum/(float)a;
NOTE : a should be replaced by count , which hold the exact count of the element in array.
The complete corrected code is,
#include <stdio.h>
int main(void)
{
float numbers[10];
int i;
int n;
int count =0;
for(i = 0; i<10; i++)
{
scanf("%f", &numbers[i]);
count++;
if(numbers[i] == -1)
break;
if(numbers[i] == 0)
break;
}
n = count-1;
float sum = 0;
float average;
for(i = 0; i<=n; i++)
{
sum = sum + numbers[i];
}
average = sum/(float)count;
printf("The average price of the %d products is %.2f.\n", count, average);
return 0;
}
I've written this so far:
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
but when I enter 8, for example, it won't allow me to add any other numbers.
What's the problem?
while (i <= n); --> while (i <= n). Drop the ;. With the;, the while() loop never ends and { scanf("\n%d", &a); ... is never entered.
Suggest using auto formatting - easy to catch problems like this.
Also, to read n values use < #BLUEPIXY
// while (i <= n)
while (i < n)
#Shabnam You can use this code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}