c - 3-digit integer number program won't execute -
yes, basic c coding homework problem. no, not looking me. considering first programming class, i'm not surprised can't work, , i'm there plenty wrong it. want pointing out problems in code , things missing can fix them on own.
homework question:
write program read 1 integer number (your input must 1 3 digit number 100 999), , think of number being abc (where a, b, , c 3 digits of number). now, form number become abc, bca, , cab, find out remainder of these 3 numbers when divided 11. assume remainders respectively x, y, , z , add them x+y, y+z, , z+x. if of these summations odd number, increase 11 if summation plus 11 less 20, otherwise decrease summation 11 (this summation operation must positive number less 20). finally, divide each of sums in half. now, print out resulting digits.
my code:
#include <math.h> #include <stdio.h> #include <stdlib.h> int main() { //declare variables int orignumber; int x, y, z; int number; number = x, y, z; int sum; // printf("input 3 digit number"); // int c; c = orignumber %10; // int b; b=((orignumber - c) % 100)/10; // int a; = (orignumber - (b + c))/100; // int abc, bca, cab; abc = (a*100) + (10*b) + c; bca = (10*b) + c + (a*100); cab = c + (a*100) + (10*b); // if((number % 2) == 1) { if(number + 11 < 20) number += 11; else if((100 - 11 > 0) && (100 - 11 < 20)) number -= 11; } // x = abc/11; y = bca/11; z = cab/11; // sum = (x + y), (y + z), (z + x); }
to start with, need read input. start prompt includes carriage return:
printf("input 3 digit number: \n");
since it's 3 digit number, add following line read input:
scanf("%3d", &orignumber);
the next bit of code works quite until if (number % 2)
meaningless since didn't define number
- well, did, line
number = x, y, z;
does not think does. if add
printf("so far have abc=%d, bca=%d, cab=%d\n", abc, bca, cab);
after first read in number , computed three, see on way.
note that
number = x, y, z;
uses thing called "comma operator". things (a,b,c) "evaluated" values not returned. @ rate, have line, didn't yet assign value x,y , z.
is enough started?
update have had few hours mull over, here few more pointers.
your computation of abc, cab, bca
makes no sense. show 1 of them:
cab = c*100 + a*10 + b;
next need compute each of x, y , z. again, here 1 of three:
y = bca%11;
now have make sums - call them xy
, yz
, , zx
. 1 of them:
zx = z + x;
next, deal instruction: "now if of these summations odd number, increase 11 if summation plus 11 less 20, otherwise decrease summation 11:
if(xy % 2 == 1) { if(xy + 11 < 20) xy += 11; else xy -= 11; }
use similar code 3 sums. "divide 2":
xy /= 2;
repeat needed.
finally, print out result:
printf("xy: %d, yz: %d, zx: %d\n", xy, yz, zx);
the amazing thing if did right, original numbers back...
you make code more compact using array of values , looping through - rather repeating code snippets wrote above different variables. suspect outside scope of expected know @ point.
can take here?
Comments
Post a Comment