| New Reply |
Basic C Program - Help with logic! |
Share Thread | Thread Tools |
| Jan30-13, 06:27 PM | #1 |
|
|
Basic C Program - Help with logic!
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 eachother. This obviously didn't seem very efficient to me is there another basic way I can try? Thanks! |
| Jan30-13, 06:44 PM | #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. |
| Jan30-13, 07:08 PM | #3 |
|
|
So put them into individual variables and do a > b b > c and so on?
|
| Jan30-13, 08:02 PM | #4 |
Recognitions:
|
Basic C Program - Help with logic!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. |
| Jan30-13, 08:25 PM | #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 */
}
Code:
if (floor(number) == number)
{
/* number is an integer */
}
else
{
/* number has a fractional component */
}
Code:
float error_bound = 0.000005
if (abs(number1) - abs(number2) > error_bound)
{
/* consider them equal */
}
else
{
/* not equal */
}
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)
Code:
int numbers[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter an integer: ");
scanf("%d", &numbers[i]);
}
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 */
}
}
Eventually, you should learn about better ways to handle user input using getc() which allows you to have control over how you handle errors. |
| Jan31-13, 03:46 AM | #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) 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? |
| New Reply |
| Thread Tools | |
Similar Threads for: Basic C Program - Help with logic!
|
||||
| Thread | Forum | Replies | ||
| Help with Logic of Program | Programming & Comp Sci | 3 | ||
| Need Help with the Logic of my Program | Programming & Comp Sci | 4 | ||
| Searching for Interdisciplinary PhD Program on Logic | Academic Guidance | 1 | ||
| logic circuits drawing program | Engineering, Comp Sci, & Technology Homework | 3 | ||