Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working in c++ and I am trying to find the 1001th prime number. When I run this code it throws error on line 14 that the function is overloaded. Can you tell me why it is throwing me the error? Thanks.
#include <iostream>
using namespace std;
int main() {
cout << "Hi in this program I will tell you the 1001th prime number" << endl;
int number;
for (int i = 2; i < 1000000; i++) {
for (int prime = 2; prime < 1000000; prime ++) {
if (prime % i != 0) {
for (int count = 1; count < 100000; count ++) {}
cout << count << prime << " is a prime number" << endl;// this line has the problem
}
}
}
}
The "count" variable has it's scope only within the "for" loop. That loop terminates with the '}' character at the end of line.
The count you are referencing on your cout line is from somewhere else. I assume that somewhere in global scope you're pulling in definitions of two "count" functions and the compiler thinks you want to refernce one of those, but doesn't know which one to take.
Take a look at your curly braces after the for loop:
for (int count = 1; count < 100000; count ++) {}
You're closing the for loop block before you get to the next line, where you reference 'count'. So, the count variable is not in scope when you try to print it. Try changing it to this:
if (prime % i != 0) {
for (int count = 1; count < 100000; count ++) {
cout << count << prime << " is a prime number" << endl;
} // closing curly brace is here now :)
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include
using namespace std;
int main()
{
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
I understand the below problem of finding factors of numbers. But i want to do a c++ program which only show the numbers which have 5 or more factors. suppose i give a range of numbers 15 to 20.then it will print only those numbers those have 5 or more factors. such as example if i give a range 15 to 20 then it will print out only 16,18,20.because these 3 integers have 5 or more factors in 15 to 20 range. i couldnt understand how to do that code so i am asking.
As I understood you are searching the tech finding number prime factors of an natural number. Firstly the code you published is for getting all the divisor's of given positive number. But Finding its prime factors a little bit different but the idea same as you used (modular arithmetic)
this is a very simple version of achieving your task (but needs optimization)
#include <iostream>
//This function does not handle the repeating factors count
int numberOfPrimeFactors(int number) {
int count = 0;
for ( int i = 2; i <= number; ++i ) {
while ( number % i == 0 ) {
number /= i;
count++;
}
}
return count;
}
int main() {
int Rbegin = 1;
int Rend = 100;
for(int i = Rbegin; i<Rend; ++i) {
if(numberOfPrimeFactors(i) >= 5)
std::cout << i << " has 5 or more prime factor"<< std::endl;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to make a part of the game hangman, where the user inputs a letter and then a loop check for that letter in a random array. If the letter is found, it then couts a changed array including now that letter and offers the user to again, input another letter. It seems for loops are not working since the program doesnt scan the whole array for ever letter inputted. How can I fix this?
int main(){
string guess[25];
string password[5];
srand((unsigned)time(0));
string letters[5] = {"_ ","_ ","_ ","_ ","_ "};
char array[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','r','s','t','u','v','z'};
for(int r = 0; r < 5; r++){
int g = rand() % 24;
password[r] = array[g];
}
cout << endl;
for(int z = 0; z < 25; z++){
cout << "Enter Letter: " << endl;
cin >> guess[z];
for(int b = 0; b < 5; b++){
if(uguess[z] == password[b]){
letters[b] = guess[b];
cout << letters[b];
}else{
cout << letters[b];
}
}
cout << endl;
}
can someone point me in the right direction. Thanks
It always says that the word being guessed if asdfg, but it messes it up very badly, as in doesn't always show the letter, even if it has been guessed, it shows it later.
crke[b] = ugib[b];
This line should be:
crke[b] = ugib[z];
You might want to consider investing some time in learning how to use a debugger, which would've helped you figure it out.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to get the greatest value from the array and its index number also by using a function maxin but my logic somehow isn't working?
#include <iostream>
#include <conio.h>
#include <proceass.h>
void maxin(double[], int);
void main()
{
const int k = 10;
int l = 0;
double num[k];
for (int j = 0; j < k; j++)
{
cout << "Enter the number " << j + 1 << " = ";
cin >> num[j];
if (cin.fail())
{
cout << "Wrong data entered " << "\nTry again";
getch();
exit(0);
}
}
maxin(num, l);
cout << "The Greatest number is = " << num;
cout << "\nIt is " << l << "th number";
getch();
}
void maxin(double k[], int p)
{
int l, s;
l = 10;
s = 0;
double m;
for (int n = 0; n < l; n++)
{
if (k[s] > k[n++])
{
m = k[n];
}
else
{
m = k[n++];
s = ++;
}
}
p = s;
k[s] = m;
}
Your maxin function is invoking Undefined Behavior on your program for causing access to areas beyond the bounds of the array k. This happens because not only is n incremented in the for loop statement, but again in the if statement which is evaluated on each iteration as well. This also happens in the else statement, which is another case of the problem.
When n is 1 less than l, n++ will be >= l, and subsequently dereferencing that address, k[n++], will cause Undefined Behavior. After that, anything can happen to your program, including valid or invalid side effects.
When finding the maximum/minimum value in an array, a variable is usually set to an arbitrary value in the array (typically the first index), and then iteration is performed to check if any other value in the array is smaller/larger than that variable. When that condition passes, the variable is set to the new value in the array.
Furthermore, since you said you needed to set the variable to the index at which the largest value was found, it is necessary that you pass p by reference.
The STL approach:
vector< double > v = {1,2,3,4,5};
auto maxElemIter = std::max_element(begin(v), end(v));
cout << "Max is: " << *maxElemIter;
cout << ", at index: " << distance(begin(v), maxElemIter) << endl;
(I know, this is a cruel suggestion, given code as stated in question above...)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm having a super simple problem, but I also had a question, so I figured I'd post both.
First of all, I'm not sure what's not working about this program cycling through an array and out putting the values. What's happening now, is that it just outputs the last value in the array.
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
int i = 0;
for( i = 0; i < 9; i++);
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
that's not working for some reason, but I also wanted to know why, if I initialize the i variable inside the for loop, it says myArray[i] inside of the for loops, at the cout, isn't initialized at all. Peculiar to me.
I just tested the following code and it worked:
#include <iostream>
using namespace std;
int main() {
int myArray[10] = {0,1,1,2,3,5,8,13,21,34};
for(int i = 0; i < 10; i++) {
cout << i + 1 << '\t' << myArray[i] << endl;
}
}
Notice the syntactical changes that were made. Also you were previously leaving out the last element in the array.
What's important to note is that a semicolon is a null statement. By placing a semicolon after a for loop, you are executing the null statement (i.e. do nothing) for the total number of iterations through the loop. Afterwards, the code inside the curly braces is run as if it were regular code in the body of your method. That's why only the last iteration was printing, because the for loop had "done nothing" for all the other iterations but still incremented i. Therefore, when the code within the braces ran, it did so for the last expected iteration.
for( i = 0; i < 9; i++);
// ^here
Remove the extra semicolon. The original code in your question is equivalent to:
for( i = 0; i < 9; i++)
{
//do nothing
}
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Now you can declare i inside the for loop:
for( int i = 0; i < 9; i++)
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
You have an extra semicolon here:
for( i = 0; i < 9; i++); // there should be no semi-colon at the end of this line.
// ^ remove this!
What's happening is that the compiler iterates the loop completely, running the "empty" loop body (that extra semicolon). After the loop is done, it reaches your cout statement.
for( i = 0; i < 9; i++);
// ^ THIS TINY PIECE OF ABOMINATION
{
cout << i + 1 << '\t' << myArray[i] << endl;
}
Remove it.
The block below the for statement isn't associated with it because the for is actually associated with a null statement (indicated by the ; immediately after the for()).
You should also put your declaration of i inside for
for(int i = 0; i < 9; i++) {
...
That way, you limit its scope and makes you less prone to some errors.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Enter an odd value greater than zero -- I understand how to do that.
Print out a triangle that looks like the following if 5 is entered:
54321
432
3
If 11 is entered:
10987654321
098765432
9876543
and so on
I see that we must divide the input by 10 and print the remainder but I'm having trouble printing the countdown.
for (i = n; i >= 1; i--)
i = i - 1;
use a for loop (start at 0), and on each iteration print a substring from i to string.length()-1 an use the set width to increase the indent, following code below:
string num = "10987654321";
for (int i = 0; i < num.length(); ++i){
cout << setw(i) << right << num.substr(i, num.length()-i) << endl;
}
This should give you your desired output. (if "<< right <<" doesn't work, swap "right" with "left")
ALSO
you need to include iomanip to use setw()
int num = 10987654321;
int numSpaces = 1;
cout << num << endl;
for (int i = 0; i >= 1; i--)
{
for (int j = 0; j < numSpaces; j++)
{
cout << " ";
}
cout << num % 10 << endl;
num = num / 10;
numSpaces++;
}
Voila a beautiful triangle :)