Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
January 6th, 2013, 10:34 PM
 davis123
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 50 m 31 sec
Reputation Power: 0
Calculation Program for C++

Hello, I am new to C++. I am trying to build a program that solves this the bellow problem. I've started it but I'm no sure if this is right.

Problem:

A Gym Teacher's riddle
A Gym teacher lines up her students in groups and sometimes has lone students, that is, there are students not in any group, given the situations below determine how many students are in the gym teacher's class.
When she lines them up by 2's, there is a lone student
When she lines them up by 3's, there is a lone student
When she lines them up by 4's, there is a lone student
When she lines them up by 5's, there is a lone student
When she lines them up by 6's, there is a lone student
When she lines them up by 7's, there are no left over students.
Hint:
Set up the equality
Let the brute computing force of the computer solve the math for you (e.g., for-loop + if-then).

Code:
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int num ;

if ( (num % 2 == 1) && (num % 3 == 1) && (num % 4 == 1) && (num % 5 == 1) && (num % 6 == 1) && (num % 7 == 0) )
{
cout << " The number of students is " << num << endl ;

}
getch();
return 0 ;
}

#2
January 6th, 2013, 11:06 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,256
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 36 m 23 sec
Reputation Power: 1985
One: As presented, your code has no formatting. If it originally had any formatting, then that has been stripped away since everything here is in HTML -- leading spaces disappear. You need to present your code using code tags, thus:
Code:
```#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int num ;

if ( (num % 2 == 1) && (num % 3 == 1) && (num % 4 == 1) && (num % 5 == 1) && (num % 6 == 1) && (num % 7 == 0) )
{
cout << " The number of students is " << num << endl ;

}
getch();
return 0 ;
}```

Learn it! Use it!

Two: The logic of your if-statement is guaranteed to yield a false.

Please present me with one single number that is evenly divisible by 7 and yet has a remainder of 1 when divided by 2, 3, 4, 5, and 6. That is what you are testing for and, I believe, you are guaranteed to never satisfy the conditions of that if-statement. Maybe such a number exists, but it would be very esoteric indeed, and extremely rare.

Oops! Excuse me! Your assignment is to come up with such a number! Carry on!

Three: num has no value! Or rather, it contains a single value, a garbage value, since that local variable is uninitialized and hence contains garbage. What is the probability that some random garbage value is magically the one you are seeking? Mindbogglingly nil!

OK, now stop and think for a change. You are to employ a "brute force" approach. That means that you try every possibility. OK, what that tells me is that you try each and every possible value; that is the very definition of "brute force". Which suggests what? That you try multiple values. What values? Every single positive value the num can have. Now what does that suggest? A for-loop, right? Use the for-loop to assign a value to num and then test num for satisfying the conditions, right?

So why don't you use a for-loop to go from 7 to INT_MAX (consult and #include your compiler's limits.h header file)? That would be the brute-force approach. Though as an optimization, you could iterate by 7 rather than by 1. After all, each number needs to be a multiple of 7, right?

Think about that for a bit.

#3
January 7th, 2013, 09:11 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,460
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
j solutions ( www.jsoftware.com ):

Filter =: (#~`)(`:6)
4 5 6&([: *./ 1 = |/)Filter 7*i.1e6

As dwise_1 said, you'll need ways to
• initialize num
• change num
• stop when you've found enough solutions
• use brutishness intelligently.
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Calculation Program for C++