January 6th, 2013, 10:34 PM

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.
Please help!
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., forloop + ifthen).
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 ;
}
January 6th, 2013, 11:06 PM

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 ifstatement 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 ifstatement. 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 forloop, right? Use the forloop to assign a value to num and then test num for satisfying the conditions, right?
So why don't you use a forloop to go from 7 to INT_MAX (consult and #include your compiler's limits.h header file)? That would be the bruteforce 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.
January 7th, 2013, 09:11 AM

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 and Makefiles!