C++ program, Please help.

closed account (DLUk92yv)
Hey, i am new to c++ programming and dont know much but I have this question i need to solve and i have done it but it i don't think it's correct. Can you help me out?

Question: Mr.X is preparing for the wedding ceremony of his son. He wants to arrange the event in their garden. But he doesn't have enough tables and chairs at home. He has asked tables and chairs from his neighbors. And thus, they all have different sizes. He wants to write a program that defines the number of chairs that can be used.

Given the width and length of a rectangular table and then the length of a chair, find out how many chairs can fit around the table?
Input specification
You will be given three integers: the width (tw) and length (tl) of the table and the width of a chair (cw) where 15 cm ≤ cw ≤ 40 cm and 9 cm ≤ (tw and tl) ≤ 280 cm
Note: All chairs have the same square shape.

Output specification
Find and show the number of chairs that can be placed around the rectangular table.

Sample Input I Sample Output I
80 40 18 12



Now this is my code:

#include <stdio.h>
int main()
{
int tw;
scanf("%d", &tw);
int tl;
scanf("%d", &tl);
int cw;
scanf("%d", &cw);


if (tw>=cw && tl>=cw)
total=(2*tw/cw)+(2*tl/cw);
else if (tw<cw && tl>=cw)
total=2*tl/cw;
else if (tw>=cw && tl<cw)
total=2*tw/cw;

printf("%d", total);
return 0;
}
So do you actually have a question? You've posted the assignment, some C code, but no actual question.

I thought you said this is a C++ program, where is your C++ code? I'd also recommend using meaningful variable names to make your program easier to understand.

closed account (DLUk92yv)
umm yeah the question was whats wrong in my code..and thanks for the advice
You need to ask specific questions. Tell us why you think your program is incorrect. For example what output is your program producing, if it even compiles and runs. What is the output you expect for the input you provided. If it doesn't compile what are your exact error messages?

closed account (DLUk92yv)
well actually i think its kind of correct because i also did some calculations and the output produced its okay, but when i submit it at school's page it says wrong answer and i have no idea what part might be wrong.
Okay so it's producing some answer, that's a good first step. What do you expect the answer to be for the input given in post 1 and what output is your program producing?

I will say you need to be careful about calculations like the following:
total=(2*tw/cw)+(2*tl/cw);
This is using integer math, and remember there are no fractional parts in integer math 3/4 would yield zero.

closed account (DLUk92yv)
the output is correct, just like it should be..

and actually as i said i am new to programming so I dont really understand the integer math part
An integer has no fractions! So if you divide one number by another number that doesn't have a remainder you're fine. However if you divide two numbers that have a remainder you lose that remainder.

So 1 / 2 will yield zero, 4 / 2 will yield 2, 99 / 100 will yield zero, etc.

How many different table length, table widths and chair widths did you try with your program? I doubt your schools test page will only test with one set of numbers.
Last edited on
closed account (DLUk92yv)
okay i understand now.
i tried with a couple but school gave us only 2 different input
Then I suggest you check with quite a few more. Verify the computer results with paper and pencil.

Topic archived. No new replies allowed.