Hack Forums
[C] Need to create a simple program for homework - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Need to create a simple program for homework (/showthread.php?tid=5902303)



Need to create a simple program for homework - Voxi - Nov 1, 2018

Here is what I have to do:

You know that the combination to a safe is a four-digit integer (between 1000 and 9999) that:
a) is a perfect square, and
b) is not divisible by 2, 3 or 5.


Could anyone help me out on where i'd start? I'm really bad at C++ and think i'm gonna fail my class lol


RE: Need to create a simple program for homework - Mouse❤️ - Nov 1, 2018

If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.


RE: Need to create a simple program for homework - Souffrance - Nov 1, 2018

Assuming you have to code it, the way I would start is by having a loop that goes from:

32 till 99.

In each of these iterations of the loop, get the squared number (i.e. number times itself, then check if it's divisible by 2, 3 or 5.) If it isn't divisible by any of them. Print it out.

Of course that's just assuming this is what the assignment actually wants you to do.

You'll need to use a for loop and an if statement to accomplish this.


RE: Need to create a simple program for homework - int main() - Nov 2, 2018

(Nov 1, 2018 03:55 PM)Mouse❤️ Wrote:  If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.

LOOOOOOOOOL poor guy


RE: Need to create a simple program for homework - FoxBlood - Nov 2, 2018

Nobody will help you here, it's about your programming education, if you don't like programming that means you are in a wrong school


RE: Need to create a simple program for homework - Abruptum - Nov 2, 2018

Hey,
The Solution is quite easy to understand.

Code
#include <math.h>
#include <iostream>


using namespace std;
bool isDivisible(int x, int n);
bool isPerfectSquare(int n);
int main(){
    int a=1000;
    while(a<10000){
        if(!isDivisible(a,2) && !isDivisible(a,3) && !isDivisible(a,5) && isPerfectSquare(a)){
            cout<<a<<"\n";
        }
        a++;
    }
    return 0;
}
bool isPerfectSquare(int n){
    int squareRootN=(int)(sqrt((double)n)+0.5);
    return squareRootN*squareRootN == n;
}
bool isDivisible(int x, int n){
    return ((double)x/(double)n) == (double)(x/n);
}

Do it now yourself, use stackoverflow and other sites to gain informations how other people have solved this problem.

(There is a better version and you should be able to create it now on your own)


RE: Need to create a simple program for homework - Souffrance - Nov 2, 2018

(Nov 2, 2018 08:37 AM)Abruptum Wrote:  Hey,
The Solution is quite easy to understand.

Code
#include <math.h>
#include <iostream>


using namespace std;
bool isDivisible(int x, int n);
bool isPerfectSquare(int n);
int main(){
   int a=1000;
   while(a<10000){
       if(!isDivisible(a,2) && !isDivisible(a,3) && !isDivisible(a,5) && isPerfectSquare(a)){
           cout<<a<<"\n";
       }
       a++;
   }
   return 0;
}
bool isPerfectSquare(int n){
   int squareRootN=(int)(sqrt((double)n)+0.5);
   return squareRootN*squareRootN == n;
}
bool isDivisible(int x, int n){
   return ((double)x/(double)n) == (double)(x/n);
}

Do it now yourself, use stackoverflow and other sites to gain informations how other people have solved this problem.

(There is a better version and you should be able to create it now on your own)

This is inefficient, I'm not sure why you're checking if each number is a perfect square at all. Just generate only perfect squares instead.


RE: Need to create a simple program for homework - icko1996 - Nov 2, 2018

Don't forget about the modulus operator.

You can check if a number is divisible by another number the following way:
( x % y ) == 0, where x is the dividend, y the divisor.

Good luck with your assignment!


RE: Need to create a simple program for homework - Abruptum - Nov 2, 2018

(Nov 2, 2018 12:21 PM)Souffrance Wrote:  This is inefficient, I'm not sure why you're checking if each number is a perfect square at all. Just generate only perfect squares instead.

OMG ;D it was just an bad example so he could improve it.
Pleas read the whole post and don't bitch around.


RE: Need to create a simple program for homework - incognito04 - Nov 14, 2018

A short example program 🤝.
PHP Code

<?php 
#include <cmath>
#include <array>
#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>

int main()
{
 
   const auto is_div = [](const short x, const short y) -> bool {
 
       return x % y == 0;
 
   };

 
   const auto is_sqrt = [](const short n) -> bool {
 
       const auto sr = std::sqrt(n); 
 
       return ((sr - std::floor(sr)) == 0);
 
   };

 
   std::array<short, 9000> data;

 
   std::iota(data.begin(), data.end(), 1000);

 
   std::vector<short> copyOfData;

 
   const auto itr = std::copy_if(data.begin(), data.end(), std::back_inserter(copyOfData), [&is_div, &is_sqrt](const short n) {
 
       return is_sqrt(n) && is_div(n, 2) && is_div(n, 3) && is_div(n, 5); 
 
   });

 
   for (const auto n : copyOfData)
 
       std::cout << n << '\n';

 
   return 0;
} 



RE: Need to create a simple program for homework - kray - Nov 15, 2018

(Nov 1, 2018 03:55 PM)Mouse❤️ Wrote:  If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.

after first reading the original post, I was confused. After reading your reply, I laughed so hard I'm now dead.