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

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

Related

Print the positive, negative, and all entries made using while loop

So I've tried to calculate the average of the positive and negative entries made in the while loop but I can't seem to enter any type of number into the console. So I need to enter numbers as long as it's not zero and add the positive numbers as well as negative numbers. I'm required to count how many positive and negative entries were entered. I need also to print the average of the positive and negatives entries.
Here is my code:
int num;
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
float average = 0;
printf("Enter a positive and negative integer:");
while (num!=0)
{
scanf("%d", &num);
if (num > 0){
positivenum += num;
cpositive++;
}
else if (num < 0){
negativenum += num;
cnegative++;
}
This is exactly when do while is judicious:
int num;
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
printf("Enter a positive and negative integer:");
do
{
scanf("%d", &num);
if (num > 0){
positivenum += num;
cpositive++;
}
else if (num < 0){
negativenum += num;
cnegative++;
}
} while (num!=0);
if (cpositive > 0)
printf("Average of positive numbers: %f\n", (double)positivenum / (double)cpositive);
if (cnegative > 0)
printf("Average of negative numbers: %f\n", (double)negativenum / (double)cnegative);
Changed the type of your counts from int to unsigned (negative counts doesn't make sense). Also moved the prompt inside the loop and and wrote a function to calculate the average:
#include <stdio.h>
void average(const char *what, int value, unsigned count) {
if(!count) return;
printf("average of %s numbers: %f\n", what, (float) value / count);
}
int main() {
int num;
int positivenum = 0, negativenum = 0;
unsigned cpositive = 0, cnegative = 0;
for(;;) {
printf("Enter a positive and negative integer: ");
scanf("%d", &num);
if(!num) break;
if(num < 0) {
negativenum += num;
cnegative++;
continue;
}
positivenum += num;
cpositive++;
}
average("positive", positivenum, cpositive);
average("negative", negativenum, cnegative);
}
#include <stdio.h>
int main() {
int num;
int positivenum = 0, negativenum = 0;
int cpositive = 0, cnegative = 0;
int range = 0;
int i = 0;
printf("Enter the range.\n");
scanf("%d", &range);
printf("Enter a positive and negative integer:\n");
do
{
scanf("%d", &num);
if (num > 0){
positivenum += num;
cpositive++;
}
else if (num < 0){
negativenum += num;
cnegative++;
}
i++;
} while ( (num!=0) && (i < range));
if (cpositive > 0)
printf("Average of positive numbers: %f\n", (float)positivenum / (float)cpositive);
if (cnegative > 0)
printf("Average of negative numbers: %f\n", (float)negativenum / (float)cnegative);
}
I have also added a range to break the loop.

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

arithmetic average of an array with range -5 to 5 in c

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

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 Prog - While loop

Hi once again I have found myself stuck with coding. I have found the correct code but I do not understand why my initial coding does not work. Could anyone explain briefly the problem?
My initial code and the runnable code is posted below.
INITIAL CODE (does not work as expected)
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, num2, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num2);
if (num1 < num2)
{
smallest = num1;
}
if (num2 < num1)
{
smallest = num2;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
RUNNABLE CODE
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num1);
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
In your first attempt you save the value of smallest each loop, but you get rid of it because of the next loop will check always num1 and new entered number in num2.
In the second code, the working one, the loop is keeping track of the last smallest found.
A little thing, the code chunk
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
the second if does not make sense, you can write
if (num1 < smallest)
{
smallest = num1;
}
else
{
smallest = smallest;
}
or better
if (num1 < smallest)
{
smallest = num1;
}

Resources