This is a program which is supposed to find circular primes below certain max value. It works for max<=1000, but if max=10000 , the program ends without errors, but does not print the last two lines to console, even though it always should. It also doesn't print any more circular primes out, but that might be my algorithm problem, and I will worry about it later.
Note: I'm using MVS 2010, and there are unnecessary std:: before cout, because it sometimes says that cout is ambigous.
using namespace std;
int main(){
const int max = 10000;
int nrOfPrimes = 0;
int* primes = findIfPrimes(max);
for(int i = 2; i < max; i++){
//check if number is prime
if(primes[i] == 0){
nrOfPrimes++;
int l = 0;
int* permutations = findPermutations(i, l);
//variable saying lf all permutations are prime
bool allPrime = true;
//check all permutations, if they are not prime change allPrime
for(int j = 0; j < l; j++){
if(primes[permutations[j]] != 0){
allPrime = false;
break;
}
}
//if it wasnt circular prime- continue
if(allPrime == false)
continue;
//if it was circular prime, change all permutations to "circular prime"
std::cout << "Circular primes: ";
for(int j = 0; j < l; j++){
primes[permutations[j]] = 2;
std::cout << permutations[j] << " " << endl;
}
std::cout << endl;
}
}
//find total count
int result = 0;
for(int i = 2; i < max; i++)
if(primes[i] == 2)
result++;
std::cout << "total number of primes " << nrOfPrimes << endl;
std::cout << "total number of circular primes " << result << endl;
return 0;
}
Put just before return 0 in main():
bool fail = cout.fail();
cout.clear();
cout << fail << endl;
cin.ignore(cin.rdbuf()->in_avail()); // clears all remaining input
cin.get(); // waits for ENTER
What tells it for fail?
Do the same with fail = cout.good();. What tells it then?
Related
For this vectors and merging assignment, we are supposed to read in user inputted strings and sort them alphabetically. I got the first two parts, but when I am putting the sorted elements in the new vector, it says that my new vector is out of range. Does anyone know how to fix this?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> que1;
vector<string> que2;
vector<string> que_merge;
string firstName;
string secondName;
int counterq1 = 0;
int counterq2 = 0;
cout << "Enter queues: " << endl;
bool check = true;
while(check) {
cin >> firstName;
if (firstName == "ENDQ"){
check = false;
}
else{
que1.push_back(firstName);
counterq1++;
check = true;
}
}
// que1.resize(counterq1);
bool check2 = true;
while (check2) {
cin >> secondName;
if (secondName == "ENDQ") {
check2 = false;
} else {
que2.push_back(secondName);
counterq2++;
}
}
// que2.resize(counterq2);
cout << "que1: " << counterq1 << endl;
for (int i = 0; i < que1.size(); i++) {
cout << que1.at(i) << endl;
}
cout << endl;
cout << "que2: " << counterq2 << endl;
for (int j = 0; j < que2.size(); j++) {
cout << que2.at(j) << endl;
}
cout << endl;
cout << "que_merge: " << counterq1 + counterq2 << endl;
int i = 0;
int k = 0;
int j = 0;
while (1){
if (i >= counterq1 || j >= counterq2){
break;
}
if(que1.at(i) < que2.at(j)){
que_merge.push_back(que1.at(i));
i++;
}
else{
que_merge.push_back(que2.at(j));
j++;
}
k++;
}
if (que1.empty()){
for (int m = j; m < counterq2; m++){
que_merge.push_back(que2.at(m));
}
} else {
for (int l = i; l < counterq1; ++l) {
que_merge.push_back(que1.at(l));
}
}
for (int l = 0; l < (counterq1+counterq2); l++) {
cout << que_merge.at(l) << endl;
}
return 0;
}
I think your problem is that these lines:
if (que1.empty()){
for (int m = j; m < counterq2; m++){
que_merge.push_back(que2.at(m));
}
} else {
for (int l = i; l < counterq1; ++l) {
que_merge.push_back(que1.at(l));
}
}
doesn't do what you expect.
As far as I can see, your idea is to merge the remaining element from either que1 or que2.
However, that is not what you get from this code as the elements in que1 and que2 is never erased. In other words - the check for an empty queue is rather meaningless and you can't be sure that all elements are added to que_merge
So when you do:
for (int l = 0; l < (counterq1+counterq2); l++) {
cout << que_merge.at(l) << endl;
}
you may read beyond the number of elements in que_merge
Tip:
Don't count the number of elements your self. Use size() (like que_merge.size()) instead. For instance:
for (int l = 0; l < que_merge.size(); l++) {
cout << que_merge.at(l) << endl;
}
or a range based loop like:
for (const auto& s : que_merge) {
cout << s << endl;
}
#include <algorithm>
Use std::merge and std::sort, not necessarily in that order.
If the assignment says you can't do it the C++ way, that you have to write it all out, you can do it the engineer's way: Find an example that works, and crib it.
There are two possible implementations of std::merge on this page: All about C++ merge
I used the following code for palindrome check of an integer array and used the value of variable 'declare' as check of palindrome. I used the technique that if declare is 1 at the end, array is palindrome, else not. But its not working. In the end of code, it always keeps the value of declare which was initialized, independent of rest of the code. Please Debug.
#include <iostream>
using namespace std;
void main()
{
int array1[3] = {0,0,1};
int j = 2;
cout << "Given Array is:\n";
for (int i = 0; i < 3; i++)
cout << array1[i];
cout << endl;
int determiner[3];
for (int i = 0; i <3; i++){
determiner[j] = array1[i];
j -= 1;
}
cout << "Reversed Array is:\n";
for (int i = 0; i < 3; i++)
cout << determiner[i];
cout << endl;
int declare;
for (int u = 0; u < 3; u++)
{
if (array1[u] = determiner[u])
{
declare = 1;
}
if (array1[u] != determiner[u])
{
declare = 0;
break;
}
}
cout << endl;
cout << declare<< endl;
if (declare==1)
cout << "Given Array is Palindrome. Cheers!!!\n";
if (declare==0)
cout << "Emhmm! This aint Palindrome.\n";
system("pause");
}
if (array1[u] = determiner[u])
should be
if (array1[u] == determiner[u])
I am suppose to make a program that asks a user to input an integer 'n' between 1 and 100, and have an input validation loop. The program will then calculate the first 'n' prime numbers and print them. So I figured out how to calculate the prime numbers and display them, 10 numbers per line, all in the main function. What I have to do is to have a function called isPrime() that takes an integer and returns true if it is prime and false otherwise. I'm not sure how to go about this. This is the code I have for function main().
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int number;
int count = 0;
cout << "Enter an integer between 1 and 100: ";
cin >> number;
while (number < 0 || number > 100)
{
cout << "Invalid number." << endl;
cout << "Enter an integer between 1 and 100: ";
cin >> number;
}
cout << "The first " << number << " primes: \n" << endl;
for (int i = 2; number > 0; ++i)
{
bool isPrime = true;
for (int j = 2; j < i; ++j)
{
if (i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
count++;
--number;
cout << setw(5) << i;
if (count % 10 == 0)
cout << endl;
}
}
cout << endl;
system("pause");
return 0;
}
Any help is good, thanks in advance.
You seem to be testing i % j for every j lower then i. But if you tested for j = 3 then you can exclude all multiples of 3. When a number can't be divided by 3 it can't be divided by 6. This will improve performance.
You can also implement the AKS primalty test https://en.wikipedia.org/wiki/AKS_primality_test
This might be a little more work. I don't know the details of this test but it is deterministic and works on all numbers.
If you want a function bool isPrime(int number) you can just extract this code from your main method, it looks something like this:
bool isPrime(int number){
for (int j = 2; j < number/2; ++j)
{
if (number % j == 0)
{
return false;
}
}
return true;
}
The for loop in the main function will be something like:
for (int i = 2; number > 0; ++i)
{
if (isPrime(i))
{
count++;
--number;
cout << setw(5) << i;
if (count % 10 == 0)
cout << endl;
}
}
So I'm trying to make a program that will separate one array of ints into two, one for even ints, and one for uneven ints. Now, the strange thing is, if I only enter even or uneven numbers into the base array, the program works fine, but if I enter a mix of the two, one of the values held by the two new array will be a random, usually negative, big number, any idea why that is?
#include <iostream>
using namespace std;
void main()
{
int *a, n, *even_nums = 0, *uneven_nums = 0, counter_even = 0,counter_uneven = 0;
cout << "How many values does your array have?\n" << endl;
cin >> n;
a = new int[n];
cout << "\nEnter the values in your array:" << endl;
for (int i = 0; i < n; i++)
{
cout << "a[" << i << "] = ";
cin >> a[i];
if (a[i] % 2 == 0)
counter_even++;
else
counter_uneven++;
}
if (counter_even == 0)
cout << "There are no even numbers in your array." << endl;
else
even_nums = new int[counter_even];
if (counter_uneven == 0)
cout << "There are no uneven numbers in your array." << endl;
else
uneven_nums = new int[counter_uneven];
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
even_nums[i] = a[i];
else
uneven_nums[i] = a[i];
}
if (counter_even != 0)
{
cout << "\nThe even numbers in your array are:" << endl;
for (int i = 0; i < counter_even; i++)
cout << even_nums[i] << " ";
}
if (counter_uneven != 0)
{
cout << "\nThe uneven numbers in your array are:" << endl;
for (int i = 0; i < counter_uneven; i++)
cout << uneven_nums[i] << " ";
}
system("PAUSE");
}
In
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
even_nums[i] = a[i];
else
uneven_nums[i] = a[i];
}
You are using the same index for all of the arrays. This will not work as even_nums and uneven_nums will be smaller than a if you have both. You will eventually be writing past the end of the array which is undefined behavior.
What you need to do is add one index for each array and every time you insert an element into the array then you advance that index.
for (int i = 0, u = 0, e = 0; i < n; i++)
{
if (a[i] % 2 == 0)
even_nums[e++] = a[i];
else
uneven_nums[u++] = a[i];
}
Also you are using void main() which is not standard and should not be used. int main() and int main(int argc, char** argv) are the standard acceptable signatures of main()
In the block
for (int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
even_nums[i] = a[i];
else
uneven_nums[i] = a[i];
}
you are using i as the same index for arrays a, even_nums and uneven_nums. You need to use separate indexes for these arrays.
For example, if you have n=10 elements, 5 even and 5 odd, your even_nums and uneven_nums contains only 5 elements each.
I'm almost done with my Bulls and Cows project however if I enter a word or a sequence of numbers with an alphabet or number repeating, the 'cow' portion of the code messes up. As an example: consider the following
Enter something that you want someone to guess: cool
Time to guess! The code is of size 4. book
COWS: 0 BULLS: 2
ozzo
COWS: 4 BULLS: 0
As you can see, after entering "ozzo", the cow value should be 2, not 4.
How can I fix this without having to change the entire code?
for (size_t i = 0; i != startg.getSize(); ++i){
if (guess[i] == origWord[i]){
bullCtr++;
} else {
for (size_t j = 0; j != startg.getSize(); ++j){
if (origWord[i] == guess[j]){
cowCtr++;
}
}
}
}
Code after applying fix:
for (size_t i = 0; i != startg.getSize(); ++i){
if (guess[i] == origWord[i]){
bullCtr++;
} else {
for (size_t j = 0; j != startg.getSize(); ++j){
if (origWord[i] == guess[j]){
origWord[i] = 'X';
cowCtr++;
}
}
}
origWord = origWordcpy;
}
Your cow checking is problematic.
What I would do for the sake of ease (not exactly) is this (I'm talking about the else statement only):
for(unsigned int j = 0 ; j != startg.getSize() ; j++)
{
if(origWord[i] == guess[j])
{
origWord[i] = 1; //Just assigning a certain value there to mark that we've already did something with it
cowCtr++;
}
}
And that should do the work.
EDIT:
You should obviously have a temporary string instead of origWord because changing it would affect the next iteration of the outer loop (getting the guess and comparing again) - I only showed you the way.
Here is one possible implementation of the Bulls & Cows game:
// used constants; numbers to be guessed
const int first_num = 2;
const int second_num = 4;
const int third_num = 1;
const int forth_num = 5;
int main(){
// vector holding the values to be guessed
vector<int>gamenum(4);
gamenum[0] = first_num;
gamenum[1] = second_num;
gamenum[2] = third_num;
gamenum[3] = forth_num;
// prompt message; input cycle till perfect guess (4 bulls)
int bulls = 0;
while (!(bulls == 4)){
// vector holding the guesses
vector<int>guesses;
// vector input values
int guess1(0), guess2(0), guess3(0), guess4(0);
cout << "\t\tPlay the game ""Bulls and Cows\n""" << endl;
cout << "Enter a set of four numbers, separated by whitespace space: ";
cin >> guess1 >> guess2 >> guess3 >> guess4;
guesses.push_back(guess1);
guesses.push_back(guess2);
guesses.push_back(guess3);
guesses.push_back(guess4);
// input confirmation; show your guess
cout << "\nYour guess is: ";
for (int i = 0; i < guesses.size(); ++i){
cout << guesses[i];
}
// bulls criterion
for (int j = 0; j < guesses.size(); ++j){
if (guesses[j] == gamenum[j]) ++bulls;
}
// cows criterion
int cows = 0;
for (int gue = 0; gue < guesses.size(); ++gue){
for (int gam = 0; gam < gamenum.size(); ++gam){
if (guesses[gue] == gamenum[gam] && gue != gam) ++cows;
}
}
// print result
if (bulls < 4){
cout << "\nBulls: " << bulls << " and Cows: " << cows <<endl;
cout << "\n\n\n" << endl;
// reset bulls
bulls = 0;
}
// empty guesses vector
guesses.clear();
// reset cows
cows = 0;
}
// print success
cout << "\nPerfect Guess!" << endl;
cout << "Bulls: " << bulls << endl;
cout << "\n\n\n" << endl;
keep_window_open();
return 0;
}
Not optimal by any means, rudimentary, but working. You can use it as benchmark.