Basic C Program - Help with logic

In summary, NemoReally suggests using scanf() with a string that contains the input values separated by spaces, and then using a long if/else block to check for the biggest number.
  • #1
Saterial
54
0
So I'm working on two questions while learning C.

The first question is, I need to create a program that prompts a user "Please enter an integer" and display whether the answer is odd or even. I'm using the logic that you can divide the input by 2, if the answer is a whole number it is even, if it's a decimal it is odd. How can you check whether the input is a decimal or not? Would this solution work for a question that asked for an input of 2 numbers, say "15 5"? Let 15 be a, 5 be b and divide a by b; if answer is whole number then it is a multiple, else it is an odd number?

Second question is, I need to create a program that prompts a user "Please enter a five integers:" for example "5 12 -10 0 1". From there it will display "The max is: 12" and "The min is -10". How can I do this without using an array?

I get the idea behind it, I just can't figure out how to make it check whether the output of the calculation is a float. Additionally, this is an intro to learning C class; my idea for a solution to question 1 was using something like "scanf("%d", a b c d e); and then a long line of code checking if each variable is greater than each other. This obviously didn't seem very efficient to me is there another basic way I can try?

Thanks!
 
Technology news on Phys.org
  • #2
Look up the modulus operator.

I've never done a C programming course, but I imagine you haven't covered macros or functions yet? In which case, note that you can use more than one line and that copy, paste and edit were made for some jobs. A prime consideration in writing code is to make it obvious what you have done and as easy to read as possible - long complex lines are both easy to mistype and difficult to interpret. Don't worry too much about "efficiency" at first, you should learn to become more efficient as you progress.
 
  • #3
So put them into individual variables and do a > b b > c and so on?
 
  • #4
Saterial said:
Second question is, I need to create a program that prompts a user "Please enter a five integers:" for example "5 12 -10 0 1". From there it will display "The max is: 12" and "The min is -10". How can I do this without using an array?

Read one integer, do the processing on it, then read the next one.

If the first integer is n, the maximum and minimum for the one integer you have read so far, are both n.

Now if you read the second integer into the same variable n, you can find the max and min of the first two integers ...

... and repeat, till you have read all five integers.
 
  • #5
To check whether a number is even or odd, as NemoReally mentioned, use the modulus (%) operator which returns the remainder after division, like this:

Code:
if (number % 2 == 0)
{
  /* number / 2 leaves 0 remainder, therefore number is even */
}
else
{
 /* number / 2 leaves a non-zero remainder, therefore number is odd */
}

Now, your question re: floating point numbers is a bit of a curly one. For the purpose of this exercise, I think you should avoid trying to work out if your number is a float, and just use the modulus approach above. For what it's worth, one way to check if a number has a fractional component, is to use the floor() function, which rounds a number down to the nearest whole number

Code:
if (floor(number) == number)
{
  /* number is an integer */
}
else
{
  /* number has a fractional component */
}

Due to the way a computer stores floating point numbers, you should never compare them using the equality operator (==). Instead, you should define an error bound and check to see whether the floats are equal within that bound. Note that the abs() function returns the absolute value, or magnitude of the number (i.e. makes negative numbers positive). This way you can minus one from the other to see how close they are.

Code:
float error_bound = 0.000005

if (abs(number1) - abs(number2) > error_bound)
{
  /* consider them equal */
}
else
{
  /* not equal */
}

Read up on IEEE floating point representation for further information on precision.

As for your 5 integers problem, the easiest way is to use scanf() but it's not the recommended way to accept user input in a C program because you can't handle errors. In any case:

Code:
printf("Enter 5 integers separated by spaces: ")
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e)

Will work and then you can use a long if/else block to check for the biggest one. Alternatively, you could use a loop and an array which is nicer:

Code:
int numbers[5];
int i;
for (i = 0; i < 5; i++)
{
  printf("Enter an integer: ");
  scanf("%d", &numbers[i]);
}

Now you have an array of 5 numbers, and you still need to find the max and min, you could use the big if/else block, or better, a little routine like this (which could be made into a function if you are up to that):

Code:
int i;
int max = numbers[0]; /* set max to the first number */
for (i = 1; i < 5; i++)
{
  if (numbers[i] > max) /* if this number is bigger than current max */
  {
    max = numbers[i]; /* set max to this number */
  }
}

and similarly for min.

Eventually, you should learn about better ways to handle user input using getc() which allows you to have control over how you handle errors.
 
  • #6
Additionally, for determining if a number is odd or even and you *don't* want to use the modulo operator, you can use the trick that integer are rounded, so,
Code:
n == 2*(n/2)
will be true for n even, and not for n odd.

For the comparison, ask yourself: if you had only one number, what would be the maximum? If you already have the maximum of the n-1 previously entered number, what will be the maximum when you enter the nth one?
 

1. How do I start writing a basic C program?

To start writing a basic C program, you will need to first set up a development environment by downloading a C compiler and an editor. Once you have your environment set up, you can start writing your program by declaring variables, writing functions, and using control structures to create the logic of your program.

2. What are variables and how do I declare them in a C program?

Variables are used to store data in a C program. They can hold values such as numbers, characters, and strings. To declare a variable in a C program, you will need to specify its data type and give it a name. For example, to declare an integer variable named "age", you would write "int age;"

3. How do I use control structures in a C program?

Control structures are used to control the flow of a program. They include if/else statements, loops, and switch statements. These structures allow you to make decisions and repeat actions based on certain conditions. To use a control structure, you will need to write the appropriate syntax and provide the necessary conditions and code blocks.

4. What is the purpose of functions in a C program?

Functions are blocks of code that perform a specific task. They are used to break down a program into smaller, more manageable parts. This makes it easier to read and understand the code, and also allows for code reuse. Functions can have parameters and return values, making them versatile for different purposes.

5. How do I debug my C program if it's not working correctly?

If your C program is not working correctly, you can use a debugger to find and fix errors in your code. Debuggers allow you to step through your code line by line and see the values of your variables at each step. This can help you identify the source of the problem and make necessary changes to your logic. Additionally, you can use print statements to output values and check the flow of your program.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
677
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
19
Views
961
  • Programming and Computer Science
Replies
2
Views
966
  • Programming and Computer Science
Replies
4
Views
728
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
Back
Top