c - how to make negative numbers into positive - Stack Overflow
How does C know whether a number is positive or negative?
if statement - Positive and Negative number program in C - Stack Overflow
calculus - Can constant C always be positive? - Mathematics Stack Exchange
What is a simple C program to check if a number is positive, negative, or zero?
Why is zero considered neither positive nor negative?
How do you check if a number is positive or negative in C using a ternary operator?
Videos
abs() is for integers only. For floating point, use fabs() (or one of the fabs() line with the correct precision for whatever a actually is)
You have to use:
abs() for int
fabs() for double
fabsf() for float
Above function will also work but you can also try something like this.
if(a<0)
{
a=-a;
}
I know the concept of storing the leftmost byte as 0 or 1 for negative, one's complement, and two's complement.
But at the end of the day, they are still bits? so if 8 is 0000 1000 and -8 (1st compliment) is 11110111. Here although the compliment is a negative value as such, it can still be interpreted as a really large positive number. How does it differentiate?
Edit: Doubt solved,appreciate your time.
I will provide you with an example approach. However, this example does not validate the user input. It is very important that you verify user input, that it is what you expect it to be. I will leave this as an exercise for the read:
#include <stdio.h>
#include <limits.h>
int main(void) {
int input;
int total = 0;
int max = 0;
int count = 0;
int min = INT_MAX;
while(scanf("%d", &input)) {
if (input<0)
break;
if (input>max)
max = input;
else if (input<min)
min = input;
total += input;
count++;
}
if (count>0)
printf("Max: %d\tMin: %d\tAverage: %f\n", max, min, (double)total/count);
else
printf("No positive numbers entered\n");
return 0;
}
Welcome to programming.
You have been given a problem to solve, and fortunately it is stated in a very clear way. Later problems will not be so clear.
The next step is to analyze it. In practice this means break it apart to examine each part of the problem, understand them, and see how they fit together. You can see that part of your solution is specified to help you get started: "using a loop statement".
The problem specifies the inputs (the numbers that will be typed in), and the outputs your program will produce. Let's go through it in a simplified way.
For each output, you need to think through how to obtain them from the inputs.
- The largest positive value entered
Q: How will the program obtain that?
A: You will need to create a variable, whose value keeps track of the largest positive value entered.
Q: What type will it be?
A: For now, just make it the type that you read in:int. Your specification doesn't say "whole numbers", or "integers", so a better choice may befloat. This is an example of where you should clarify with your client / user / teacher. For now, let's stick withint.
Q: Every variable should be initialized. What initial value will you give it?
A: Hmm. It needs to be smaller than every positive number, so that when the program compares the first input with it, the input will be greater. Technically, positive numbers are greater than zero, so zero is a good choice.
Q: What will it be named?
A: A harder question that it first appears. The names we choose give our code meaning, so they are worth thinking about. But we also have to practical. We could call itThe_largest_positive_value_entered, but we would soon get tired of typing and reading that.maximumis good and fairly clear,maxis good also, but in general avoid names likembecause they don't carry enough meaning.
Q: When will it's value be changed?
A: Inside the loop, your program will need to test (using pseudo-code) if value-entered > maximum then maximum <- (means is assigned) value-entered. You will need to translate that to C.
You need to follow similar reasoning for each output, and for how to decide on the loop, and also about what to do with nun-numeric input and no input.
Because you are a beginner, write the program to work with just and maximum first and test it, then add the code for the minimum and test it, then the average, etc.
Watch how the code works with the debugger to help yourself learn, and put in printf statements (that you will later comment out) to output values you want to know about.
Avoid copying other people's code. Programming is a practical skill you learn by doing. Reading other people's code can be good, because you can learn about style, techniques, and get ideas. But don't copy code if you want to learn to program.