C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2013, 10:34 PM
davis123 davis123 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 davis123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.
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., 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 ;
}

Reply With Quote
  #2  
Old January 6th, 2013, 11:06 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,123 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 5 m 8 sec
Reputation Power: 1949
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.

Reply With Quote
  #3  
Old January 7th, 2013, 09:11 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Calculation Program for C++

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap